Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Handle NaN and Inf in sprintf (directives e, f, g)
  • Loading branch information
usev6 committed Nov 9, 2015
1 parent 03b69e4 commit 94d5575
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/HLL/sprintf.nqp
Expand Up @@ -268,7 +268,7 @@ my module sprintf {
!! has_flag($/, 'space') ?? ' '
!! '';
$float := nqp::abs_n($float);
$float := stringify-to-precision($float, $precision);
$float := stringify-to-precision($float, $precision) unless nqp::isnanorinf($float);
pad-with-sign($sign, $float, $size, $pad);
}
sub scientific($float, $e, $precision, $size, $pad, $/) {
Expand All @@ -277,14 +277,16 @@ my module sprintf {
!! has_flag($/, 'space') ?? ' '
!! '';
$float := nqp::abs_n($float);
my $exp := $float == 0.0 ?? 0 !! nqp::floor_n(nqp::log_n($float) / nqp::log_n(10));
$float := $float / nqp::pow_n(10, $exp);
$float := stringify-to-precision($float, $precision);
if $exp < 0 {
$exp := -$exp;
$float := $float ~ $e ~ '-' ~ ($exp < 10 ?? '0' !! '') ~ $exp;
} else {
$float := $float ~ $e ~ '+' ~ ($exp < 10 ?? '0' !! '') ~ $exp;
unless nqp::isnanorinf($float) {
my $exp := $float == 0.0 ?? 0 !! nqp::floor_n(nqp::log_n($float) / nqp::log_n(10));
$float := $float / nqp::pow_n(10, $exp);
$float := stringify-to-precision($float, $precision);
if $exp < 0 {
$exp := -$exp;
$float := $float ~ $e ~ '-' ~ ($exp < 10 ?? '0' !! '') ~ $exp;
} else {
$float := $float ~ $e ~ '+' ~ ($exp < 10 ?? '0' !! '') ~ $exp;
}
}
pad-with-sign($sign, $float, $size, $pad);
}
Expand All @@ -295,6 +297,8 @@ my module sprintf {
!! '';
$float := nqp::abs_n($float);

return pad-with-sign($sign, $float, $size, $pad) if nqp::isnanorinf($float);

my $exp := $float == 0.0 ?? 0 !! nqp::floor_n(nqp::log_n($float) / nqp::log_n(10));

if -2 - $precision < $exp && $exp < $precision {
Expand Down

0 comments on commit 94d5575

Please sign in to comment.