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,