Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Strange rounding Error in Perl

by kju (Initiate)
on Nov 21, 2004 at 20:30 UTC ( #409416=perlquestion: print w/replies, xml ) Need Help??
kju has asked for the wisdom of the Perl Monks concerning the following question:

Whats 215.88 minus 214.89? For perl the answer is not so obvious:

kju@teriyaki:~$ perl -e 'print 215.88-214.89' 0.990000000000009
Is that a bug?
kju@teriyaki:~$ perl -v This is perl, v5.8.4 built for i386-linux-thread-multi

Replies are listed 'Best First'.
Re: Strange rounding Error in Perl
by Enlil (Parson) on Nov 21, 2004 at 20:37 UTC
Re: Strange rounding Error in Perl
by pg (Canon) on Nov 21, 2004 at 21:52 UTC

    Other than those discussion people had before, if precision is so important, for example, this is a finance, or money related program, use bignum.

    use bignum; print 215.88-214.89;

    This gives you 0.99. But you have to fully understand the performance implication by doing this.

Re: Strange rounding Error in Perl
by davido (Archbishop) on Nov 22, 2004 at 04:38 UTC

    Any programming language that doesn't expose you to this artifact of decimal to binary conversion for floating point numbers isn't telling the truth. The artifact is there, whether you're using BASIC, COBOL, Pascal, Perl, C/C++, etc. Whether or not you see it will depend on if the language hides it by performing behind the scenes rounding and consequential loss of precision.

    See my answer here for a little more info. Trust me, it's not unique to Perl. We learned about it in Computer Science I, in high school in 1984, on the old Franklin Ace 1000 (Apple II similar) computers with FP-Basic.


      I don't think you were right about COBOL, so better remove it from your assertion ;-) COBOL supports fixed decimal, you can define picture like 99.99 etc.

        No, floating point math operations in COBOL are suceptible to the binary to decimal conversion problem too. Defining a picture, or whatever you want to call it is essentially forcing rounding to hide the problem. COBOL allows for fixed decimal numbers, and they're handled differently internally. They're not floating point. For floating point math, the decimal to binary conversion error is still there. You're just not seeing it easily because the language is masking it by rounding, and by not showing all of the available digits, and that is exactly what I meant by saying that languages that don't show this behavior are telling a lie.

        Calculators hide the error too, again, by not showing all digits, and by rounding.


Re: Strange rounding Error in Perl
by TedPride (Priest) on Nov 22, 2004 at 14:50 UTC
    If you don't have to do many operations on your numbers, you can just do something like the following:
    print ((215.88 * 100 - 214.89 * 100) / 100);
    This will return 0.99, which is what you want.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://409416]
Approved by Old_Gray_Bear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2018-06-25 17:36 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (128 votes). Check out past polls.