# sprintf rouding error - Page 2

Threaded View
•  Subject
• Author
• Posted on

## Re: sprintf rouding error

Broki@gmx.de wrote:

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

-Joe

## Re: sprintf rouding error

Joe Smith wrote:

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.

## Re: sprintf rouding error

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

## Re: sprintf rouding error

funny.

I never thought of this type of error as an answer to the subset problem,
but the bias seems to be in the rigth direction.

--
The most common of all follies is to believe passionately in the palpably
not true. It is the chief occupation of mankind.
-- H L Mencken, A Mencken Chrestomathy (1949)

## Re: sprintf rouding error

Gerry Ford wrote:

But this is only a user error, not a program error.  So which subset do
you refer to - the subset of programmers who expect floating point numbers
to have infinite precision, or those who don't know the difference between
rounding and truncation?

--
Just because I've written it doesn't mean that
either you or I have to believe it.

## Re: sprintf rouding error

Repair the corrupted file:

perl -p -i -e 'tr/\r//d' bad_file

--
Tad McClellan                          SGML consulting
tadmc@augustmail.com                   Perl programming
Fort Worth, Texas

## Re: sprintf rouding error

Repair the corrupted file:

perl -p -i -e 'tr/\r//d' bad_file

--
Tad McClellan                          SGML consulting
tadmc@augustmail.com                   Perl programming
Fort Worth, Texas