WTF ?

Andy Armstrong andy at hexten.net
Wed Jan 10 12:57:51 GMT 2007


On 10 Jan 2007, at 12:39, Paul Orrock wrote:
> I've tried this on a couple of machines running perl 5.8.6 and 5.8.8
>
> for ($i=1.2;$i<4;$i+=0.05) {
> print $i . "\n";
> }
>
> Starts off fine, I get
>
> 1.2
> 1.25
> 1.3 etc
>
> Suddenly I get
>
> 3.4
> 3.45
> 3.5
> 3.54999999999999
> 3.59999999999999
> 3.64999999999999
> 3.69999999999999
>
> Can anyone explain why this is ? I get the correct (in my eyes)  
> 1dp / 2dp result with 0.04 and 0.06 but 0.05 ends up to 14dp ?

Are you from the past?

Oh look, 6502 BBC Basic does it too:

 >10 FOR i = 1.2 TO 4 STEP 0.05
 >20 PRINT i
 >30 NEXT
 >RUN
        1.2
       1.25
        1.3
       1.35
        1.4
       1.45
        1.5
       1.55
        1.6
       1.65
        1.7
       1.75
        1.8
       1.85
        1.9
       1.95
          2
       2.05
        2.1
       2.15
        2.2
       2.25
        2.3
       2.35
        2.4
       2.45
        2.5
       2.55
2.59999999
2.64999999
2.69999999
2.74999999
2.79999999
2.84999999
2.89999999
2.94999999
2.99999999
3.04999999
3.09999999
3.14999999
3.19999999
3.24999999
3.29999999
3.34999999
3.39999999
3.44999999
3.49999999
3.54999999
3.59999999
3.64999999
3.69999999
3.74999999
3.79999999
3.84999999
3.89999999
3.94999999
3.99999999

(tested with Acme::6502 :)

It's just rounding innit? Perfectly normal.

-- 
Andy Armstrong, hexten.net



More information about the london.pm mailing list