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