From f41d050d32bb3378393a6cb654d4f901b3ef6486 Mon Sep 17 00:00:00 2001 From: Maxim Uvarov Date: Mon, 3 Jun 2024 21:33:34 +0800 Subject: [PATCH] hotfix for `significant-digits` to not error if the input was 0 (#863) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In the last `pr` there was an error, that I just discovered and fixed. Also I added test for this case in future, and some test for negative numbers ``` 0 | signfificant digits 2 33 │ 34 │ let insignif_position = $n - 1 - ($num | math abs | math log 10 | math floor) · ──┬─ ────┬─── · │ ╰── 'math log' undefined for values outside the open interval (0, Inf). · ╰── value originates from here 35 │ ╰──── ``` I'm sorry for hassle here 😞 --- stdlib-candidate/std-rfc/math/mod.nu | 11 ++++++++++- stdlib-candidate/tests/math.nu | 8 ++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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) +}