Re: sprintf rouding error



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.
.



Relevant Pages

  • Re: How do you keep track of what all the numbers mean?
    ... that if you develop a floating point representation of a system, ... but I would suspect that scaling constants is a main part of it. ... There will be an assumed scaling between the floating point ... will depend upon the input signal power. ...
    (comp.dsp)
  • Re: Double data Type Problem
    ... Great tutorial on IEEE representation. ... of floating point numbers. ... There is just one caution to using the Round function... ... calculation using something known as Banker's Rounding... ...
    (microsoft.public.vb.general.discussion)
  • Re: is int(round(val)) safe?
    ... > round to the nearest integer by using int)? ... Since round() returns an integer, ... here) floating point values can handle *integer* values ... The problem* with floating point is inaccurate representation ...
    (comp.lang.python)
  • Re: is int(round(val)) safe?
    ... >> I suspect it is fine, but wanted to be sure that weird floating point ... >> representation on some platform might make it unsafe there (i.e. get the ... >Since round() returns an integer, ... doesn't matter that it's to the right of the new LSB of 2.0**53 ). ...
    (comp.lang.python)
  • Re: Rounding numbers
    ... Divide by that power of 10 factor ... I believe this has to do with the fact that the floating point representation for 12.565 is ever so slightly less than that value, so the Int function will operate on a number smaller than 0.5+1256.5 thus producing the incorrect result. ...
    (comp.lang.basic.visual.misc)