3:03pm, Aben Siatris wrote: > > : my $number=(0.75-0.54)/0.03; > > : print "$number\n"; > > : for (0..$number) > > : { > > : print "$_ / $number\n"; > > : } > > : > > : my $number=(75-54)/3; > > : print "$number\n"; > > : for (0..$number) > > : { > > : print "$_ / $number\n"; > > : } > > : > > : output: > > : 7 > > : 0 / 7 > > : 1 / 7 > > : 2 / 7 > > : 3 / 7 > > : 4 / 7 > > : 5 / 7 > > : 6 / 7 > > : 7 > > : 0 / 7 > > : 1 / 7 > > : 2 / 7 > > : 3 / 7 > > : 4 / 7 > > : 5 / 7 > > : 6 / 7 > > : 7 / 7 > > at first is 'FOR' from 0..7 and output prints 0..6 > at second is 'FOR' from 0..7 and output prins 0..7 > > it is normal? > The first time through, Perl is working with floating-point numbers, but the second time, it is dealing with integers, so there are no rounding errors.
You can see the difference in this simpler example: # perl -e'$number=(0.75-0.54)/0.03;$number=int($number);printf("%f\n",$number);' 6.000000 # perl -e'$number=(75-54)/3;$number=int($number);printf("%f\n",$number);' 7.000000 To see the root of the problem: # perl -e'$number=(0.75-0.54)/0.03;;printf("%.20g\n",$number);' 6.9999999999999991118 # perl -e'$number=(75-54)/3;;printf("%.20g\n",$number);' 7 The rounding errors caused by using floating point math are causing the difference. In a strongly-typed language, this would be more obvious ('cause you would have had to declare your variable as a float in the first place), but you would still have the same problem. ------------------------------------------------------------- "The lawgiver, of all beings, most owes the law allegiance. He of all men should behave as though the law compelled him. But it is the universal weakness of mankind that what we are given to administer we presently imagine we own." ---------------------- H.G. Wells --------------------------- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>