[sf-perl] Question on Perl math

Randal L. Schwartz merlyn at stonehenge.com
Fri Apr 13 17:10:03 PDT 2007


>>>>> "Dajen" == Dajen Huang <Dajen.Huang at Sun.COM> writes:

Dajen> Hello,
Dajen> Does anyone know why Perl put out .00000000001 on the 30th time of the
Dajen> following calculation, then back to normal on the 82th time?

Dajen> I use Perl for microprocessor design and that +/-0.000000001 really
Dajen> kills me. I assume numbers are converted to full 64 bits floating point
Dajen> then perform the calculation. But still doesn't make sense to me for
Dajen> this to happen. They are small numbers so no danger of overflow. Plus I
Dajen> am not doing '1 divided by 3' type of thing. It's a pure add.

But "0.1" is a "1 divided by 3 thing".  See the FAQ:

Found in /usr/libdata/perl5/pod/perlfaq4.pod
  Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)?
    Internally, your computer represents floating-point numbers in binary.
    Digital (as in powers of two) computers cannot store all numbers
    exactly. Some real numbers lose precision in the process. This is a
    problem with how computers store numbers and affects all computer
    languages, not just Perl.

    perlnumber show the gory details of number representations and
    conversions.

    To limit the number of decimal places in your numbers, you can use the
    printf or sprintf function. See the "Floating Point Arithmetic" for more
    details.

            printf "%.2f", 10/3;

            my $number = sprintf "%.2f", 10/3;


-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn at stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


More information about the SanFrancisco-pm mailing list