Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: float values and operators

by hardburn (Abbot)
on Aug 11, 2004 at 20:12 UTC ( [id://382079]=note: print w/replies, xml ) Need Help??


in reply to Re: float values and operators
in thread float values and operators

That's what I thought, too, but it doesn't appear to be the case:

$ perl -le '$a = 36.8; $b = 36.6; print "true\n" if $a >= ($b + 0.2);' # Prints nothing, just as poster showed $ perl -le '$a = 36.8; $b = 36.6; $c = $b + 0.2; print $c;' 36.8 $ perl -le '$a = 36.8; $b = 36.6; $c = $b + 0.2; print $a;' 36.8 $ perl -le '$a = 36.8; $b = 36.6; $c = $b + 0.2; print $b;' 36.6

"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Replies are listed 'Best First'.
Re^3: float values and operators
by Zaxo (Archbishop) on Aug 11, 2004 at 20:17 UTC

    $ perl -e'print 36.8 - (36.6 + 0.2)' -7.105427357601e-15

    After Compline,
    Zaxo

      Hrm, that's what I get, too. So why doesn't the imprecision show up when you print the variables out?

      "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

        Hrm, that's what I get, too. So why doesn't the imprecision show up when you print the variables out?
        Because print doesn't print the full precision of its args. That's what printf is for:
        $ perl585 -e'print 26.6+0.2, "\n"' 26.8 $ perl585 -e'printf "%.20f\n ", 26.6+0.2' 26.80000000000000071054

        Dave.

        Because the default is prudently to print using DBL_DIG or LDBL_DIG digits which should be one less than the number of digits necessary to distinguish any possible value. If you feel exceptionally daring, you can override this by configuring perl with something like -Accflags=-DOVR_DBL_DIG=16 or -Accflags=-DOVR_LDBL_DIG=19. I wouldn't recommend it, though.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://382079]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-28 18:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found