|
5 | 5 | #import "parsing.typ" as parsing: nonum |
6 | 6 |
|
7 | 7 | #let update-state(state, args, name: none) = { |
| 8 | + assert-no-fixed(args) |
8 | 9 | state.update(s => { |
9 | 10 | assert-settable-args(args, s, name: name) |
10 | 11 | s + args.named() |
|
44 | 45 | } |
45 | 46 |
|
46 | 47 |
|
| 48 | +#let process-exponent(info, exponent) = { |
| 49 | + let new-exponent = if type(exponent) == dictionary { |
| 50 | + assert( |
| 51 | + "fixed" in exponent or "sci" in exponent, |
| 52 | + message: "Expected key \"fixed\" or \"sci\", got " + repr(exponent) |
| 53 | + ) |
| 54 | + |
| 55 | + if "fixed" in exponent { |
| 56 | + exponent.fixed |
| 57 | + } else { |
| 58 | + let threshold = exponent.sci |
| 59 | + if type(threshold) == int { |
| 60 | + threshold = (-threshold, threshold) |
| 61 | + } |
| 62 | + let e = parsing.compute-sci-digits(info) |
| 63 | + if e > threshold.at(0) and e < threshold.at(1) { |
| 64 | + return info |
| 65 | + } |
| 66 | + e |
| 67 | + } |
| 68 | + } else if exponent == "eng" { |
| 69 | + parsing.compute-eng-digits(info) |
| 70 | + } else if exponent == "sci" { |
| 71 | + parsing.compute-sci-digits(info) |
| 72 | + } |
| 73 | + |
| 74 | + let e = if info.e == none { 0 } else { int(info.e) } |
| 75 | + // let significant-figures = (info.int + info.frac).trim("0").len() |
| 76 | + |
| 77 | + let shift = utility.shift-decimal-left.with(digits: new-exponent - e) |
| 78 | + |
| 79 | + info.e = str(new-exponent).replace("−", "-") |
| 80 | + (info.int, info.frac) = shift(info.int, info.frac) |
| 81 | + |
| 82 | + if info.pm != none { |
| 83 | + if type(info.pm.first()) != array { |
| 84 | + info.pm = shift(..info.pm) |
| 85 | + } else { |
| 86 | + info.pm = pm.map(x => shift(..x)) |
| 87 | + } |
| 88 | + } |
| 89 | + // if info.int != "0" { |
| 90 | + // info.frac = info.frac.slice(0, calc.max(0, significant-figures - info.int.len())) |
| 91 | + // } |
| 92 | + |
| 93 | + info |
| 94 | +} |
| 95 | + |
| 96 | + |
47 | 97 |
|
48 | 98 | #let show-num = it => { |
49 | 99 |
|
|
59 | 109 | } |
60 | 110 | if "sign" not in info {info.sign = "" } |
61 | 111 | } else { |
62 | | - let num-str = number-to-string(it.number) |
63 | | - if num-str == none { |
64 | | - assert(false, message: "Cannot parse the number `" + repr(it.number) + "`") |
65 | | - } |
66 | | - info = decompose-normalized-number-string(num-str) |
| 112 | + info = parse-numeral(it.number) |
67 | 113 | } |
68 | 114 |
|
69 | | - /// Maybe shift exponent |
70 | | - if it.fixed != none { |
71 | | - let e = if info.e == none { 0 } else { int(info.e) } |
72 | | - let shift(int, frac) = utility.shift-decimal-left(int, frac, it.fixed - e) |
73 | | - (info.int, info.frac) = shift(info.int, info.frac) |
74 | | - |
75 | | - if info.pm != none { |
76 | | - if type(info.pm.first()) != array { |
77 | | - info.pm = shift(..info.pm) |
78 | | - } else { |
79 | | - info.pm = pm.map(x => shift(..x)) |
80 | | - } |
| 115 | + if it.exponent != auto { |
| 116 | + info = process-exponent(info, it.exponent) |
| 117 | + if "prefixed-eng" in it { |
| 118 | + info.e = none |
81 | 119 | } |
82 | | - |
83 | | - info.e = str(it.fixed).replace("−", "-") |
84 | 120 | } |
85 | 121 |
|
86 | 122 | /// Round number and uncertainty |
|
146 | 182 | force-parentheses-around-uncertainty: false, |
147 | 183 | ..args |
148 | 184 | ) = { |
| 185 | + assert-no-fixed(args) |
| 186 | + |
149 | 187 | let inline-args = ( |
150 | 188 | align: align, |
151 | 189 | prefix: prefix, |
|
0 commit comments