Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: While loop with addition goes weird (binary float rounding error)

by LanX (Cardinal)
on Apr 08, 2018 at 13:08 UTC ( #1212530=note: print w/replies, xml ) Need Help??


in reply to While loop with addition goes weird

Because 0.01 = 1/(2*2*5*5) is a decimal not a binary fraction.

That means it can't be expressed without rounding error as a binary floating point number.(5 and 2 are different primes), pretty much like how you can't express 1/3 as a finite decimal floating point number without.

see also Humans have too many fingers and What Every Computer Scientist Should Know About Floating-Point Arithmetic

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Wikisyntax for the Monastery

  • Comment on Re: While loop with addition goes weird (binary float rounding error)

Replies are listed 'Best First'.
Re^2: While loop with addition goes weird (binary float rounding error)
by chiel (Initiate) on Apr 08, 2018 at 13:52 UTC
    Thanks for your reply. But to me, your answer is as difficult to understand as my problem ;) Can you explain it more simplistic? Maybe with example on how to do it? I'm just trying to add numbers here.

      Don't be so lazy, follow the two links posted by LanX++.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      We can almost always ignore floating-point errors and round our results with printf. Unfortunately there is no "simplistic" way to recognize the rare cases where this is a bad idea (Or what to do about it if you really have one). The documents already cited are not easy to read! Be assured that they to not require knowledge of any math beyond elementary algabra - only a commitment to learn.
      Bill

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2020-10-22 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (225 votes). Check out past polls.

    Notices?