Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PATCH] Correct Math::BigInt -> new() for non-integer input. #14653

Closed
p5pRT opened this issue Apr 17, 2015 · 8 comments
Closed

[PATCH] Correct Math::BigInt -> new() for non-integer input. #14653

p5pRT opened this issue Apr 17, 2015 · 8 comments

Comments

@p5pRT
Copy link

p5pRT commented Apr 17, 2015

Migrated from rt.perl.org#124325 (status was 'resolved')

Searchable as RT124325$

@p5pRT
Copy link
Author

p5pRT commented Apr 17, 2015

From @pjacklam

Correct Math​::BigInt -> new() for non-integer input.

The documentation in Math​::BigInt says about input values that "Non-integer
values result in NaN." The actual behaviour doesn't comply with this.

- All input values in the range (-1,1) written as a decimal number, e.g.,
  -0.75 and 0.5, now return NaN, not 0.

- Input values with a large (absolute value) negative exponent, e.g.,
  1e-9999999, now return NaN. The former behaviour was to die with the
  message "Quantifier in {,} bigger than 32766 in regex; marked by ..."

- This patch fixes CPAN RT #61887 and CPAN RT #63038.

@p5pRT
Copy link
Author

p5pRT commented Apr 17, 2015

From @pjacklam

0001-Correct-Math-BigInt-new-for-non-integer-input.patch
From d54e1dead488e17ae4a5e02890795912e7654406 Mon Sep 17 00:00:00 2001
From: Peter John Acklam <pjacklam@online.no>
Date: Fri, 17 Apr 2015 21:28:37 +0200
Subject: [PATCH] Correct Math::BigInt -> new() for non-integer input.

The documentation in Math::BigInt says about input values that "Non-integer
values result in NaN." The actual behaviour doesn't comply with this.

- All input values in the range (-1,1) written as a decimal number, e.g.,
  -0.75 and 0.5, now return NaN, not 0.

- Input values with a large (absolute value) negative exponent, e.g.,
  1e-9999999, now return NaN. The former behaviour was to die with the
  message "Quantifier in {,} bigger than 32766 in regex; marked by ..."

- This patch fixes CPAN RT #61887 and CPAN RT #63038.
---
 dist/Math-BigInt/lib/Math/BigInt.pm | 16 +++++++++++-----
 dist/Math-BigInt/t/bigintpm.inc     |  4 ++--
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/dist/Math-BigInt/lib/Math/BigInt.pm b/dist/Math-BigInt/lib/Math/BigInt.pm
index 62f1be9..763b3f4 100644
--- a/dist/Math-BigInt/lib/Math/BigInt.pm
+++ b/dist/Math-BigInt/lib/Math/BigInt.pm
@@ -626,9 +626,13 @@ sub new
     elsif ($e < 0)
       {
       # xE-y, and empty mfv
-      #print "xE-y\n";
-      $e = abs($e);
-      if ($$miv !~ s/0{$e}$//)		# can strip so many zero's?
+      # Split the mantissa at the decimal point. E.g., if
+      # $$miv = 12345 and $e = -2, then $frac = 45 and $$miv = 123.
+
+      my $frac = substr($$miv, $e);     # $frac is fraction part
+      substr($$miv, $e) = "";           # $$miv is now integer part
+
+      if ($frac =~ /[^0]/)
         {
         if ($_trap_nan)
           {
@@ -640,8 +644,10 @@ sub new
         }
       }
     }
-  $self->{sign} = '+' if $$miv eq '0';			# normalize -0 => +0
-  $self->{value} = $CALC->_new($$miv) if $self->{sign} =~ /^[+-]$/;
+  unless ($self->{sign} eq $nan) {
+      $self->{sign} = '+' if $$miv eq '0';		# normalize -0 => +0
+      $self->{value} = $CALC->_new($$miv) if $self->{sign} =~ /^[+-]$/;
+  }
   # if any of the globals is set, use them to round and store them inside $self
   # do not round for new($x,undef,undef) since that is used by MBF to signal
   # no rounding
diff --git a/dist/Math-BigInt/t/bigintpm.inc b/dist/Math-BigInt/t/bigintpm.inc
index c3e815c..fa320b0 100644
--- a/dist/Math-BigInt/t/bigintpm.inc
+++ b/dist/Math-BigInt/t/bigintpm.inc
@@ -1091,6 +1091,8 @@ E23:NaN
 -1010E-2:NaN
 -1.01E+1:NaN
 -1.01E-1:NaN
+1E-999999:NaN
+0.5:NaN
 &bnan
 1:NaN
 2:NaN
@@ -2409,8 +2411,6 @@ inf:-inf:NaN
 15241:2:123
 144:2:12
 12:2:3
-0.49:2:0
-0.0049:2:0
 # invalid ones
 1:NaN:NaN
 -1:NaN:NaN
-- 
2.1.4

@p5pRT
Copy link
Author

p5pRT commented Jun 2, 2015

From @tonycoz

On Fri Apr 17 12​:30​:41 2015, pjacklam wrote​:

Correct Math​::BigInt -> new() for non-integer input.

The documentation in Math​::BigInt says about input values that "Non-integer
values result in NaN." The actual behaviour doesn't comply with this.

- All input values in the range (-1,1) written as a decimal number, e.g.,
-0.75 and 0.5, now return NaN, not 0.

- Input values with a large (absolute value) negative exponent, e.g.,
1e-9999999, now return NaN. The former behaviour was to die with the
message "Quantifier in {,} bigger than 32766 in regex; marked by ..."

- This patch fixes CPAN RT #61887 and CPAN RT #63038.

Thanks, applied to blead as 8db2973.

Tony

@p5pRT
Copy link
Author

p5pRT commented Jun 2, 2015

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Jun 2, 2015

@tonycoz - Status changed from 'open' to 'resolved'

@p5pRT
Copy link
Author

p5pRT commented Jun 4, 2015

@tonycoz - Status changed from 'resolved' to 'pending release'

@p5pRT
Copy link
Author

p5pRT commented May 13, 2016

From @khwilliamson

Thank you for submitting this report. You have helped make Perl better.
 
With the release of Perl 5.24.0 on May 9, 2016, this and 149 other issues have been resolved.

Perl 5.24.0 may be downloaded via https://metacpan.org/release/RJBS/perl-5.24.0

@p5pRT
Copy link
Author

p5pRT commented May 13, 2016

@khwilliamson - Status changed from 'pending release' to 'resolved'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant