diff --git a/README.md b/README.md index 5c9edfa..45e3077 100644 --- a/README.md +++ b/README.md @@ -209,7 +209,7 @@ If `follow-uncertainty` is false or the number just has no uncertainty, the norm ```typ #set-round( follow-uncertainty: bool = true, - uncertainty-precision: auto | int = auto, + uncertainty-precision: auto | int | dictionary = auto, mode: str = "places", precision: int | auto = auto, pad: bool = true, @@ -218,7 +218,10 @@ If `follow-uncertainty` is false or the number just has no uncertainty, the norm ) ``` - `follow-uncertainty: bool = true` : If `true`, round to match the uncertainty (if present) instead of rounding to a fixed precision. -- `uncertainty-precision: int | auto = auto` : The number of significant figures to round the uncertainty to. +- `uncertainty-precision: auto | int | dictionary = auto` : + - `auto` : The uncertainty is left as-is. + - `int` : The number of significant figures to round the uncertainty to. + - `(places: int)` : The number of decimal places to round the uncertainty to. - `mode: str = "places"` : Sets the fixed appendrounding mode. The possible options are - `"places"` : The number is rounded to the number of decimal places given by the `precision` parameter. - `"figures"` : The number is rounded to a number of significant figures given by the `precision` parameter. diff --git a/src/rounding.typ b/src/rounding.typ index 015322d..1ce61c1 100644 --- a/src/rounding.typ +++ b/src/rounding.typ @@ -302,8 +302,12 @@ /// -> "+" | "-" sign: "+", - /// The precision to round the uncertainty to. If `auto`, the uncertainty left as is. - /// -> auto | int + /// The precision to round the uncertainty to. If `auto`, the uncertainty + /// left as is. If given as an int, the precision is interpreted in terms + /// of number of significant figures. Alternatively, a dictionary of the form + /// `(places: int)` can be passed to specify the precision in terms of decimal + /// places. + /// -> auto | int | dictionary precision: auto, /// Rounding direction. @@ -333,10 +337,21 @@ calc.max( ..uncertainty.map(((i, f)) => f.len()) ) - } else { + } else if type(precision) == std.int { precision + calc.max( ..uncertainty.map(((i, f)) => count-leading-zeros(i + f) - i.len()) ) + } else if type(precision) == dictionary { + assert( + precision.keys() == ("places",), + message: "Expected a dictionary with the key \"places\"" + ) + precision.places + } else { + assert( + false, + message: "Unexpected argument " + repr(precision) + " for uncertainty-precision" + ) } uncertainty = uncertainty diff --git a/tests/num/rounding/test.typ b/tests/num/rounding/test.typ index 27e85fa..38cdb37 100644 --- a/tests/num/rounding/test.typ +++ b/tests/num/rounding/test.typ @@ -255,6 +255,24 @@ ), ("1", "23", (("", "04"), ("", "30"))), ) +#assert.eq( + round-to-uncertainty( + "1", + "23", + (("0", "04"), ("0", "3")), + precision: (places: 5), + ), + ("1", "23000", (("", "04000"), ("", "30000"))), +) +#assert.eq( + round-to-uncertainty( + "1455", + "23", + (("47", "04"), ("80", "3")), + precision: (places: -1), + ), + ("1460", "", (("50", ""), ("80", ""))), +) #assert.eq( round( "0",