RE: how to round a floating-point value to an integer value

2007-03-16 Thread Chris Wagner
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

RE: how to round a floating-point value to an integer value

2007-03-16 Thread Chris Wagner
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

Re: how to round a floating-point value to an integer value

2007-03-16 Thread Bill Luebkert
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

RE: how to round a floating-point value to an integer value

2007-03-16 Thread Casteele/ShadowLord
-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,

RE: how to round a floating-point value to an integer value

2007-03-16 Thread Suresh Govindachar
$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

RE: how to round a floating-point value to an integer value

2007-03-16 Thread Jan Dubois
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

RE: how to round a floating-point value to an integer value

2007-03-16 Thread Jan Dubois
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

Re: how to round a floating-point value to an integer value

2007-03-16 Thread Bill Luebkert
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

RE: how to round a floating-point value to an integer value

2007-03-16 Thread Su, Yu (Eugene)
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:

Re: how to round a floating-point value to an integer value

2007-03-16 Thread Bill Luebkert
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

RE: how to round a floating-point value to an integer value

2007-03-16 Thread Su, Yu (Eugene)
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