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

I have written the following..

for($i=0; $i<=10; $i+=0.1)
{print "$i, \n";}

The output of this is:
0
0.1
0.2
0.3
...
5.9
5.99999999999999
..
8.69999999999999
8.79999999999998
..
9.99999999999998
Could someone explain why this uggly problem occurs?
Thank you for enlighting me and others.

Replies are listed 'Best First'.
Re: Problem with numbers..
by japhy (Canon) on Feb 06, 2006 at 13:47 UTC
    Believe it or not, this is a FAQ. And it's been asked here several times. You can find the answer in perlfaq4; it's the FIRST question in there.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: Problem with numbers..
by ikegami (Patriarch) on Feb 06, 2006 at 13:52 UTC

    The others explained why. The following is the workaround:

    for (my $i10=0; $i10<=100; $i10++) { my $i = $i10 / 10; print "$i, \n"; }

    or the easier to read

    for (0..100) { my $i = $_ / 10; print "$i, \n"; }

    Always use integers for loop counters.

    If you need to compare two decimal numbers, check out Comparing Reals using a Tolerance.

Re: Problem with numbers..
by BrowserUk (Patriarch) on Feb 06, 2006 at 13:47 UTC

    See Re: Re: Re: Bug? 1+1 != 2.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Problem with numbers..
by davorg (Chancellor) on Feb 06, 2006 at 13:50 UTC

    It's a standard problem when dealing with real numbers on a computer. Most real numbers can't be represented accurately using the binary representations used in computers. You'll therefore just get an approximation (as your example shows).

    There are two options for dealing with it. Either stick with real numbers and make sure that everything is rounded to the correct number of decimal places before displaying it to the user or use integer arithmatic whenever possible.

    See also perlfaq4.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Problem with numbers..
by graff (Chancellor) on Feb 07, 2006 at 01:48 UTC
    Another good "workaround" is to use "printf" instead of "print":
    for ( $i=0; $i<=10; $i+=0.1 ) { printf( "%3.1f\n", $i ); }
    That "does the right thing": it prints the results with the appropriate amount of precision.
Re: Problem with numbers..
by spiritway (Vicar) on Feb 07, 2006 at 01:34 UTC

    To expand on what others have said, a computer can only exactly represent numbers with an integer powers of 2 in the denominator (1/2, 1/4, 1/8, etc.). Any other denominator will result in a representation that continues indefinitely. The representation is either truncated or rounded, but in any event it is inexact. For example, 0.1 (decimal) is represented as 0.0001100110010011001... in binary. Much of the time, the tiny error is insignificant. However, these errors accumulate when you use repeated calculations, and can eventually cause complete loss of precision.