Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: bug in Perl compilation?

by ikegami (Pope)
on Dec 21, 2005 at 18:47 UTC ( #518396=note: print w/replies, xml ) Need Help??


in reply to bug in Perl compilation?

I'm curious as to what

perl -e 'printf("%.30f\n", $_) foreach 12e-4, 1.2e-3'

prints for you. I get

0.001199999999999999900000000000 0.001199999999999999900000000000

on ActivePerl v5.6.1 on Win2k (where I get 0.0012 is equal to 0.0012).

Replies are listed 'Best First'.
Re^2: bug in Perl compilation?
by chas (Priest) on Dec 21, 2005 at 19:27 UTC
    With perl v5.6.1 on WinXP (*not* ActivePerl), I get the same as you did.
    And the OP's code produces "0.0012 is equal to 0.0012" for me.
Re^2: bug in Perl compilation?
by bkgallo (Initiate) on Dec 21, 2005 at 19:25 UTC
    LINUX:
    0.001199999999999999894875757356 0.001200000000000000111716191853
    HP-UX:
    0.001199999999999999890000000000 0.001199999999999999890000000000
    WIN2K (with ActivePerl):
    0.001200000000000000000000000000 0.001200000000000000000000000000

    Even weirder results than with duff's code.

    Anybody know any way to make this portable across all the platforms?

      That's so weird!! I thought this was all standardized.

      Anybody know any way to make this portable across all the platforms?

      When dealing with reals, you usually need to compare within a tolerance.
      a == b
      is equivalent to
      a - b == 0
      and to
      |a - b| <= 0
      With a tolerance, it becomes
      |a - b| <= tol

      Tolerance can be absolute
      (e.g. tol = 0.0001)
      or relative
      (e.g. tol = a * 0.05)

        Hmmm... Reading your post, I wondered if it wouldn't be good to have a module that overloads this operators applying such tolerance... I took a look on CPAN and couldn't found such module.

        Update: Math::BigFloat seems to do someting like that...

        like...

        use FloatingPoint::Helper -tolerance => 0.0001; # Ok, not a good module name... my $a = FloatingPoint::Helper->float(12e-4); my $b = FloatingPoint::Helepr->float(1.2e-3); if ($a > $b) { print "really greater...\n"; } elsif ($a < $b) { print "really lesser...\n"; } else { print "almost equal...\n"; }

        It would be possibly interesting to have different tolerances for different numbers, but I don't know what to do if two numbers with different tolerances are compared, or worse, if you sum them... Maybe using the tolerance of the leftest operand... I Don't know...

        daniel

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://518396]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2018-07-22 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (454 votes). Check out past polls.

    Notices?