* Brian Gibson <[EMAIL PROTECTED]> [2004:05:23:15:48:40-0400] scribed: > Can anyone explain why in this simple "for" loop it changes after it gets to > 6.1? I am trying to create a pull down menu for a web page that lists > processor speeds from 1.0 Ghz all the way up to 20.0 Ghz and things get crazy > in between. I am incrementing my counter by .1 each time thru the loop. > > $starting_number = 1.0; > $ending_number = 20.1; > > for ($speed = $starting_number; $speed < $ending_number; $speed = $speed + .1) > { > > print "$speed Ghz\n"; > > } ## End of "for 1.0 Ghz to the total possible speed of the processors....."
As others have offered, perl's math always uses floating point, and this may have undesired results for things that appear as simple as you are doing here. One thing to keep in mind while using the suggested [s]printf is that this may mask undesired truncation. In your case, it probably will not matter; but, consider this: my $starting_number = 1; my $ending_number = 20; my $speed = 10 * $starting_number; while ($speed <= (10 * $ending_number) ) { printf "%4.1f GHz\n", $speed / 10; $speed++; } hth -- Best Regards, mds mds resource 877.596.8237 - Dare to fix things before they break . . . - Our capacity for understanding is inversely proportional to how much we think we know. The more I know, the more I know I don't know . . . --
signature.asc
Description: Digital signature
_______________________________________________ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs