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] Fix RT 25274: Math::BigInt::Calc->_num() overflow. #10917
Comments
From @pjacklamWhy: Math::BigInt::Calc->_num() converts a big integer (in the internal perl -MMath::BigInt=lib,Calc -wle \ How: This fix computes the output in a different way, never multiply Test: It is not obvious to me how to test this automatically in a dist/Math-BigInt/lib/Math/BigInt/Calc.pm | 23 ++++++++++++++--------- Inline Patchdiff --git a/dist/Math-BigInt/lib/Math/BigInt/Calc.pm b/dist/Math-BigInt/lib/Math/BigInt/Calc.pm
index 3cb2025..c778dcd 100644
--- a/dist/Math-BigInt/lib/Math/BigInt/Calc.pm
+++ b/dist/Math-BigInt/lib/Math/BigInt/Calc.pm
@@ -272,17 +272,22 @@ sub _str
sub _num
{
- # Make a number (scalar int/float) from a BigInt object
- my $x = $_[1];
+ # Make a Perl scalar number (int/float) from a BigInt object.
+ my $x = $_[1];
- return 0+$x->[0] if scalar @$x == 1; # below $BASE
- my $fac = 1;
- my $num = 0;
- foreach (@$x)
- {
- $num += $fac*$_; $fac *= $BASE;
+ return 0 + $x->[0] if scalar @$x == 1; # below $BASE
+
+ # Start with the most significant element and work towards the least
+ # significant element. Avoid multiplying "inf" (which happens if the number
+ # overflows) with "0" (if there are zero elements in $x) since this gives
+ # "nan" which propagates to the output.
+
+ my $num = 0;
+ for (my $i = $#$x ; $i >= 0 ; --$i) {
+ $num *= $BASE;
+ $num += $x -> [$i];
}
- $num;
+ return $num;
}
##############################################################################
--
1.7.2.3 |
From @cpansproutOn Wed Dec 22 10:43:11 2010, pjacklam@online.no wrote:
############################################################################## Thank you. Applied as e5dcd52. |
The RT System itself - Status changed from 'new' to 'open' |
@cpansprout - Status changed from 'open' to 'resolved' |
From @cpansproutAs often happeneth, I forgat to forward this to p5p: On Sun Dec 26 14:38:35 2010, sprout wrote:
##############################################################################
|
From [Unknown Contact. See original ticket]As often happeneth, I forgat to forward this to p5p: On Sun Dec 26 14:38:35 2010, sprout wrote:
##############################################################################
|
Migrated from rt.perl.org#81170 (status was 'resolved')
Searchable as RT81170$
The text was updated successfully, but these errors were encountered: