Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down
21 changes: 18 additions & 3 deletions src/rounding.typ
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions tests/num/rounding/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading