Skip to content

Commit

Permalink
Fix bug where Solaris 80 bit long doubles on x86 hardware causes
Browse files Browse the repository at this point in the history
10**11 to not equal 10E10.  Modified PPI::Token::Number::Exp to
match what the Perl interpreter does internally when parsing
scientific notation.
  • Loading branch information
jmaslak authored and wchristian committed Jan 20, 2017
1 parent a259c6a commit 9f91982
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
24 changes: 22 additions & 2 deletions lib/PPI/Token/Number/Exp.pm
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,28 @@ sub literal {
my $neg = $mantissa =~ s/^\-//;
$mantissa =~ s/^\./0./;
$exponent =~ s/^\+//;
my $val = $mantissa * 10 ** $exponent;
return $neg ? -$val : $val;

# This algorithm is reasonably close to the S_mulexp10()
# algorithm from the Perl source code, so it should arrive
# at the same answer as Perl most of the time.
my $negpow = 0;
if ($exponent < 0) {
$negpow = 1;
$exponent *= -1;
}

my $result = 1;
my $power = 10;
for (my $bit = 1; $exponent; $bit = $bit << 1) {
if ($exponent & $bit) {
$exponent = $exponent ^ $bit;
$result *= $power;
}
$power *= $power;
}

my $val = $neg ? 0 - $mantissa : $mantissa;
return $negpow ? $val / $result : $val * $result;
}


Expand Down
2 changes: 1 addition & 1 deletion t/07_token.t
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ SCOPE: {
'0.0e-10' => '10e',
'0.0e+10' => '10e',
'0.0e100' => '10e',
'1_0e1_0' => '10e', # Known to fail on 5.6.2
'1_0e1_0' => '10e',
'0b' => 2,
'0b0' => 2,
'0b10' => 2,
Expand Down

0 comments on commit 9f91982

Please sign in to comment.