in reply to Reduce from 2 to 0.1.

It's all about floating point approximation. If you work with floating points, you sometimes get bitten. I was. See Comparison misses a true in *some* cases for example.

Solution: round it off with sprintf.

Hope this helps,

Jeroen
"We are not alone"(FZ)
Update: davorg is right. Stick with integers, or put the sprintf in the for:  $i = sprintf('%.1f', $i - 0.1);

Replies are listed 'Best First'.
Re: Re: Reduce from 2 to 0.1.
by davorg (Chancellor) on Apr 10, 2001 at 19:22 UTC
    Solution: round it off with sprintf.

    Not actually a very good solution as this test shows:

    #!/usr/bin/perl -w use strict; for (my $i = 2 ; $i >= 0.1 ; $i -= 0.1){ printf "%.1f\n", $i; }

    The output is:

    2.0 1.9 1.8 1.7 1.6 1.5 1.4 1.3 1.2 1.1 1.0 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2

    Note that 0.1 is not printed, contrary to what you'd expect if you were reading the condition in the code.

    You'd probably need to adjust the condition to take this into account.

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

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me