vitoco has asked for the wisdom of the Perl Monks concerning the following question:
If I try this oneliner:
perl -e "$a=-50.123456;for($i=0;$i<20;$i++){print qq!$i $a\n!;$a+=0.01}"I get this:
0 -50.123456 1 -50.113456 2 -50.103456 3 -50.093456 4 -50.083456 5 -50.073456 6 -50.063456 7 -50.053456 8 -50.043456 9 -50.033456 10 -50.023456 11 -50.013456 12 -50.003456 13 -49.993456 14 -49.983456 15 -49.973456 16 -49.963456 17 -49.953456 18 -49.943456 19 -49.933456
But if I try this one:
perl -e "$a=-71.123456;for($i=0;$i<20;$i++){print qq!$i $a\n!;$a+=0.01}"I get this:
0 -71.123456 1 -71.113456 2 -71.103456 3 -71.093456 4 -71.083456 5 -71.073456 6 -71.063456 7 -71.053456 8 -71.043456 9 -71.033456 10 -71.023456 11 -71.0134559999999 12 -71.0034559999999 13 -70.9934559999999 14 -70.9834559999999 15 -70.9734559999999 16 -70.9634559999999 17 -70.9534559999999 18 -70.9434559999999 19 -70.9334559999999
What's going on here?
Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Floating point issue
by pryrt (Abbot) on Aug 12, 2016 at 21:57 UTC | |
by vitoco (Hermit) on Aug 12, 2016 at 22:04 UTC | |
by pryrt (Abbot) on Aug 12, 2016 at 22:09 UTC | |
|
Re: Floating point issue
by Marshall (Canon) on Aug 12, 2016 at 22:26 UTC | |
|
Re: Floating point issue
by Laurent_R (Canon) on Aug 13, 2016 at 07:44 UTC | |
|
Re: Floating point issue
by vitoco (Hermit) on Aug 13, 2016 at 21:53 UTC | |
by Laurent_R (Canon) on Aug 14, 2016 at 08:14 UTC |