Rounding error in program

D

Dol

Hi all,

When I execute the following program, $d is set to 18 instead of 19 as
I would expect. Any ideas why this happens?

Thanks,
Dol

#!/yourpathtoperl/perl

$a = 50;
$b = 0.37;

$c = $a*$b
$d = sprintf("%.0f",$c);

print "$c\t$d\n";
 
S

Sherm Pendley

Dol said:
When I execute the following program, $d is set to 18 instead of 19 as
I would expect. Any ideas why this happens?

Have a look in 'perldoc -q trig' for a discussion of rounding errors and
why this happens.

sherm--
 
H

Helgi Briem

When I execute the following program, $d is set to 18 instead of 19 as
I would expect.

Why would you expect that? It is the wrong expectation.
Any ideas why this happens?

Yes, because 18.5 should be rounded to 18, not 19.

Always rounding X.5 *up* as you probably learned in grade school
introduces a systematic bias. This was changed by the International
Standards Organisation in 1992, to rounding to the nearest even
integer.
#!/yourpathtoperl/perl

$a = 50;
$b = 0.37;

$c = $a*$b
$d = sprintf("%.0f",$c);

print "$c\t$d\n";

for (1..30)
{
$c = $_+0.5;
$d = sprintf("%.0f",$c);

print "$c\t$d\n";
}

--
Helgi Briem hbriem AT simnet DOT is

Never worry about anything that you see on the news.
To get on the news it must be sufficiently rare
that your chances of being involved are negligible!
 
M

Mark Clements

Dol said:
When I execute the following program, $d is set to 18 instead of 19 as
I would expect. Any ideas why this happens?
$a = 50;
$b = 0.37;

$c = $a*$b
$d = sprintf("%.0f",$c);

print "$c\t$d\n";

from

perldoc -f sprintf

You can specify a precision (for numeric
conversions) or a maximum width (for string
conversions) by specifying a "." followed by a
number. For floating point formats, this
specifies the number of decimal places to show
(the default being 6), eg:

so you've specified zero decimal places and got zero decimal places.
*but* (breaking the problem down further)

redwood 23780 $ perl -le '$a=shift;$f=shift;printf "%.${f}f\n",$a' .5 0
0
redwood 23781 $ perl -le '$a=shift;$f=shift;printf "%.${f}f\n",$a' .6 0
1

which, while being intuitive, seems to contradict the documentation. I'm
obviously missing something.

Mark


Mark
 
C

Chris Mattern

Dol said:
Hi all,

When I execute the following program, $d is set to 18 instead of 19 as
I would expect. Any ideas why this happens?

Your error was in expecting to get rounding. Using sprintf like that
doesn't result in the your number being rounded; the fractional part
is simply truncated. If you want rounding, add 0.5 to the number
first before sending it to sprintf.
Thanks,
Dol

#!/yourpathtoperl/perl

$a = 50;
$b = 0.37;

$c = $a*$b
$d = sprintf("%.0f",$c);

print "$c\t$d\n";

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
S

Shawn Corey

Helgi said:
for (1..30)
{
$c = $_+0.5;
$d = sprintf("%.0f",$c);

print "$c\t$d\n";
}

My output is:
1.5 2
2.5 2
3.5 4
4.5 4
5.5 6
6.5 6
7.5 8
8.5 8
9.5 10
10.5 10
....

Why?

--- Shawn
--
perl -v

This is perl, v5.8.0 built for darwin

Copyright 1987-2002, Larry Wall

Perl may be copied only under the terms of either the Artistic License
or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl' or `perldoc perl'. If you have access to the
Internet, point your browser at http://www.perl.com/, the Perl Home Page.
 
P

Paul Lalli

Chris Mattern said:
Your error was in expecting to get rounding. Using sprintf like that
doesn't result in the your number being rounded; the fractional part
is simply truncated. If you want rounding, add 0.5 to the number
first before sending it to sprintf.

Please check your assumption before posting a reply to a question like
this. You are regrettably wholly incorrect.

printf "%.0f\n", 3.8;

outputs "4".

Paul Lalli
 
H

Helgi Briem

My output is:
1.5 2
2.5 2
3.5 4
4.5 4
5.5 6
6.5 6
7.5 8
8.5 8
9.5 10
10.5 10
...

Why?

????

I just told you why. Didn't you read my post?

perldoc -q trig

a.k.a. Does Perl have a round() function? What about ceil() and
floor()? Trig functions?

--
Helgi Briem hbriem AT simnet DOT is

Never worry about anything that you see on the news.
To get on the news it must be sufficiently rare
that your chances of being involved are negligible!
 
A

A. Sinan Unur

My output is:
1.5 2
2.5 2
3.5 4
4.5 4
5.5 6
6.5 6
7.5 8
8.5 8
9.5 10
10.5 10
...

Why?

--- Shawn

You snipped the relevant part of Helgi's post. Reading what you are
responding to helps:


Sinan.
 
H

Helgi Briem

Have a look in 'perldoc -q trig' for a discussion of rounding errors and
why this happens.

It's not a rounding "error". It is correct rounding.

--
Helgi Briem hbriem AT simnet DOT is

Never worry about anything that you see on the news.
To get on the news it must be sufficiently rare
that your chances of being involved are negligible!
 
J

Jeff 'japhy' Pinyan

You snipped the relevant part of Helgi's post. Reading what you are
responding to helps:

Does anyone know the ISO's reasoning behind it? It seems to create a new
bias, which doesn't seem to make sense to me, at least.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
Senior Dean, Fall 2004 % have long ago been overpaid?
RPI Corporation Secretary %
http://japhy.perlmonk.org/ % -- Meister Eckhart
 
C

ctcgag

Jeff 'japhy' Pinyan said:
Does anyone know the ISO's reasoning behind it?

So that you round up just about as often as you round down, so they
cancel each other out--rather than rounding up more often than down.
It seems to create a new
bias,

Normally, no one cares what the even/odd distribution of the least
siginificant digit of a column of numbers are, so the bias it introduces is
harmless.
which doesn't seem to make sense to me, at least.

None of it makes sense to me. If you are going to be summing a column
of numbers, don't round them first.

Xho
 

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

Forum statistics

Threads
474,160
Messages
2,570,890
Members
47,423
Latest member
henerygril

Latest Threads

Top