Skip to content

Commit

Permalink
Fix formatting and simplify
Browse files Browse the repository at this point in the history
Since @irevoire fixed problems arrising from calls to simplify, a lot of rounding/conversion bugs have been solved by extension. This lets the function work with a far simpler implementation.
  • Loading branch information
Goju-Ryu committed Sep 25, 2024
1 parent 33dfe66 commit 29d5d64
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
20 changes: 9 additions & 11 deletions numbat/modules/core/mixed_units.nbt
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ fn _mixed_units<D: Dim>(q: D, units: List<D>, names: List<String>, round_last: B

fn _zero_length<A: Dim>(val: A) -> A = val * 0 -> val


fn _mixed_unit_list<A: Dim>(val: A, units: List<A>, min_unit: A, acc: List<A>) -> List<A> =
if trunc_in(min_unit, val) == 0
then concat(acc, map(_zero_length, units))
else if len(units) == 1
then cons_end(val -> head(units), acc)
else _mixed_unit_list(val - unit_val, tail(units), min_unit, cons_end(unit_val, acc))
where unit_val: A =
fn _mixed_unit_list<D: Dim>(val: D, units: List<D>, acc: List<D>) -> List<D> =
if val == 0
then concat(acc, map(_zero_length, units))
else if len(units) == 1
then cons_end(val -> head(units), acc)
else _mixed_unit_list(val - unit_val, tail(units), cons_end(unit_val, acc))
where unit_val: D =
if (len(units) > 0)
then (val |> round_in(min_unit) |> trunc_in(head(units)))
else error("Units list cannot be empty")

then (val |> trunc_in(head(units)))
else error("Units list cannot be empty")
4 changes: 2 additions & 2 deletions numbat/modules/units/mixed.nbt
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ fn pounds_and_ounces(mass: Mass) -> String =


@name("Unit list")
@description("Convert a value to a mixed representation using the provided units.)
fn unit_list<A: Dim>(units: List<A>, value: A) -> List<A> = _mixed_unit_list(value, units, minimum(units) / 100, [])
@description("Convert a value to a mixed representation using the provided units.")
fn unit_list<D: Dim>(units: List<D>, value: D) -> List<D> = _mixed_unit_list(value, units, [])

0 comments on commit 29d5d64

Please sign in to comment.