Hello, whilst putting together a reporting page I found that some of my numbers were wrong - I was getting 99% even though it should have been 100%. Prodding around and writing some test scripts I found that it was related to floating point division and sprintf rounding down. This code demonstrates some oddness:
foreach my $int ( 1 .. 600 ) { my $number = $int / $int; next if $number == 1; warn "$int / $int != 1 (== $number)\n"; } produces: 49 / 49 != 1 (== 1) 98 / 98 != 1 (== 1) 103 / 103 != 1 (== 1) 107 / 107 != 1 (== 1) 161 / 161 != 1 (== 1) 187 / 187 != 1 (== 1) 196 / 196 != 1 (== 1) 197 / 197 != 1 (== 1) 206 / 206 != 1 (== 1) 214 / 214 != 1 (== 1) 237 / 237 != 1 (== 1) ...
Now I know that $num / $num != 1 sometimes because of floating point considerations - but I am keen to know more. For instance why is $number always displayed as '1'? Also why do the hits seem to happen more often as the numbers get larger ( twice for 1..100, eleven times for 401..500)?
In reply to Insights sought into floating point division strangeness by EvdB
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |