Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Simple math gone wrong

by sdyates (Scribe)
on Sep 04, 2003 at 12:19 UTC ( #288841=perlquestion: print w/replies, xml ) Need Help??

sdyates has asked for the wisdom of the Perl Monks concerning the following question:

I try something simple like:

print 1062676995.614963 - 1062676995.594934;

And I get: 0.0200290679931641!!! Why

I should get: 0.020029 - why do I get data going back multiple decimal places? I have never seen this before. Should I be using certain command to do math. I tried going to 2 decimal places and still have the problem.

I got the variables from Time::HiRes but I dont see how this is the problem. Please help Monks, sdyates

Replies are listed 'Best First'.
Re: Simple math gone wrong
by tachyon (Chancellor) on Sep 04, 2003 at 12:24 UTC

    It all comes back to how the computer actually stores and processes numbers. Floating point numbers are by nature inexact. If you need exact you need to use integer math and then correct for decimal places. See Bug? 1+1 != 2, Filthy Floats or the IEEE stuff here including What Every Computer Scientist Should Know about Floating-Point Arithmetic

    If you don't really care to much just use printf/sprintf

    printf '%.6f', 1062676995.614963 - 1062676995.594934 __DATA__ 0.020029




Re: Simple math gone wrong
by Abigail-II (Bishop) on Sep 04, 2003 at 12:26 UTC
    $ perldoc -q decimal Found in /opt/perl/lib/5.8.0/pod/perlfaq4.pod Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)? The infinite set that a mathematician thinks of as the real numbers can only be approximated on a computer, since the computer only has a finite number of bits to store an infinite number of, um, numbers. Internally, your computer represents floating- point numbers in binary. Floating-point numbers read in from a file or appearing as literals in your program are converted from their decimal floating-point representation (eg, 19.95) to an internal binary representation. However, 19.95 can't be precisely represented as a binary floating-point number, just like 1/3 can't be exactly represented as a decimal floating-point number. The computer's binary representation of 19.95, therefore, isn't exactly 19.95. When a floating-point number gets printed, the binary floating-point representation is converted back to decimal. These decimal numbers are dis- played in either the format you specify with printf(), or the current output format for num- bers. (See "$#" in perlvar if you use print. $# has a different default value in Perl5 than it did in Perl4. Changing $# yourself is deprecated.) This affects all computer languages that represent decimal floating-point numbers in binary, not just Perl. Perl provides arbitrary-precision decimal numbers with the Math::BigFloat module (part of the standard Perl distribution), but mathematical operations are consequently slower. If precision is important, such as when dealing with money, it's good to work with integers and then divide at the last possible moment. For example, work in pennies (1995) instead of dollars and cents (19.95) and divide by 100 at the end. To get rid of the superfluous digits, just use a format (eg, "printf("%.2f", 19.95)") to get the required precision. See "Floating-point Arith- metic" in perlop.


Re: Simple math gone wrong
by gjb (Vicar) on Sep 04, 2003 at 12:25 UTC

    It's due to the internal representation of numbers. Have a look at this (brilliant) node, it does a much better job at explaining the issue than I have the patience for ;-)

    Hope this helps, -gjb-

Re: Simple math gone wrong
by Taulmarill (Deacon) on Sep 04, 2003 at 12:34 UTC
    maybe you want to use one of the Math Modules (look for them at CPAN).

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://288841]
Approved by gjb
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2023-05-28 01:12 GMT
Find Nodes?
    Voting Booth?

    No recent polls found