hotfix for significant-digits to not error if the input was 0 (#863)

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 😞
This commit is contained in:
Maxim Uvarov 2024-06-03 21:33:34 +08:00 committed by GitHub
parent ae5c1a2727
commit f41d050d32
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 1 deletions

View File

@ -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)

View File

@ -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)
}