Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Bug? 1+1 != 2

by helgi (Hermit)
on Jun 19, 2003 at 09:29 UTC ( #267114=note: print w/replies, xml ) Need Help??

in reply to Bug? 1+1 != 2

The problem is pretty simple, as others have stated and is due to the rounding off of floating point numbers.

The real problem is however, that this is a Very Frequently Asked Question and as such, should be answered in the FAQ.

Of course the question in perlfaq4:

perldoc -q Why am I getting long decimals (eg, 19.9499999999999) inste +ad of the numbers I should be getting (eg, 19.95)?
in essence answers this question, but it could be indexed with a different heading, something like:
Why don't my floating point numbers equal each other?

Helgi Briem
helgi DOT briem AT decode DOT is

Replies are listed 'Best First'.
Re: Re: Bug? 1+1 != 2
by shotgunefx (Parson) on Jun 19, 2003 at 10:59 UTC
    I figured it was along those lines as "eq" worked. Any idea on how Perl decides to format the stringified number? I looked at $# but it says it's depreciated so I would think it lies elsewhere.


    "To be civilized is to deny one's nature."
      The internal decision about how Perl "stringifies" floats are quite well described in perlnumber; if you need different conversions, you have to resort to printf and explicit rounding.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (12)
As of 2016-10-21 14:28 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (289 votes). Check out past polls.