B
Bart Lateur
Joe said:No, it's because int($x+0.5) produces bad results when there
are many instances of $x = $n + 0.5 (for integer $n). Observe:
perl -le 'for $x (1.5, 2.5, 3.5, 4.5) {$sum1 += int($x+0.5); $sum2 += $x}; print "sum1=$sum1 sum2=$sum2"'
sum1=14 sum2=12
What the heck are you talking about? int() truncates ("rounds towards
zero"), it doesn't round. The results you get are exact, not rounded
values.
0.5 is not a problem in floating point presentation, as 2 is a power of
2. So there is no error at all in any internal representation of $n+0.5,
for any integer $n (within reasonable limits).
Internally, a floating point value is represented as
+/-Sum(b(i) * 2**-i) * (2**e)
A summation over integer values of i , of a bit value b(i) that is
either 0 or 1, times 2 raised to the power of -i, for each i, the whole
possibly flipped in sign (mantissa), and multiplied by an signed integer
power of 2, e (exponent).
There are typically 53 bits in the mantissa, while e is limited to a
small signed integer.
This is actually a (possibly huge) integer divided by a power of 2.
So there's no problem at all representating, for example, the value of
(123 + 456/1024) as this is the same as (123*1024+456)/1024
And there definitely is not problem for 5.5 which is 11/2.