Skip Menu |

This queue is for tickets about the Math-Round CPAN distribution.

Report information
The Basics
Id: 50956
Status: open
Priority: 0/
Queue: Math-Round

People
Owner: Nobody in particular
Requestors: daniel.wedul+bitcard [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: 0.06
Fixed in: (no value)



Subject: Incorrect rounding with nearest
Perl version 5.8.0 Math::Round version 0.06 Code: use Math::Round; print nearest(.01, 64.005)."\n"; Output: 64 Expected: 64.01 I tried this also using the string '64.005' and got the same output. Using 63.005 or '63.005' produces the expected result 63.01 though.
On my system I detected the same like reported. Use $Math::Round::half = 0.5000000000003; or local $Math::Round::half = 0.5000000000003; instead of the internal module default of 0.50000000000008; This is not really a bug. It is documented in the module description.
Subject: Re: [rt.cpan.org #50956] Incorrect rounding with nearest
Date: Wed, 16 Jul 2014 06:26:01 +0900
To: bug-Math-Round [...] rt.cpan.org
From: Masanori HATA <hata [...] mihr.net>
Very, very roughly I could fix the problem by doing like blow: ################################################################# my $HALF = 0.50000000000008; # same as original $Math::Round::half sub another_nearest ($$) { my($exp, $num) = @_; if ($num >= 0) { return int($num * 10 ** (-$exp) + $HALF) / 10 ** (-$exp); } else { return POSIX::ceil($num * 10 ** (-$exp) - $HALF) / 10 ** (-$exp); } } say another_nearest(-2, 64.005); ################################################################# But this code will cause another problem. It will cause overflow errors far more easily than original Math::Round::nearest. Generally, I don't recommend above solution. -- Masanori HATA