diff --git a/stdlib-candidate/std-rfc/math/mod.nu b/stdlib-candidate/std-rfc/math/mod.nu index 9a045ea7..f5c0b884 100644 --- a/stdlib-candidate/std-rfc/math/mod.nu +++ b/stdlib-candidate/std-rfc/math/mod.nu @@ -31,7 +31,16 @@ export def 'significant-digits' [ _ => {$input} } - let insignif_position = $n - 1 - ($num | math abs | math log 10 | math floor) + let insignif_position = $num + | if $in == 0 { + 0 # it's impoosbile to calculate `math log` from 0, thus 0 errors here + } else { + math abs + | math log 10 + | math floor + | $n - 1 - $in + } + # See the note below the code for an explanation of the construct used. let scaling_factor = 10 ** ($insignif_position | math abs) diff --git a/stdlib-candidate/tests/math.nu b/stdlib-candidate/tests/math.nu index c719adc3..cafe00db 100644 --- a/stdlib-candidate/tests/math.nu +++ b/stdlib-candidate/tests/math.nu @@ -19,3 +19,11 @@ export def "test significant-digits-duration" [] { export def "test significant-digits-ints" [] { assert equal (123456 | math significant-digits 2) 120000 } + +export def "test significant-digits-0" [] { + assert equal (0 | math significant-digits 2) 0 +} + +export def "test significant-digits-negative" [] { + assert equal (-1.23456789 | math significant-digits 5) (-1.2346) +}