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

I'm trying to understand (for my own sanity - and others I work with) why the following snippet does what it does:

my $x = 8.78; my $y = $x * 100; my $z = $x * 1000; printf "%08d\n", $y; # prints 00000877 - wrong printf "%08d\n", "$y"; # prints 00000878 - right printf "%3.5f\n", $y; # prints 878.00000 - right printf "%08d\n", $z; # prints 00008780 - right

I can understand that since $y is a floating-point number it might be stored in memory as 877.99999999999... But, if so, why doesn't the %3.5f example print 877.99999? And why does multiplying by 1000 make a difference for the %08d version?

I've tested this code on version 5.005_2 on aix, 5.8.6 on win32, and 5.8.4 on linux. Also, I know that the simple "fix" is to convert it to a string first (as in the 2nd printf). 8.78 seems to be one of the magic numbers that triggers this phenomenon. Most do not. I'm sure that there are others... I'd just like to better understand what's happening.

TIA

Replies are listed 'Best First'.
Re: [s]printf %d oddity
by ikegami (Patriarch) on May 19, 2005 at 22:06 UTC
    why doesn't the %3.5f example print 877.99999?

    Because %f rounds. (%d truncates.)

    >perl -e "printf '%.16f', 8.78" 8.7799999999999994 >perl -e "printf '%.5f', 8.78" 8.78000 >perl -e "printf '%d', 4.7" 4 >perl -e "printf '%.0f', 4.7" 5 >perl -e "printf '%d', -4.7" -4 >perl -e "printf '%.0f', -4.7" -5
    And why does multiplying by 1000 make a difference for the %08d version?

    If I were to guess, the floating point processor is forced to discard low-precision bits, and it rounds the result when it does so.

Re: [s]printf %d oddity
by Joost (Canon) on May 20, 2005 at 08:46 UTC
Re: [s]printf %d oddity
by chas (Priest) on May 19, 2005 at 22:15 UTC
    Curious indeed! 8.78 does seem to behave peculiarly in this respect. For example:
    if(8.78*100==878){print "OK for 8.78\n"} #prints nothing if(7.78*100==778){print "OK for 7.78\n"} #prints "OK for 7.78"
    etc
    chas
      I'd love to see an explanation for this. It's got me doubting Perl as a language for doing even simple calculations. I can't see how this can be put down to rounding errors between binary and decimal
        This is *just* what happens when you convert between binary and decimal - see the links I provided in the post below.

        update:
        Think of it like this: decimal floats are inaccurate too, since they can't represent a rational number like 1/3 in finite notation:

        1/3 = 0.333333333333333333....

        If all arithmatic was done using decimal notation, assuming a maximum of 10 places you get:

        $x = 1/3; # 0.3333333333 $y = $x * 3; $y == 1 -> false # $y == 0.9999999999

        The same thing happens here, only since humans have a hard time thinking in floating binary, it's much harder to predict where.