Skip to content

Commit

Permalink
- Slightly faster conversion of integer strings (>= 2^64) into numbers.
Browse files Browse the repository at this point in the history
`Num("ddd...ddd")` is now faster than `Num("ddd...ddde0")`, where previosuly the performance was the same.
  • Loading branch information
trizen committed Aug 10, 2019
1 parent 5068296 commit 9e805cf
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions lib/Sidef/Types/Number/Number.pm
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,8 @@ package Sidef::Types::Number::Number {
);
}

# Floating-point
if ($s =~ /^([+-]?+(?=\.?[0-9])[0-9_]*+(?:\.[0-9_]++)?(?:[Ee](?:[+-]?+[0-9_]+))?)\z/) {
# Decimal expansion (parsed as a rational value)
if ($s =~ tr/e.// and $s =~ /^([+-]?+(?=\.?[0-9])[0-9_]*+(?:\.[0-9_]++)?(?:[Ee](?:[+-]?+[0-9_]+))?)\z/) {
my $frac = _str2frac($1);

if (index($frac, '/') != -1) {
Expand Down Expand Up @@ -400,16 +400,18 @@ package Sidef::Types::Number::Number {
}
}

# Floating point value
if ($s =~ tr/e.//) {
my $r = Math::MPFR::Rmpfr_init2(CORE::int($PREC));
if (Math::MPFR::Rmpfr_set_str($r, $s, 10, $ROUND)) {
Math::MPFR::Rmpfr_set_nan($r);
}
return $r;
}
#<<<
# Decimal expansion (parsed as a floating-point value)
#~ if ($s =~ tr/e.//) {
#~ my $r = Math::MPFR::Rmpfr_init2(CORE::int($PREC));
#~ if (Math::MPFR::Rmpfr_set_str($r, $s, 10, $ROUND)) {
#~ Math::MPFR::Rmpfr_set_nan($r);
#~ }
#~ return $r;
#~ }
#>>>

# Remove the plus sign (if any)
# Remove a leading plus sign
$s =~ s/^\+// if substr($s, 0, 1) eq '+';

# Fractional value
Expand Down

0 comments on commit 9e805cf

Please sign in to comment.