in reply to Rounding error?

It all comes down to the inexactitude in the way computers represent floating point values. By printing the values out with as many digits of precision as you can, you'll see that one value rounds down, and the other rounds up.

printf "%.17f\n", 40.88050; 40.88049999999999800 printf "%.17f\n", 41.78050; 41.78050000000000400

The (probable) reason that you see a different result from C, is that you are (probably) using single precision (floats) in your C code, whilst Perl uses double precision. (Probably:)

See Re: Re: Re: Bug? 1+1 != 2 for more information.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
"Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

Replies are listed 'Best First'.
Re^2: Rounding error?
by pg (Canon) on Oct 19, 2004 at 03:27 UTC
    "The (probable) reason that you see a different result from C, is that you are (probably) using single precision (floats) in your C code, whilst Perl uses double precision. (Probably:)"

    This seems to be a good guess:

    #include <stdio.h> #include <stdlib.h> int main(int argc, char * * argv) { { double a = 40.88050; printf("%.3f\n",a); printf("%.17f\n",a); a = 41.78050; printf("%.3f\n",a); printf("%.17f\n",a); } { float a = 40.88050; printf("%.3f\n",a); printf("%.17f\n",a); a = 41.78050; printf("%.3f\n",a); printf("%.17f\n",a); } return 0; }

    Prints:

    40.880 40.88049999999999784 41.781 41.78050000000000352 40.881 40.88050079345703125 41.780 41.78049850463867188