Subject: | Overloaded bcmp incorrect |
Date: | Thu, 30 Jun 2016 16:51:23 +0100 |
To: | "bug-Math-Currency [...] rt.cpan.org" <bug-Math-Currency [...] rt.cpan.org> |
From: | Daniel Beardsmore <daniel [...] trustnetworks.co.uk> |
Good afternoon
Please see the following tests:
trust-database:~ $ perl
use Math::Currency;
use feature qw/say/;
my $small = new Math::Currency(10);
my $large = new Math::Currency(100);
say "$small $large";
say "small vs large = " . $small->bcmp($large);
say "$small $large";
say "large vs small = " . $large->bcmp($small);
say "$small $large";
#10.00 #100.00
small vs large = 1
#10.00 #100.00
large vs small = 1
#10.00 #100.00
trust-database:~ $ perl
use Math::BigFloat;
use feature qw/say/;
my $small = new Math::BigFloat(10);
my $large = new Math::BigFloat(100);
say "$small $large";
say "small vs large = " . $small->bcmp($large);
say "$small $large";
say "large vs small = " . $large->bcmp($small);
say "$small $large";
10 100
small vs large = -1
10 100
large vs small = 1
10 100
Notice that Math::Currency returns 1 for both large <=> small and small <=> large.
The problem is thus:
Math::BigFloat defines bcmp as follows:
$x->bcmp($y); # compare numbers (undef, < 0, == 0, > 0)
Your undocumented overridden function expects the following for older Math::BigInt versions:
Math::Currency->bcmp($x, $y)
I can see that either 0.48 resolved a compatibility issue with newer versions of BigInt, but you're only applying it to versions at least 1.999717. I am using Debian 8.4 with Perl 5.20.2 and this has BigInt 1.9993; the revised behaviour was certainly required for this version of BigInt. In fact, I don't even know what your original implementation of bcmp was intended to achieve: my PC has Perl 5.8.9 installed and bcmp in Math::BigInt even then was only expecting one parameter.
I've worked around this by resetting _LEGACY_MATH_BIGINT to 0.