sprintf rouding error

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

Ben Morrow

Quoth Bart Lateur said:
$x}; print "sum1=$sum1 sum2=$sum2"'

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.

Have you been following this thread at all? The OP was asking why 6274.5
(or some such) rounds to 6274, rather that to 6275 as he was taught in
school. Using int($x + 0.5) for rounding, rather than sprintf %.0f, will
give him what he wants; however, using values like this has
mathematically worrying properties, so it's not generally a good idea.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top