Skip to content

Commit

Permalink
hotfix for significant-digits to not error if the input was 0 (#863)
Browse files Browse the repository at this point in the history
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 😞
  • Loading branch information
maxim-uvarov committed Jun 3, 2024
1 parent ae5c1a2 commit f41d050
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
11 changes: 10 additions & 1 deletion stdlib-candidate/std-rfc/math/mod.nu
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions stdlib-candidate/tests/math.nu
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit f41d050

Please sign in to comment.