At 12:48 AM 3/17/2007 -0500, [EMAIL PROTECTED] wrote:
>Incidentally, perl is a typeless language, compounding the problem even
more. That is, perl
>converts a variable or literal to an integer, a floating point double, or
text string on-the-fly as
>needed. A single line of perl code may hide seve
At 08:04 PM 3/16/2007 -0400, Su, Yu \(Eugene\) wrote:
>I expect
>print sprintf("%.0f %.0f %.0f %.0f %.0f %.0f %.0f %.0f %.0f %.0f", 0.5,
1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5);
>will give me
>1 2 3 4 5 6 7 8 9 10
>but instead I get
>0 2 2 4 4 6 6 8 8 10
>How to round a floating-point value
Casteele/ShadowLord wrote:
>
> Actually, Bill's answer has an error.. Adding .05 just introduces a subtle
> error because some
> decimal floating point numbers cannot be exactly represented in binary form.
> For example:
>
> my ($i, $j);
> foreach $i (0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1
Actually, Bill's answer has an error.. Adding .05 just introduces a subtle
error because some
decimal floating point numbers cannot be exactly represented in binary form.
For example:
my ($i, $j);
foreach $i (0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5,
$Bill wrote:
> Su, Yu (Eugene) wrote:
>
>> I expect
>> print sprintf("%.0f %.0f %.0f %.0f %.0f %.0f %.0f %.0f %.0f %.0f", 0.5,
1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5);
>> will give me
>> 1 2 3 4 5 6 7 8 9 10
>> but instead I get
>> 0 2 2 4 4 6 6 8 8 10
>>
>> How to
On Fri, 16 Mar 2007, Jan Dubois wrote:
> On Fri, 16 Mar 2007, Su, Yu (Eugene) wrote:
> > Interesting. Any explanation why sprintf treats 3.5 and 4.5
> > differently in rounding?
>
> It is called "unbiased rounding", which has the advantage of not
> adding an upward trend to your numbers:
>
> ht
On Fri, 16 Mar 2007, Su, Yu (Eugene) wrote:
> Interesting. Any explanation why sprintf treats 3.5 and 4.5
> differently in rounding?
It is called "unbiased rounding", which has the advantage of not
adding an upward trend to your numbers:
http://en.wikipedia.org/wiki/Rounding
Note that Bill's
Su, Yu (Eugene) wrote:
> Interesting. Any explanation why sprintf treats 3.5 and 4.5 differently in
> rounding?
Try adding this to the loop:
printf "%.20f", $_;
___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To uns
Interesting. Any explanation why sprintf treats 3.5 and 4.5 differently in
rounding?
Thanks Bill.
-Eugene
-Original Message-
From: Bill Luebkert [mailto:[EMAIL PROTECTED]
Sent: Friday, March 16, 2007 5:33 PM
To: Su, Yu (Eugene)
Cc: perl-win32-users@listserv.ActiveState.com
Subject: Re:
Su, Yu (Eugene) wrote:
> I expect
> print sprintf("%.0f %.0f %.0f %.0f %.0f %.0f %.0f %.0f %.0f %.0f", 0.5, 1.5,
> 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5);
> will give me
> 1 2 3 4 5 6 7 8 9 10
> but instead I get
> 0 2 2 4 4 6 6 8 8 10
>
> How to round a floating-point value to an integer val
I expect
print sprintf("%.0f %.0f %.0f %.0f %.0f %.0f %.0f %.0f %.0f %.0f", 0.5, 1.5,
2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5);
will give me
1 2 3 4 5 6 7 8 9 10
but instead I get
0 2 2 4 4 6 6 8 8 10
How to round a floating-point value to an integer value using (.5) up rule?
Thanks.
-Eugene
11 matches
Mail list logo