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

2007-03-20 Thread Ted Schuerzinger
[EMAIL PROTECTED] graced perl with these words of wisdom: > Srt f lk wht wer dng 2 Nglsh >. *We*? Speak for yourself. I find it intensely irritating to read posts on an email list like this where people write "u" and "ur" when they're not talking about Burmese honorifics or ancient Mesopotam

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

2007-03-20 Thread Thomas, Mark - BLS CTR
> Remember when one state (Kansas, Nebraska, or Arkansas, I think it was) > wanted to declare pi = 3, which would've made all the wheels in the > state turn square overnight? This isn't quite true, but the truth is equally weird. It was a bill with a method to square a circle, which is to find a s

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

2007-03-20 Thread Deane . Rothenmaier
>But at some point it has to happen, whether u do it urself >in some code or the arithmetic engine does it on its floats. Understood. But the difference is that computers round beyond where we humans generally care about. Not that it's actually rounding--as you pointed out in another post--but

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

2007-03-19 Thread Chris Wagner
At 08:39 AM 3/19/2007 -0500, [EMAIL PROTECTED] wrote: >I have a recollection from a computer math class many years ago that you >should never, ever, round intermediate numbers in a calculation. The >"rounding" itself then becomes part of the next step, then the next, and >so on. While that may n

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

2007-03-19 Thread Chris Wagner
At 04:34 AM 3/18/2007 +, [EMAIL PROTECTED] wrote: >Do 'human rounding principles' say that you would expect 10 / 3 rounded >to 2 DP would produce 3.34 or 3.33? How about -10 / 3? 10/3 rounded to two places is 3.33. 3.34 is simply wrong. To say that 3.34 is 10/3 rouned to two places means tha

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

2007-03-19 Thread Casteele/ShadowLord
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 16 Mar 2007 at 21:52, Bill Luebkert wrote: > That can be resolved by using a smaller value than .05. Actually, I > use something like this to do my rounding when CPU isn't important : > > sub round ($;$) {# $ret = round ($num, [$digi

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

2007-03-19 Thread Deane . Rothenmaier
>From wagnerc: >That's all good for rounding intermediate numbers in a calculation... I have a recollection from a computer math class many years ago that you should never, ever, round intermediate numbers in a calculation. The "rounding" itself then becomes part of the next step, then the next

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

2007-03-17 Thread Chris Wagner
I decided to write my own rounding algorithm that takes it out of Perl's hands and does all the math itself using strings. Since it uses strings it operates on the same literal value that a human would be looking at rather than an opaque object. Floats are better thought of as opaque objects than

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

2007-03-17 Thread Mark Dootson
Chris Wagner wrote: > OP was trying to round for display. In this case whatever rounding function > u use has to output a string consistent with human rounding principles. > Floating point numbers that a machine uses internally have no real relation > to the real numbers that humans use. And the

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

2007-03-17 Thread Chris Wagner
At 05:32 PM 3/17/2007 +, Mark Dootson wrote: >If you are using floating point arithmetic, you want unbiased rounding, >'even' if you don't realise that this is good for you. That's all good for rounding intermediate numbers in a calculation but the OP was trying to round for display. In this

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

2007-03-17 Thread Mark Dootson
I'd disagree that there is any problem with sprintf and 'rounding'. Basically, you can round towards zero, away from zero, or use unbiased rounding. Towards even is the accepted method of unbiased rounding. Unbiased rounding is the method least likely to introduce bias into your end results. If y

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

2007-03-17 Thread John Deighan
I think the safest way to round is to add 0.5, then use the POSIX::floor function. The Perl docs warn against using int() for just about anything (I don't have the exact quote handy): use strict; use POSIX qw(floor); MAIN: { foreach my $x ( 0.0, 0.1, 0.2, 0.3, 0.4, 0.5,

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

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
planation 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@lists

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

2007-03-16 Thread Suresh Govindachar
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? > > I'd add .5 in the next place down : > > foreach (0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0) { > pr

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

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)
: how to round a floating-point value to an integer value 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

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

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