Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: floating point addition

by choroba (Cardinal)
on Jan 12, 2015 at 16:14 UTC ( [id://1112958]=note: print w/replies, xml ) Need Help??


in reply to floating point addition

What Every Computer Scientist Should Know About Floating-Point Arithmetic.

In Perl, if speed is not a concern, you can just add -Mbignum.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: floating point addition
by morgon (Priest) on Jan 12, 2015 at 17:59 UTC
    I think bignum just gives you more precision but does not eliminate the problem.

    To eliminate the problem you could use bigrat and use fractions.

    Compare:

    perl -Mbignum -e ' printf "%20.17f\n", 20.1;' 20.10000000000000142 perl -Mbigrat -e ' printf "%20.17f\n", 20.1;' 20.10000000000000142 perl -Mbigrat -e ' printf "%20.17f\n", 20+1/10; 20.00000000000000000
      You are right about bignum. Your bigrat example, though, doesn't work. One needs
      perl -Mbigrat -le 'print 20/1 + 1/10'
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        perl -Mbigrat -e ' printf "%20.17f\n", 20/1+1/10;' 20.10000000000000142 perl -Mbigrat -e ' printf "%20.17f\n", 201/10;' 20.10000000000000142
        Does not seem to work either - I don't know why not...
Re^2: floating point addition
by kresike (Initiate) on Jan 12, 2015 at 16:49 UTC
    That I've stumbled upon myself, and it seems like an interesting article, however I'm a bit reluctant to read 67 pages of that just to reach the same conclusion I already know, avoid computers touching floating point numbers ... A bit naive of me to think that 20.1 written like that means the same for a computer as it does to me.

      I think I detect a note of intended irony in that last sentence, but basically that's what you get when you try to do base 10 math in a base 2 machine. The sooner you become of aware of it, the sooner you can deal with it.

      This part probably sounds like one of those tirades from old farts that begin "back in my day.." .. but .. back in my day, one of the required courses was "Programming for Numerical Methods" or some such title, and we spent the class trying to predict how poorly our computations would proceed based on understanding the limits of the algorithms. There were two results to each computation, the result and the error estimate, and the second result was more important because it told you if could take the first one seriously or not. I took the class in the 80's and our textbook dated back to the 60's. This has become less important over time because improvements in computational speed and memory allow us to use floating point numbers of sufficiently high resolution that we avoid a lot of these problems .. but here we are in 2015 and 1/10th is still giving us fits.

      Dum Spiro Spero

        I wouldn't say that its very old fart-ish. I finished uni relatively recently (about 4 years ago) and floating point errors (and the inherent inaccuracies with storing base 10 values in base 2 format) were certainly covered in our courses. Indeed, I would expect this to be basic knowledge covered in any good Software Dev / Programming course.

      As that is an incorrect conclusion I suggest you do yourself a favour and invest your valuable time in reading and understanding those 67 pages.

      Jenda
      Enoch was right!
      Enjoy the last years of Rome.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-24 00:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found