Subject: | [PATCH] Math::BigFloat->new considers any reference a BigInt. |
If you pass in a reference to Math::BigFloat->new() it assumes its a
BigInt object. This assumption breaks overloaded objects.
$bigfloat = Math::BigFloat->new($overloaded_number);
It thinks its a BigInt and tries to call as_number() on it.
The attached patch tests for and fixes this.
Subject: | new_overloaded.patch |
Auto-merging (0, 27642) /local/Math-BigInt to /vendor/Math-BigInt (base /vendor/Math-BigInt:27640).
A t/new_overloaded.t
U lib/Math/BigFloat.pm
==== Patch <-> level 1
Source: 9c88509d-e914-0410-b01c-b9530614cbfe:/local/Math-BigInt:27642
Target: 9c88509d-e914-0410-b01c-b9530614cbfe:/vendor/Math-BigInt:27640
Log:
r27641@windhund: schwern | 2007-02-25 12:16:14 -0800
Local copy
r27642@windhund: schwern | 2007-02-25 12:30:13 -0800
Math::BigFloat->new would consider any reference to be a BigInt. This
broke overloaded non-BigInts.
=== t/new_overloaded.t
==================================================================
--- t/new_overloaded.t (revision 27640)
+++ t/new_overloaded.t (patch - level 1)
@@ -0,0 +1,32 @@
+#!/usr/bin/perl -w
+
+# Math::BigFloat->new had a bug where it would assume any object is a
+# BigInt which broke overloaded non-BigInts.
+
+use Test::More tests => 4;
+
+
+package Overloaded::Num;
+
+use overload '0+' => sub { ${$_[0]} },
+ fallback => 1;
+sub new {
+ my($class, $num) = @_;
+ return bless \$num, $class;
+}
+
+
+package main;
+
+use Math::BigFloat;
+
+my $overloaded_num = Overloaded::Num->new(2.23);
+is $overloaded_num, 2.23;
+
+my $bigfloat = Math::BigFloat->new($overloaded_num);
+is $bigfloat, 2.23, 'BigFloat->new accepts overloaded numbers';
+
+my $bigint = Math::BigInt->new(Overloaded::Num->new(3));
+is $bigint, 3, 'BigInt->new accepts overloaded numbers';
+
+is( Math::BigFloat->new($bigint), 3, 'BigFloat from BigInt' );
=== lib/Math/BigFloat.pm
==================================================================
--- lib/Math/BigFloat.pm (revision 27640)
+++ lib/Math/BigFloat.pm (patch - level 1)
@@ -127,7 +127,7 @@
my $self = {}; bless $self, $class;
# shortcut for bigints and its subclasses
- if ((ref($wanted)) && (ref($wanted) ne $class))
+ if ((ref($wanted)) && eval { $wanted->isa("Math::BigInt") })
{
$self->{_m} = $wanted->as_number()->{value}; # get us a bigint copy
$self->{_e} = $MBI->_zero();
==== BEGIN SVK PATCH BLOCK ====
Version: svk v2.0.0 (darwin)
eJyNVN9v21QULhLiwbzxS0IMcdk82oq6ude/GjssC6hN2q7tKtGNhw1Z19fHjZljB/9oqOpOJE5W
xAMvSLzuiRf+Gf6U/QscO20ZotUqJcqN7znfOd/x951uctDqsKLdpoXMaPHt4we2vc8z0b/L9EI2
C/CCLE5kowjhCEJZK8L4UNaLiA8Ab9M4T0R1yHhyCFl1CMQzyNpthnDNOdxGDXEBW6O6PIujVLZq
eCdLAGRWmJ21oqNVH0dmVpEC3tSwTgJHQRrEEbahrpm6iiEYzzA/HkLkJHGcXV6pVTYtRBin4FTw
FaRRxasyUqoTvCABgT0d49OsRrrIrgP1KwKReOD+J7Ru0zgv9Doiwx46ZgWkyc2Ce57jByFU48ga
EYyc+AiSMOYeeKtVcdWR1UJh1+Opc7z6lg+H4bGTwU+ZB2HGqxqOI+tNC0e9t7CwMBPjDyZbPz/v
Pv1ra/iiO35nM56tj2/9vjP2f8Hfcn18sP7FeGMMf+D3t/XJJ5Pu5P3ywfj2tDf+au+js+34xf54
ZUP+dX/y42S9/Lg3CafrZHLw4fjhJOmVn26W97ulWu7dOtuNzrbL5mS37E565XePyrcn2+Vg1pu+
tTntOdP3ysflVncajbem2cZns96T2Z3PG3maNNwgagyRP1FGknSH7PKsb9vfBIddnEimtHE+pM89
wombH5JRHxIgQUZGcR7iwzTNB0B4dExi9wecDwlSwhEF87cijOoHok/cJH4G5N8hkyiOlHRVkvIU
yAGkmW3vxgib4TEl99pEb0mSNOTiGT8E8tC29/JBiyzSLxfT3CUnRD6RnSf0+1NyuiIRn4ehi6Gs
JZ0MjpdkEWJTK0SO8sEyuUc6TiuBLE8i4oaQpuTpCmlJp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