From 3d6017f56e6b8f8227e1fd4570d7e7a67b7fc28a Mon Sep 17 00:00:00 2001 From: Peter John Acklam Date: Fri, 5 Nov 2010 11:52:14 +0100 Subject: [PATCH] RT ticket #62101: as_int() returns NaN for inf Math::BigFloat -> binf() -> as_int() returns NaN, but should return inf. In other words, if $x is a Math::BigFloat, then $x -> as_int() shall return the same as Math::BigInt -> new($x). The bug appears both for +inf and -inf. -lib/Math/BigFloat.pm: Add two lines to catch the cases when the input is +/-inf and NaN, respectively. -t/bare_mbf.t: incremented test count by 3 -t/bigfltpm.inc: add 3 tests, for +inf, -inf, and NaN -t/bigfltpm.t: incremented test count by 3 -t/sub_mbf.t: incremented test count by 3 -t/with_sub.t: incremented test count by 3 --- dist/Math-BigInt/lib/Math/BigFloat.pm | 3 +++ dist/Math-BigInt/t/bare_mbf.t | 2 +- dist/Math-BigInt/t/bigfltpm.inc | 3 +++ dist/Math-BigInt/t/bigfltpm.t | 2 +- dist/Math-BigInt/t/sub_mbf.t | 2 +- dist/Math-BigInt/t/with_sub.t | 2 +- 6 files changed, 10 insertions(+), 4 deletions(-) diff --git a/dist/Math-BigInt/lib/Math/BigFloat.pm b/dist/Math-BigInt/lib/Math/BigFloat.pm index 0493442..668fca7 100644 --- a/dist/Math-BigInt/lib/Math/BigFloat.pm +++ b/dist/Math-BigInt/lib/Math/BigFloat.pm @@ -3684,6 +3684,9 @@ sub as_number $x = $x->can('as_float') ? $x->as_float() : $self->new(0+"$x"); } + return Math::BigInt->binf($x->sign()) if $x->is_inf(); + return Math::BigInt->bnan() if $x->is_nan(); + my $z = $MBI->_copy($x->{_m}); if ($x->{_es} eq '-') # < 0 { diff --git a/dist/Math-BigInt/t/bare_mbf.t b/dist/Math-BigInt/t/bare_mbf.t index 7fb1ff0..11b3222 100644 --- a/dist/Math-BigInt/t/bare_mbf.t +++ b/dist/Math-BigInt/t/bare_mbf.t @@ -1,7 +1,7 @@ #!/usr/bin/perl -w use strict; -use Test::More tests => 2316; +use Test::More tests => 2319; BEGIN { unshift @INC, 't'; } diff --git a/dist/Math-BigInt/t/bigfltpm.inc b/dist/Math-BigInt/t/bigfltpm.inc index e8801a5..059ebe1 100644 --- a/dist/Math-BigInt/t/bigfltpm.inc +++ b/dist/Math-BigInt/t/bigfltpm.inc @@ -587,6 +587,9 @@ fnormNaN:NaN -2:-2 -123.456:-123 -200:-200 +-inf:-inf +inf:inf +NaN:NaN # test for bug in brsft() not handling cases that return 0 0.000641:0 0.0006412:0 diff --git a/dist/Math-BigInt/t/bigfltpm.t b/dist/Math-BigInt/t/bigfltpm.t index fa6f878..0423086 100644 --- a/dist/Math-BigInt/t/bigfltpm.t +++ b/dist/Math-BigInt/t/bigfltpm.t @@ -1,7 +1,7 @@ #!/usr/bin/perl -w use strict; -use Test::More tests => 2316 +use Test::More tests => 2319 + 5; # own tests diff --git a/dist/Math-BigInt/t/sub_mbf.t b/dist/Math-BigInt/t/sub_mbf.t index 91b960c..ab7108a 100644 --- a/dist/Math-BigInt/t/sub_mbf.t +++ b/dist/Math-BigInt/t/sub_mbf.t @@ -1,7 +1,7 @@ #!/usr/bin/perl -w use strict; -use Test::More tests => 2316 +use Test::More tests => 2319 + 6; # + our own tests diff --git a/dist/Math-BigInt/t/with_sub.t b/dist/Math-BigInt/t/with_sub.t index 16ebb20..d4baaec 100644 --- a/dist/Math-BigInt/t/with_sub.t +++ b/dist/Math-BigInt/t/with_sub.t @@ -3,7 +3,7 @@ # Test use Math::BigFloat with => 'Math::BigInt::SomeSubclass'; use strict; -use Test::More tests => 2316 + 1; +use Test::More tests => 2319 + 1; use Math::BigFloat with => 'Math::BigInt::Subclass', lib => 'Calc'; -- 2.7.4