Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Rounding error in program
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";

Report this thread to moderator Post Follow-up to this message
Old Post
Dol
09-18-04 01:58 AM


Re: Rounding error in program
Dol wrote:

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

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org

Report this thread to moderator Post Follow-up to this message
Old Post
Sherm Pendley
09-18-04 01:58 AM


Re: Rounding error in program
On 17 Sep 2004 05:56:05 -0700, dolgoldur@yahoo.com (Dol) wrote:

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

Report this thread to moderator Post Follow-up to this message
Old Post
Helgi Briem
09-18-04 01:58 AM


Re: Rounding error in program
Dol wrote:

> 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

Report this thread to moderator Post Follow-up to this message
Old Post
Mark Clements
09-18-04 01:58 AM


Re: Rounding error in program
Dol wrote:

> 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?"

Report this thread to moderator Post Follow-up to this message
Old Post
Chris Mattern
09-18-04 01:58 AM


Re: Rounding error in program
"Chris Mattern" <matternc@comcast.net> wrote in message
news:R5udnZaoWM7QbNfcRVn-og@comcast.com...
> Dol wrote:
> 
as 
>
> 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



Report this thread to moderator Post Follow-up to this message
Old Post
Paul Lalli
09-18-04 01:58 AM


Re: Rounding error in program
Helgi Briem wrote: 
>
>
> 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.


Report this thread to moderator Post Follow-up to this message
Old Post
Shawn Corey
09-18-04 01:58 AM


Re: Rounding error in program
On Fri, 17 Sep 2004 10:32:00 -0400, Shawn Corey
<shawn.corey@sympatico.ca> wrote:

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

Report this thread to moderator Post Follow-up to this message
Old Post
Helgi Briem
09-18-04 01:58 AM


Re: Rounding error in program
Shawn Corey <shawn.corey@sympatico.ca> wrote in
news:7zC2d.25157$0h7.1764334@news20.bellglobal.com:

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

Helgi Briem <HelgiBriem_1@hotmail.com> wrote in
 news:46olk096ruu12k2spgno4jvjpsgiusvvdo@
4ax.com:
 

Sinan.

Report this thread to moderator Post Follow-up to this message
Old Post
A. Sinan Unur
09-18-04 01:58 AM


Re: Rounding error in program
On Fri, 17 Sep 2004 09:10:43 -0400, Sherm Pendley
<spamtrap@dot-app.org> wrote:

>Dol wrote:
> 
>
>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!

Report this thread to moderator Post Follow-up to this message
Old Post
Helgi Briem
09-18-04 01:58 AM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

PERL Miscellaneous archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:26 AM.

 

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.