Re: sprintf rouding error
- From: Bart Lateur <bart.lateur@xxxxxxxxxx>
- Date: Sat, 26 Jan 2008 10:34:06 GMT
Joe Smith wrote:
Broki@xxxxxx wrote:
What about the IEEE rounding and Perl...is it a fact that the
"uncorrectness" with floating point conversion leads to the
statistically interesting way to round 50% up/ 50% down?
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.
--
Bart.
.
- Follow-Ups:
- Re: sprintf rouding error
- From: Ben Morrow
- Re: sprintf rouding error
- References:
- Re: sprintf rouding error
- From: Broki
- Re: sprintf rouding error
- From: Peter J. Holzer
- Re: sprintf rouding error
- From: Alex
- Re: sprintf rouding error
- From: Peter Makholm
- Re: sprintf rouding error
- From: Alex
- Re: sprintf rouding error
- From: Joost Diepenmaat
- Re: sprintf rouding error
- From: Alex
- Re: sprintf rouding error
- From: Joost Diepenmaat
- Re: sprintf rouding error
- From: Broki
- Re: sprintf rouding error
- From: Joe Smith
- Re: sprintf rouding error
- Prev by Date: Re: sprintf rouding error
- Next by Date: Re: sprintf rouding error
- Previous by thread: Re: sprintf rouding error
- Next by thread: Re: sprintf rouding error
- Index(es):
Relevant Pages
|