Code Comments
Programming Forum and web based access to our favorite programming groups.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";
Post Follow-up to this messageDol 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
Post Follow-up to this messageOn 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!
Post Follow-up to this messageDol 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
Post Follow-up to this messageDol 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?"
Post Follow-up to this message"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
Post Follow-up to this messageHelgi 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.
Post Follow-up to this messageOn 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!
Post Follow-up to this messageShawn 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.
Post Follow-up to this messageOn 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!
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.