in reply to Rounding error?
my $y = 1.5; $y = sprintf "%.0f", $y; my $x = 2.5; $x = sprintf "%.0f", $x; print "$y\n$x\n";
Still surprised? As pg says above, perl's rounding is unbiased - rather than rounding every .5 up, it always rounds to even. 1.5 and 2.5 both become 2.
Update: This is not documented. What is documented in perlfaq4 is this:
it probably pays not to trust whichever system rounding is being used by Perl, but to instead implement the rounding function you need yourself.
I downloaded 5.6.1 from AS, and as ikegami says below it rounds 1.5 to 2 and 2.5 up to 3. Oh, and this all has nothing to do with the original question ;)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Rounding error?
by pg (Canon) on Oct 19, 2004 at 02:35 UTC | |
|
Re^2: Rounding error?
by ikegami (Patriarch) on Oct 19, 2004 at 02:55 UTC | |
by bmann (Priest) on Oct 19, 2004 at 03:08 UTC |