accuracy-claim-kit 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/LICENSE +21 -0
- package/METHOD.md +99 -0
- package/README.md +478 -2
- package/dist/agreement.d.ts +62 -0
- package/dist/agreement.js +141 -0
- package/dist/agreement.js.map +1 -0
- package/dist/assess.d.ts +36 -0
- package/dist/assess.js +954 -0
- package/dist/assess.js.map +1 -0
- package/dist/census.d.ts +44 -0
- package/dist/census.js +95 -0
- package/dist/census.js.map +1 -0
- package/dist/dates.d.ts +2 -0
- package/dist/dates.js +23 -0
- package/dist/dates.js.map +1 -0
- package/dist/display.d.ts +56 -0
- package/dist/display.js +341 -0
- package/dist/display.js.map +1 -0
- package/dist/exact.d.ts +48 -0
- package/dist/exact.js +102 -0
- package/dist/exact.js.map +1 -0
- package/dist/format.d.ts +29 -0
- package/dist/format.js +120 -0
- package/dist/format.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/normal.d.ts +12 -0
- package/dist/normal.js +100 -0
- package/dist/normal.js.map +1 -0
- package/dist/text.d.ts +58 -0
- package/dist/text.js +202 -0
- package/dist/text.js.map +1 -0
- package/dist/types.d.ts +186 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/validate.d.ts +44 -0
- package/dist/validate.js +156 -0
- package/dist/validate.js.map +1 -0
- package/dist/wilson.d.ts +41 -0
- package/dist/wilson.js +86 -0
- package/dist/wilson.js.map +1 -0
- package/package.json +76 -4
package/dist/display.js
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
// Reads the printed form of a claimed figure ("99.8%", "99%+", "<1%", "about
|
|
2
|
+
// 95%", "95-98%", "1 in 10,000") into the figure a reader takes away, using the
|
|
3
|
+
// favorable reading, and answers the exact questions the verdict needs.
|
|
4
|
+
import { matchesCount } from './census.js';
|
|
5
|
+
import { atLeastDecimal, atMostDecimal, beyondNoise, snapAbsolute, splitDouble, withinHalfUnit } from './exact.js';
|
|
6
|
+
import { foldText } from './text.js';
|
|
7
|
+
import { clip } from './validate.js';
|
|
8
|
+
const NUM = '(\\d{1,3}(?:\\.\\d{1,15})?)';
|
|
9
|
+
const RANGE = new RegExp(`^${NUM} ?%? ?(?:-|–|—|to) ?${NUM} ?%$`);
|
|
10
|
+
const SINGLE = new RegExp(`^${NUM} ?%$`);
|
|
11
|
+
const PLUS_BEFORE_PERCENT = new RegExp(`^${NUM}\\+ ?%$`);
|
|
12
|
+
const ONE_IN_N = /^1 in ((?:[1-9]\d{0,2}(?:,\d{3})+)|(?:[1-9]\d{0,14}))$/;
|
|
13
|
+
/** A bound written after the figure: "95% or higher", "5% or lower". */
|
|
14
|
+
const BOUND_SUFFIX = /^(.*) or (higher|more|above|greater|lower|less|below)$/;
|
|
15
|
+
const FLOOR_SUFFIXES = ['higher', 'more', 'above', 'greater'];
|
|
16
|
+
const PREFIXES = [
|
|
17
|
+
// [text, kind, needs a space after, strict]
|
|
18
|
+
['>=', 'floor', false, false],
|
|
19
|
+
['≥', 'floor', false, false],
|
|
20
|
+
['>', 'floor', false, true],
|
|
21
|
+
['<=', 'ceiling', false, false],
|
|
22
|
+
['≤', 'ceiling', false, false],
|
|
23
|
+
['<', 'ceiling', false, true],
|
|
24
|
+
['≈', 'approx', false, false],
|
|
25
|
+
['~', 'approx', false, false],
|
|
26
|
+
['at least', 'floor', true, false],
|
|
27
|
+
['greater than or equal to', 'floor', true, false],
|
|
28
|
+
['not less than', 'floor', true, false],
|
|
29
|
+
['no less than', 'floor', true, false],
|
|
30
|
+
['a minimum of', 'floor', true, false],
|
|
31
|
+
['minimum of', 'floor', true, false],
|
|
32
|
+
['more than', 'floor', true, true],
|
|
33
|
+
['greater than', 'floor', true, true],
|
|
34
|
+
['over', 'floor', true, true],
|
|
35
|
+
['above', 'floor', true, true],
|
|
36
|
+
['at most', 'ceiling', true, false],
|
|
37
|
+
['less than or equal to', 'ceiling', true, false],
|
|
38
|
+
['not more than', 'ceiling', true, false],
|
|
39
|
+
['a maximum of', 'ceiling', true, false],
|
|
40
|
+
['maximum of', 'ceiling', true, false],
|
|
41
|
+
['no more than', 'ceiling', true, false],
|
|
42
|
+
['less than', 'ceiling', true, true],
|
|
43
|
+
['fewer than', 'ceiling', true, true],
|
|
44
|
+
['up to', 'ceiling', true, false],
|
|
45
|
+
['under', 'ceiling', true, true],
|
|
46
|
+
['below', 'ceiling', true, true],
|
|
47
|
+
['approximately', 'approx', true, false],
|
|
48
|
+
['approx.', 'approx', true, false],
|
|
49
|
+
['approx', 'approx', true, false],
|
|
50
|
+
['about', 'approx', true, false],
|
|
51
|
+
['around', 'approx', true, false],
|
|
52
|
+
['roughly', 'approx', true, false],
|
|
53
|
+
];
|
|
54
|
+
function bad(display) {
|
|
55
|
+
return new RangeError(`claim.display must be a percent such as "99.8%", "99%+", "<1%", "about 95%", "95-98%" or "1 in 10,000"; got "${clip(display)}"`);
|
|
56
|
+
}
|
|
57
|
+
function readNum(text, display) {
|
|
58
|
+
const [whole = '', frac = ''] = text.split('.');
|
|
59
|
+
if (Number(whole) > 100 || (Number(whole) === 100 && /[1-9]/.test(frac))) {
|
|
60
|
+
throw new RangeError(`claim.display must be between 0% and 100%; got "${clip(display)}"`);
|
|
61
|
+
}
|
|
62
|
+
return { whole, frac, digits: BigInt(`${whole}${frac}`), decimals: frac.length };
|
|
63
|
+
}
|
|
64
|
+
function proportionOf(num) {
|
|
65
|
+
// Parsing "99.8e-2" is correctly rounded; dividing 99.8 by 100 is not always.
|
|
66
|
+
return Number(`${num.frac === '' ? num.whole : `${num.whole}.${num.frac}`}e-2`);
|
|
67
|
+
}
|
|
68
|
+
function numLess(a, b) {
|
|
69
|
+
const scale = Math.max(a.decimals, b.decimals);
|
|
70
|
+
return a.digits * 10n ** BigInt(scale - a.decimals) < b.digits * 10n ** BigInt(scale - b.decimals);
|
|
71
|
+
}
|
|
72
|
+
/** A half-integer as text: 99.5, 100. `twice` is the value times two. */
|
|
73
|
+
function halves(twice) {
|
|
74
|
+
return twice % 2n === 0n ? String(twice / 2n) : `${String(twice / 2n)}.5`;
|
|
75
|
+
}
|
|
76
|
+
function oneInN(text, kind, strict, folded, display) {
|
|
77
|
+
const digits = text.replace(/,/g, '');
|
|
78
|
+
const big = BigInt(digits);
|
|
79
|
+
const bound = kind === 'floor' || kind === 'ceiling' ? kind : null;
|
|
80
|
+
if (kind === 'floor' && strict && big === 1n) {
|
|
81
|
+
// nothing is above 100%
|
|
82
|
+
throw new RangeError(`claim.display "${clip(display)}" asks for more than 100%, which is impossible`);
|
|
83
|
+
}
|
|
84
|
+
const approximate = kind === 'approx';
|
|
85
|
+
const zeros = digits.length - digits.replace(/0+$/, '').length;
|
|
86
|
+
const placeholder = 10n ** BigInt(zeros);
|
|
87
|
+
// twice the half-width of the band around N: 1 for an exact N, 10^zeros for an approximate one
|
|
88
|
+
const unit = approximate ? placeholder : 1n;
|
|
89
|
+
const n2 = 2n * big;
|
|
90
|
+
const figure = 1 / Number(digits);
|
|
91
|
+
const shown = `"${clip(folded, 30)}"`;
|
|
92
|
+
const band = `1 in ${halves(n2 - unit)} to 1 in ${halves(n2 + unit)}`;
|
|
93
|
+
const widen = zeros > 0 ? `; write "about 1 in ${digits}" to allow 1 in ${halves(n2 - placeholder)} to 1 in ${halves(n2 + placeholder)}` : '';
|
|
94
|
+
// "1 in 1" is 100%: an absolute claim, never a band (1 in 0.5 to 1 in 1.5 would take in any rate from 66.7% up)
|
|
95
|
+
const everything = big === 1n && bound === null;
|
|
96
|
+
const bandNote = bound !== null
|
|
97
|
+
? null
|
|
98
|
+
: everything
|
|
99
|
+
? `${shown} is read as 100%, an absolute claim that matches only n of n.`
|
|
100
|
+
: approximate
|
|
101
|
+
? `${shown} is read as ${band}.`
|
|
102
|
+
: `${shown} is read as exactly 1 in ${digits} (${band})${widen}.`;
|
|
103
|
+
const within = (k, n) => everything ? k === n : BigInt(k) * (n2 + unit) >= 2n * BigInt(n) && BigInt(k) * (n2 - unit) <= 2n * BigInt(n);
|
|
104
|
+
const consistentWith = (value) => {
|
|
105
|
+
if (everything)
|
|
106
|
+
return snapAbsolute(value) === 1;
|
|
107
|
+
// 1/(N + h) <= value <= 1/(N - h), with one ulp of slack each way
|
|
108
|
+
const { mantissa, exponent } = splitDouble(value);
|
|
109
|
+
if (exponent >= 0)
|
|
110
|
+
return false;
|
|
111
|
+
const d = 1n << BigInt(-exponent);
|
|
112
|
+
return (mantissa + 1n) * (n2 + unit) >= 2n * d && (mantissa === 0n ? -1n : mantissa - 1n) * (n2 - unit) <= 2n * d;
|
|
113
|
+
};
|
|
114
|
+
const satisfiedBy = (k, n) => {
|
|
115
|
+
if (bound === null)
|
|
116
|
+
return within(k, n);
|
|
117
|
+
// k / n against 1 / N, by integer arithmetic: floor 1/N <= k/n is k N >= n
|
|
118
|
+
const lhs = BigInt(k) * big - BigInt(n);
|
|
119
|
+
if (bound === 'floor')
|
|
120
|
+
return strict ? lhs > 0n : lhs >= 0n;
|
|
121
|
+
return strict ? lhs < 0n : lhs <= 0n;
|
|
122
|
+
};
|
|
123
|
+
const agreesWith = (value) => {
|
|
124
|
+
if (bound === 'floor')
|
|
125
|
+
return strict ? beyondNoise(value, figure, true) : !beyondNoise(figure, value, true);
|
|
126
|
+
if (bound === 'ceiling')
|
|
127
|
+
return strict ? beyondNoise(value, figure, false) : !beyondNoise(figure, value, false);
|
|
128
|
+
return consistentWith(value);
|
|
129
|
+
};
|
|
130
|
+
return {
|
|
131
|
+
info: { form: bound ?? 'one-in-n', figure, decimals: null },
|
|
132
|
+
bound,
|
|
133
|
+
strict: bound !== null && strict,
|
|
134
|
+
bandNote,
|
|
135
|
+
mismatchHint: everything ? 'the value must be 1 (100%), which is what "1 in 1" means' : `the value must lie inside the band the display is read at (${band})`,
|
|
136
|
+
consistentWith,
|
|
137
|
+
agreesWith,
|
|
138
|
+
matchesObserved: (k, n) => bound === null && within(k, n),
|
|
139
|
+
satisfiedBy,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Parses the display for a metric where higher is better (accuracy, precision,
|
|
144
|
+
* recall) or lower is better (error rates). Throws TypeError for a non-string
|
|
145
|
+
* and RangeError for text that is not one of the forms above, or a bound that
|
|
146
|
+
* points the wrong way for the metric ("<99%" accuracy).
|
|
147
|
+
*/
|
|
148
|
+
export function parseDisplayFigure(display, higherIsBetter) {
|
|
149
|
+
if (typeof display !== 'string')
|
|
150
|
+
throw new TypeError('claim.display must be a string');
|
|
151
|
+
if (display.length > 64)
|
|
152
|
+
throw new RangeError('claim.display is too long to be a percent figure');
|
|
153
|
+
const folded = foldText(display);
|
|
154
|
+
let t = folded;
|
|
155
|
+
if (t.endsWith('.'))
|
|
156
|
+
t = t.slice(0, -1);
|
|
157
|
+
let kind = 'percent';
|
|
158
|
+
let strict = false;
|
|
159
|
+
for (const [prefix, prefixKind, needsSpace, prefixStrict] of PREFIXES) {
|
|
160
|
+
if (t.startsWith(prefix)) {
|
|
161
|
+
let rest = t.slice(prefix.length);
|
|
162
|
+
if (needsSpace) {
|
|
163
|
+
if (!rest.startsWith(' '))
|
|
164
|
+
continue;
|
|
165
|
+
rest = rest.slice(1);
|
|
166
|
+
}
|
|
167
|
+
else if (rest.startsWith(' ')) {
|
|
168
|
+
rest = rest.slice(1);
|
|
169
|
+
}
|
|
170
|
+
kind = prefixKind;
|
|
171
|
+
strict = prefixStrict;
|
|
172
|
+
t = rest;
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
// "95% or higher" is "at least 95%": a bound written after the figure. It does
|
|
177
|
+
// not combine with a word in front, and on "1 in N" it would be ambiguous
|
|
178
|
+
// ("1 in 100 or lower": a lower rate, or a lower N?), so those are not read.
|
|
179
|
+
const suffix = BOUND_SUFFIX.exec(t);
|
|
180
|
+
if (suffix !== null) {
|
|
181
|
+
if (kind !== 'percent' || ONE_IN_N.test(suffix[1]))
|
|
182
|
+
throw bad(display);
|
|
183
|
+
kind = FLOOR_SUFFIXES.includes(suffix[2]) ? 'floor' : 'ceiling';
|
|
184
|
+
strict = false;
|
|
185
|
+
t = suffix[1];
|
|
186
|
+
}
|
|
187
|
+
// "1 in N": 1/N. N is exact (plus or minus one half) unless the figure is
|
|
188
|
+
// written as approximate ("about 1 in 100"), when the trailing zeros of N are
|
|
189
|
+
// placeholders: 100 means 50 to 150. A bound on it ("fewer than 1 in 1,000")
|
|
190
|
+
// is a bound at 1/N.
|
|
191
|
+
const one = ONE_IN_N.exec(t);
|
|
192
|
+
if (one !== null) {
|
|
193
|
+
if (kind === 'floor' && !higherIsBetter)
|
|
194
|
+
throw new RangeError(`claim.display "${clip(display)}" is a lower bound, which does not fit a metric where lower is better`);
|
|
195
|
+
if (kind === 'ceiling' && higherIsBetter)
|
|
196
|
+
throw new RangeError(`claim.display "${clip(display)}" is an upper bound, which does not fit a metric where higher is better`);
|
|
197
|
+
return oneInN(one[1], kind, strict, folded, display);
|
|
198
|
+
}
|
|
199
|
+
if (kind === 'percent') {
|
|
200
|
+
const range = RANGE.exec(t);
|
|
201
|
+
if (range !== null) {
|
|
202
|
+
const low = readNum(range[1], display);
|
|
203
|
+
const high = readNum(range[2], display);
|
|
204
|
+
if (numLess(high, low))
|
|
205
|
+
throw bad(display);
|
|
206
|
+
const end = higherIsBetter ? high : low;
|
|
207
|
+
return {
|
|
208
|
+
info: { form: 'range', figure: proportionOf(end), decimals: end.decimals },
|
|
209
|
+
bound: null,
|
|
210
|
+
strict: false,
|
|
211
|
+
bandNote: null,
|
|
212
|
+
mismatchHint: 'the value must lie inside the range',
|
|
213
|
+
consistentWith(value) {
|
|
214
|
+
// inside the range, with only one ulp of representation error at each end
|
|
215
|
+
return atLeastDecimal(value, low.digits, low.decimals + 2) && atMostDecimal(value, high.digits, high.decimals + 2);
|
|
216
|
+
},
|
|
217
|
+
agreesWith(value) {
|
|
218
|
+
return this.consistentWith(value);
|
|
219
|
+
},
|
|
220
|
+
matchesObserved: (k, n) => matchesCount(end.digits, end.decimals + 2, k, n),
|
|
221
|
+
// a range is judged at its favorable end, as a point
|
|
222
|
+
satisfiedBy: (k, n) => matchesCount(end.digits, end.decimals + 2, k, n),
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
// "99+%" is "99%+"
|
|
226
|
+
const plus = PLUS_BEFORE_PERCENT.exec(t);
|
|
227
|
+
if (plus !== null) {
|
|
228
|
+
kind = 'floor';
|
|
229
|
+
t = `${plus[1]}%`;
|
|
230
|
+
}
|
|
231
|
+
else if (t.endsWith('+')) {
|
|
232
|
+
kind = 'floor';
|
|
233
|
+
t = t.slice(0, -1);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
const single = SINGLE.exec(t);
|
|
237
|
+
if (single === null)
|
|
238
|
+
throw bad(display);
|
|
239
|
+
const num = readNum(single[1], display);
|
|
240
|
+
const form = kind;
|
|
241
|
+
if (form === 'floor' && !higherIsBetter)
|
|
242
|
+
throw new RangeError(`claim.display "${clip(display)}" is a lower bound, which does not fit a metric where lower is better`);
|
|
243
|
+
if (form === 'ceiling' && higherIsBetter)
|
|
244
|
+
throw new RangeError(`claim.display "${clip(display)}" is an upper bound, which does not fit a metric where higher is better`);
|
|
245
|
+
const scale = num.decimals + 2;
|
|
246
|
+
const isBound = form === 'floor' || form === 'ceiling';
|
|
247
|
+
if (isBound && strict && ((form === 'floor' && num.digits === 10n ** BigInt(scale)) || (form === 'ceiling' && num.digits === 0n))) {
|
|
248
|
+
// nothing is above 100% or below 0%
|
|
249
|
+
throw new RangeError(`claim.display "${clip(display)}" asks for more than 100% or less than 0%, which is impossible`);
|
|
250
|
+
}
|
|
251
|
+
return {
|
|
252
|
+
info: { form, figure: proportionOf(num), decimals: num.decimals },
|
|
253
|
+
bound: isBound ? form : null,
|
|
254
|
+
strict: isBound && strict,
|
|
255
|
+
bandNote: null,
|
|
256
|
+
mismatchHint: `they may differ by at most half a unit of the last digit printed (${(0.5 / 10 ** num.decimals).toFixed(num.decimals + 1)} percentage points)`,
|
|
257
|
+
consistentWith: (value) => withinHalfUnit(value, num.digits, scale, scale),
|
|
258
|
+
agreesWith(value) {
|
|
259
|
+
const shown = proportionOf(num);
|
|
260
|
+
if (form === 'floor')
|
|
261
|
+
return strict ? beyondNoise(value, shown, true) : !beyondNoise(shown, value, true);
|
|
262
|
+
if (form === 'ceiling')
|
|
263
|
+
return strict ? beyondNoise(value, shown, false) : !beyondNoise(shown, value, false);
|
|
264
|
+
// a printed 100% or 0% is absolute: it is the figure only for a value that is exactly 1 or 0
|
|
265
|
+
// (within the value noise), never for 99.6% that merely rounds to it (AGENTS.md: absolute claims).
|
|
266
|
+
// A figure written as approximate ("about 100%") keeps its rounding band on purpose, in the text scan:
|
|
267
|
+
// it does not claim certainty, and the README says so.
|
|
268
|
+
if (form === 'percent' && (num.digits === 0n || num.digits === 10n ** BigInt(scale)))
|
|
269
|
+
return snapAbsolute(value) === shown;
|
|
270
|
+
return withinHalfUnit(value, num.digits, scale, scale);
|
|
271
|
+
},
|
|
272
|
+
matchesObserved: (k, n) => matchesCount(num.digits, scale, k, n),
|
|
273
|
+
satisfiedBy(k, n) {
|
|
274
|
+
const lhs = num.digits * BigInt(n) - BigInt(k) * 10n ** BigInt(scale);
|
|
275
|
+
if (form === 'floor')
|
|
276
|
+
return strict ? lhs < 0n : lhs <= 0n; // figure < or <= k/n
|
|
277
|
+
if (form === 'ceiling')
|
|
278
|
+
return strict ? lhs > 0n : lhs >= 0n; // figure > or >= k/n
|
|
279
|
+
return matchesCount(num.digits, scale, k, n);
|
|
280
|
+
},
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
// ---- figures in the claim's own wording ---------------------------------
|
|
284
|
+
const escapeRegex = (text) => text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
285
|
+
const PREFIX_ALTERNATIVES = PREFIXES.map(([text]) => text)
|
|
286
|
+
.sort((a, b) => b.length - a.length)
|
|
287
|
+
.map(escapeRegex)
|
|
288
|
+
.join('|');
|
|
289
|
+
const TEXT_NUM = '\\d{1,3}(?:\\.\\d{1,15})?';
|
|
290
|
+
/** A printed figure inside prose: an optional bound or approximation word, then a percent, a range or "1 in N". */
|
|
291
|
+
const TEXT_FIGURE = new RegExp(`(?<![\\p{L}\\p{N}.])(?:(?:${PREFIX_ALTERNATIVES}) ?)?(?:${TEXT_NUM}(?: ?%)? ?(?:-|to) ?${TEXT_NUM} ?%|${TEXT_NUM} ?\\+? ?%\\+?(?: or (?:higher|more|above|greater|lower|less|below))?|1 in (?:[1-9]\\d{0,2}(?:,\\d{3})+|[1-9]\\d{0,14}))(?![\\p{N}])`, 'gu');
|
|
292
|
+
const ABSOLUTE_WORDS = /(?<![\p{L}\p{N}])(?:perfect|flawless|error-free|no errors|zero errors)(?![\p{L}\p{N}])/u;
|
|
293
|
+
/** Most figures read from one text; each is cheap, but the count is bounded anyway. */
|
|
294
|
+
const MAX_TEXT_FIGURES = 50;
|
|
295
|
+
/**
|
|
296
|
+
* The figures in `text` that say something other than the judged figure, as
|
|
297
|
+
* written (folded). A figure is read by the same parser as `claim.display`;
|
|
298
|
+
* one that cannot be read (a bound pointing the wrong way for the metric, an
|
|
299
|
+
* impossible percent) is skipped. A figure agrees when it describes `value` or
|
|
300
|
+
* `assessed` (the value, or the printed display that was judged instead), or,
|
|
301
|
+
* for a point, approximate or range figure, their complement ("87.7% right"
|
|
302
|
+
* beside a 12.3% error rate). A printed 100% or 0% is absolute: it describes
|
|
303
|
+
* only a value of exactly 1 or 0, never 99.6% that rounds to it. The words perfect, flawless, error-free, no
|
|
304
|
+
* errors and zero errors mean 100% (0% for a lower-is-better metric). The
|
|
305
|
+
* text is only scanned, never judged: nothing here changes a verdict.
|
|
306
|
+
*/
|
|
307
|
+
export function textFiguresDiffering(text, value, assessed, higherIsBetter) {
|
|
308
|
+
// "99.9 percent" is the figure "99.9%"
|
|
309
|
+
// The Arabic percent sign (U+066A) has no compatibility fold to "%"; it is the same sign.
|
|
310
|
+
const folded = foldText(text)
|
|
311
|
+
.replace(/\u066A/g, '%')
|
|
312
|
+
.replace(/(\d) ?(?:percent|per cent|pct)(?![\p{L}\p{N}])/gu, '$1%');
|
|
313
|
+
const differing = [];
|
|
314
|
+
let seen = 0;
|
|
315
|
+
for (const match of folded.matchAll(TEXT_FIGURE)) {
|
|
316
|
+
if (seen >= MAX_TEXT_FIGURES)
|
|
317
|
+
break;
|
|
318
|
+
seen += 1;
|
|
319
|
+
const snippet = match[0];
|
|
320
|
+
let figure;
|
|
321
|
+
try {
|
|
322
|
+
figure = parseDisplayFigure(snippet, higherIsBetter);
|
|
323
|
+
}
|
|
324
|
+
catch {
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
let agrees = figure.agreesWith(value) || figure.agreesWith(assessed);
|
|
328
|
+
if (!agrees && figure.bound === null)
|
|
329
|
+
agrees = figure.agreesWith(1 - value) || figure.agreesWith(1 - assessed);
|
|
330
|
+
if (!agrees)
|
|
331
|
+
differing.push(snippet);
|
|
332
|
+
}
|
|
333
|
+
const absolute = ABSOLUTE_WORDS.exec(folded);
|
|
334
|
+
if (absolute !== null) {
|
|
335
|
+
const exact = higherIsBetter ? 1 : 0;
|
|
336
|
+
if (value !== exact && assessed !== exact)
|
|
337
|
+
differing.push(absolute[0]);
|
|
338
|
+
}
|
|
339
|
+
return differing;
|
|
340
|
+
}
|
|
341
|
+
//# sourceMappingURL=display.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"display.js","sourceRoot":"","sources":["../src/display.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,gFAAgF;AAChF,wEAAwE;AAExE,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC1C,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,WAAW,EAAE,YAAY,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAClH,OAAO,EAAE,QA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CAAC,GAAG,QAAQ,CAAC,CAAA;QAC9G,IAAI,CAAC,MAAM;YAAE,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IACtC,CAAC;IACD,MAAM,QAAQ,GAAG,cAAc,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;IAC5C,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;QACtB,MAAM,KAAK,GAAG,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;QACpC,IAAI,KAAK,KAAK,KAAK,IAAI,QAAQ,KAAK,KAAK;YAAE,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAA;IACxE,CAAC;IACD,OAAO,SAAS,CAAA;AAClB,CAAC","sourcesContent":["// Reads the printed form of a claimed figure (\"99.8%\", \"99%+\", \"<1%\", \"about\n// 95%\", \"95-98%\", \"1 in 10,000\") into the figure a reader takes away, using the\n// favorable reading, and answers the exact questions the verdict needs.\n\nimport { matchesCount } from './census.js'\nimport { atLeastDecimal, atMostDecimal, beyondNoise, snapAbsolute, splitDouble, withinHalfUnit } from './exact.js'\nimport { foldText } from './text.js'\nimport { clip } from './validate.js'\n\nexport type DisplayForm = 'percent' | 'approx' | 'floor' | 'ceiling' | 'range' | 'one-in-n'\n\n/** What a result records about the parsed display. */\nexport interface ParsedDisplayInfo {\n form: DisplayForm\n /** The figure judged, as a proportion (0.99 for \"99%+\"). */\n figure: number\n /** Decimal places of the printed percent, or null for \"1 in N\". */\n decimals: number | null\n}\n\nexport interface DisplayFigure {\n info: ParsedDisplayInfo\n /** \"floor\" for \"99%+\" style claims, \"ceiling\" for \"<1%\", else null. */\n bound: 'floor' | 'ceiling' | null\n /** True for \"over 99%\" / \"<1%\": the bound itself does not satisfy the claim. */\n strict: boolean\n /** True when the printed value agrees with `value` to its rounding (plus one ulp). */\n consistentWith(value: number): boolean\n /** What the value had to satisfy when it disagrees with this figure, as the end of an error message. */\n mismatchHint: string\n /** For \"1 in N\": what band N was read at, to put in an explanation; null for every other form. */\n bandNote: string | null\n /**\n * True when `value` is what this figure says: a point, approximate figure, range or\n * \"1 in N\" within its printed rounding, a floor at or below the value, a ceiling at or\n * above it (strict bounds exclude equality). One ulp of representation error is allowed.\n */\n agreesWith(value: number): boolean\n /** True when the observed rate k / n equals the figure to its printed precision. */\n matchesObserved(k: number, n: number): boolean\n /**\n * Census check of the printed figure against the exact count k / n: a bound as\n * an inequality (strict ones exclude equality), a point, range end or \"1 in N\"\n * as a match to its printed precision.\n */\n satisfiedBy(k: number, n: number): boolean\n}\n\ninterface Num {\n whole: string\n frac: string\n digits: bigint\n decimals: number\n}\n\nconst NUM = '(\\\\d{1,3}(?:\\\\.\\\\d{1,15})?)'\nconst RANGE = new RegExp(`^${NUM} ?%? ?(?:-|–|—|to) ?${NUM} ?%$`)\nconst SINGLE = new RegExp(`^${NUM} ?%$`)\nconst PLUS_BEFORE_PERCENT = new RegExp(`^${NUM}\\\\+ ?%$`)\nconst ONE_IN_N = /^1 in ((?:[1-9]\\d{0,2}(?:,\\d{3})+)|(?:[1-9]\\d{0,14}))$/\n/** A bound written after the figure: \"95% or higher\", \"5% or lower\". */\nconst BOUND_SUFFIX = /^(.*) or (higher|more|above|greater|lower|less|below)$/\nconst FLOOR_SUFFIXES: readonly string[] = ['higher', 'more', 'above', 'greater']\n\nconst PREFIXES: readonly (readonly [string, 'floor' | 'ceiling' | 'approx', boolean, boolean])[] = [\n // [text, kind, needs a space after, strict]\n ['>=', 'floor', false, false],\n ['≥', 'floor', false, false],\n ['>', 'floor', false, true],\n ['<=', 'ceiling', false, false],\n ['≤', 'ceiling', false, false],\n ['<', 'ceiling', false, true],\n ['≈', 'approx', false, false],\n ['~', 'approx', false, false],\n ['at least', 'floor', true, false],\n ['greater than or equal to', 'floor', true, false],\n ['not less than', 'floor', true, false],\n ['no less than', 'floor', true, false],\n ['a minimum of', 'floor', true, false],\n ['minimum of', 'floor', true, false],\n ['more than', 'floor', true, true],\n ['greater than', 'floor', true, true],\n ['over', 'floor', true, true],\n ['above', 'floor', true, true],\n ['at most', 'ceiling', true, false],\n ['less than or equal to', 'ceiling', true, false],\n ['not more than', 'ceiling', true, false],\n ['a maximum of', 'ceiling', true, false],\n ['maximum of', 'ceiling', true, false],\n ['no more than', 'ceiling', true, false],\n ['less than', 'ceiling', true, true],\n ['fewer than', 'ceiling', true, true],\n ['up to', 'ceiling', true, false],\n ['under', 'ceiling', true, true],\n ['below', 'ceiling', true, true],\n ['approximately', 'approx', true, false],\n ['approx.', 'approx', true, false],\n ['approx', 'approx', true, false],\n ['about', 'approx', true, false],\n ['around', 'approx', true, false],\n ['roughly', 'approx', true, false],\n]\n\nfunction bad(display: string): RangeError {\n return new RangeError(\n `claim.display must be a percent such as \"99.8%\", \"99%+\", \"<1%\", \"about 95%\", \"95-98%\" or \"1 in 10,000\"; got \"${clip(display)}\"`,\n )\n}\n\nfunction readNum(text: string, display: string): Num {\n const [whole = '', frac = ''] = text.split('.')\n if (Number(whole) > 100 || (Number(whole) === 100 && /[1-9]/.test(frac))) {\n throw new RangeError(`claim.display must be between 0% and 100%; got \"${clip(display)}\"`)\n }\n return { whole, frac, digits: BigInt(`${whole}${frac}`), decimals: frac.length }\n}\n\nfunction proportionOf(num: Num): number {\n // Parsing \"99.8e-2\" is correctly rounded; dividing 99.8 by 100 is not always.\n return Number(`${num.frac === '' ? num.whole : `${num.whole}.${num.frac}`}e-2`)\n}\n\nfunction numLess(a: Num, b: Num): boolean {\n const scale = Math.max(a.decimals, b.decimals)\n return a.digits * 10n ** BigInt(scale - a.decimals) < b.digits * 10n ** BigInt(scale - b.decimals)\n}\n\n/** A half-integer as text: 99.5, 100. `twice` is the value times two. */\nfunction halves(twice: bigint): string {\n return twice % 2n === 0n ? String(twice / 2n) : `${String(twice / 2n)}.5`\n}\n\nfunction oneInN(text: string, kind: 'percent' | 'approx' | 'floor' | 'ceiling', strict: boolean, folded: string, display: string): DisplayFigure {\n const digits = text.replace(/,/g, '')\n const big = BigInt(digits)\n const bound = kind === 'floor' || kind === 'ceiling' ? kind : null\n if (kind === 'floor' && strict && big === 1n) {\n // nothing is above 100%\n throw new RangeError(`claim.display \"${clip(display)}\" asks for more than 100%, which is impossible`)\n }\n const approximate = kind === 'approx'\n const zeros = digits.length - digits.replace(/0+$/, '').length\n const placeholder = 10n ** BigInt(zeros)\n // twice the half-width of the band around N: 1 for an exact N, 10^zeros for an approximate one\n const unit = approximate ? placeholder : 1n\n const n2 = 2n * big\n const figure = 1 / Number(digits)\n const shown = `\"${clip(folded, 30)}\"`\n const band = `1 in ${halves(n2 - unit)} to 1 in ${halves(n2 + unit)}`\n const widen = zeros > 0 ? `; write \"about 1 in ${digits}\" to allow 1 in ${halves(n2 - placeholder)} to 1 in ${halves(n2 + placeholder)}` : ''\n // \"1 in 1\" is 100%: an absolute claim, never a band (1 in 0.5 to 1 in 1.5 would take in any rate from 66.7% up)\n const everything = big === 1n && bound === null\n const bandNote =\n bound !== null\n ? null\n : everything\n ? `${shown} is read as 100%, an absolute claim that matches only n of n.`\n : approximate\n ? `${shown} is read as ${band}.`\n : `${shown} is read as exactly 1 in ${digits} (${band})${widen}.`\n const within = (k: number, n: number): boolean =>\n everything ? k === n : BigInt(k) * (n2 + unit) >= 2n * BigInt(n) && BigInt(k) * (n2 - unit) <= 2n * BigInt(n)\n const consistentWith = (value: number): boolean => {\n if (everything) return snapAbsolute(value) === 1\n // 1/(N + h) <= value <= 1/(N - h), with one ulp of slack each way\n const { mantissa, exponent } = splitDouble(value)\n if (exponent >= 0) return false\n const d = 1n << BigInt(-exponent)\n return (mantissa + 1n) * (n2 + unit) >= 2n * d && (mantissa === 0n ? -1n : mantissa - 1n) * (n2 - unit) <= 2n * d\n }\n const satisfiedBy = (k: number, n: number): boolean => {\n if (bound === null) return within(k, n)\n // k / n against 1 / N, by integer arithmetic: floor 1/N <= k/n is k N >= n\n const lhs = BigInt(k) * big - BigInt(n)\n if (bound === 'floor') return strict ? lhs > 0n : lhs >= 0n\n return strict ? lhs < 0n : lhs <= 0n\n }\n const agreesWith = (value: number): boolean => {\n if (bound === 'floor') return strict ? beyondNoise(value, figure, true) : !beyondNoise(figure, value, true)\n if (bound === 'ceiling') return strict ? beyondNoise(value, figure, false) : !beyondNoise(figure, value, false)\n return consistentWith(value)\n }\n return {\n info: { form: bound ?? 'one-in-n', figure, decimals: null },\n bound,\n strict: bound !== null && strict,\n bandNote,\n mismatchHint: everything ? 'the value must be 1 (100%), which is what \"1 in 1\" means' : `the value must lie inside the band the display is read at (${band})`,\n consistentWith,\n agreesWith,\n matchesObserved: (k, n) => bound === null && within(k, n),\n satisfiedBy,\n }\n}\n\n/**\n * Parses the display for a metric where higher is better (accuracy, precision,\n * recall) or lower is better (error rates). Throws TypeError for a non-string\n * and RangeError for text that is not one of the forms above, or a bound that\n * points the wrong way for the metric (\"<99%\" accuracy).\n */\nexport function parseDisplayFigure(display: unknown, higherIsBetter: boolean): DisplayFigure {\n if (typeof display !== 'string') throw new TypeError('claim.display must be a string')\n if (display.length > 64) throw new RangeError('claim.display is too long to be a percent figure')\n const folded = foldText(display)\n let t = folded\n if (t.endsWith('.')) t = t.slice(0, -1)\n\n let kind: 'percent' | 'floor' | 'ceiling' | 'approx' = 'percent'\n let strict = false\n for (const [prefix, prefixKind, needsSpace, prefixStrict] of PREFIXES) {\n if (t.startsWith(prefix)) {\n let rest = t.slice(prefix.length)\n if (needsSpace) {\n if (!rest.startsWith(' ')) continue\n rest = rest.slice(1)\n } else if (rest.startsWith(' ')) {\n rest = rest.slice(1)\n }\n kind = prefixKind\n strict = prefixStrict\n t = rest\n break\n }\n }\n\n // \"95% or higher\" is \"at least 95%\": a bound written after the figure. It does\n // not combine with a word in front, and on \"1 in N\" it would be ambiguous\n // (\"1 in 100 or lower\": a lower rate, or a lower N?), so those are not read.\n const suffix = BOUND_SUFFIX.exec(t)\n if (suffix !== null) {\n if (kind !== 'percent' || ONE_IN_N.test(suffix[1] as string)) throw bad(display)\n kind = FLOOR_SUFFIXES.includes(suffix[2] as string) ? 'floor' : 'ceiling'\n strict = false\n t = suffix[1] as string\n }\n\n // \"1 in N\": 1/N. N is exact (plus or minus one half) unless the figure is\n // written as approximate (\"about 1 in 100\"), when the trailing zeros of N are\n // placeholders: 100 means 50 to 150. A bound on it (\"fewer than 1 in 1,000\")\n // is a bound at 1/N.\n const one = ONE_IN_N.exec(t)\n if (one !== null) {\n if (kind === 'floor' && !higherIsBetter) throw new RangeError(`claim.display \"${clip(display)}\" is a lower bound, which does not fit a metric where lower is better`)\n if (kind === 'ceiling' && higherIsBetter) throw new RangeError(`claim.display \"${clip(display)}\" is an upper bound, which does not fit a metric where higher is better`)\n return oneInN(one[1] as string, kind, strict, folded, display)\n }\n\n if (kind === 'percent') {\n const range = RANGE.exec(t)\n if (range !== null) {\n const low = readNum(range[1] as string, display)\n const high = readNum(range[2] as string, display)\n if (numLess(high, low)) throw bad(display)\n const end = higherIsBetter ? high : low\n return {\n info: { form: 'range', figure: proportionOf(end), decimals: end.decimals },\n bound: null,\n strict: false,\n bandNote: null,\n mismatchHint: 'the value must lie inside the range',\n consistentWith(value: number): boolean {\n // inside the range, with only one ulp of representation error at each end\n return atLeastDecimal(value, low.digits, low.decimals + 2) && atMostDecimal(value, high.digits, high.decimals + 2)\n },\n agreesWith(value: number): boolean {\n return this.consistentWith(value)\n },\n matchesObserved: (k, n) => matchesCount(end.digits, end.decimals + 2, k, n),\n // a range is judged at its favorable end, as a point\n satisfiedBy: (k, n) => matchesCount(end.digits, end.decimals + 2, k, n),\n }\n }\n // \"99+%\" is \"99%+\"\n const plus = PLUS_BEFORE_PERCENT.exec(t)\n if (plus !== null) {\n kind = 'floor'\n t = `${plus[1] as string}%`\n } else if (t.endsWith('+')) {\n kind = 'floor'\n t = t.slice(0, -1)\n }\n }\n\n const single = SINGLE.exec(t)\n if (single === null) throw bad(display)\n const num = readNum(single[1] as string, display)\n const form = kind\n if (form === 'floor' && !higherIsBetter) throw new RangeError(`claim.display \"${clip(display)}\" is a lower bound, which does not fit a metric where lower is better`)\n if (form === 'ceiling' && higherIsBetter) throw new RangeError(`claim.display \"${clip(display)}\" is an upper bound, which does not fit a metric where higher is better`)\n const scale = num.decimals + 2\n const isBound = form === 'floor' || form === 'ceiling'\n if (isBound && strict && ((form === 'floor' && num.digits === 10n ** BigInt(scale)) || (form === 'ceiling' && num.digits === 0n))) {\n // nothing is above 100% or below 0%\n throw new RangeError(`claim.display \"${clip(display)}\" asks for more than 100% or less than 0%, which is impossible`)\n }\n return {\n info: { form, figure: proportionOf(num), decimals: num.decimals },\n bound: isBound ? form : null,\n strict: isBound && strict,\n bandNote: null,\n mismatchHint: `they may differ by at most half a unit of the last digit printed (${(0.5 / 10 ** num.decimals).toFixed(num.decimals + 1)} percentage points)`,\n consistentWith: (value) => withinHalfUnit(value, num.digits, scale, scale),\n agreesWith(value: number): boolean {\n const shown = proportionOf(num)\n if (form === 'floor') return strict ? beyondNoise(value, shown, true) : !beyondNoise(shown, value, true)\n if (form === 'ceiling') return strict ? beyondNoise(value, shown, false) : !beyondNoise(shown, value, false)\n // a printed 100% or 0% is absolute: it is the figure only for a value that is exactly 1 or 0\n // (within the value noise), never for 99.6% that merely rounds to it (AGENTS.md: absolute claims).\n // A figure written as approximate (\"about 100%\") keeps its rounding band on purpose, in the text scan:\n // it does not claim certainty, and the README says so.\n if (form === 'percent' && (num.digits === 0n || num.digits === 10n ** BigInt(scale))) return snapAbsolute(value) === shown\n return withinHalfUnit(value, num.digits, scale, scale)\n },\n matchesObserved: (k, n) => matchesCount(num.digits, scale, k, n),\n satisfiedBy(k, n): boolean {\n const lhs = num.digits * BigInt(n) - BigInt(k) * 10n ** BigInt(scale)\n if (form === 'floor') return strict ? lhs < 0n : lhs <= 0n // figure < or <= k/n\n if (form === 'ceiling') return strict ? lhs > 0n : lhs >= 0n // figure > or >= k/n\n return matchesCount(num.digits, scale, k, n)\n },\n }\n}\n\n\n// ---- figures in the claim's own wording ---------------------------------\n\nconst escapeRegex = (text: string): string => text.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&')\nconst PREFIX_ALTERNATIVES = PREFIXES.map(([text]) => text)\n .sort((a, b) => b.length - a.length)\n .map(escapeRegex)\n .join('|')\nconst TEXT_NUM = '\\\\d{1,3}(?:\\\\.\\\\d{1,15})?'\n/** A printed figure inside prose: an optional bound or approximation word, then a percent, a range or \"1 in N\". */\nconst TEXT_FIGURE = new RegExp(\n `(?<![\\\\p{L}\\\\p{N}.])(?:(?:${PREFIX_ALTERNATIVES}) ?)?(?:${TEXT_NUM}(?: ?%)? ?(?:-|to) ?${TEXT_NUM} ?%|${TEXT_NUM} ?\\\\+? ?%\\\\+?(?: or (?:higher|more|above|greater|lower|less|below))?|1 in (?:[1-9]\\\\d{0,2}(?:,\\\\d{3})+|[1-9]\\\\d{0,14}))(?![\\\\p{N}])`,\n 'gu',\n)\nconst ABSOLUTE_WORDS = /(?<![\\p{L}\\p{N}])(?:perfect|flawless|error-free|no errors|zero errors)(?![\\p{L}\\p{N}])/u\n/** Most figures read from one text; each is cheap, but the count is bounded anyway. */\nconst MAX_TEXT_FIGURES = 50\n\n/**\n * The figures in `text` that say something other than the judged figure, as\n * written (folded). A figure is read by the same parser as `claim.display`;\n * one that cannot be read (a bound pointing the wrong way for the metric, an\n * impossible percent) is skipped. A figure agrees when it describes `value` or\n * `assessed` (the value, or the printed display that was judged instead), or,\n * for a point, approximate or range figure, their complement (\"87.7% right\"\n * beside a 12.3% error rate). A printed 100% or 0% is absolute: it describes\n * only a value of exactly 1 or 0, never 99.6% that rounds to it. The words perfect, flawless, error-free, no\n * errors and zero errors mean 100% (0% for a lower-is-better metric). The\n * text is only scanned, never judged: nothing here changes a verdict.\n */\nexport function textFiguresDiffering(text: string, value: number, assessed: number, higherIsBetter: boolean): string[] {\n // \"99.9 percent\" is the figure \"99.9%\"\n // The Arabic percent sign (U+066A) has no compatibility fold to \"%\"; it is the same sign.\n const folded = foldText(text)\n .replace(/\\u066A/g, '%')\n .replace(/(\\d) ?(?:percent|per cent|pct)(?![\\p{L}\\p{N}])/gu, '$1%')\n const differing: string[] = []\n let seen = 0\n for (const match of folded.matchAll(TEXT_FIGURE)) {\n if (seen >= MAX_TEXT_FIGURES) break\n seen += 1\n const snippet = match[0]\n let figure: DisplayFigure\n try {\n figure = parseDisplayFigure(snippet, higherIsBetter)\n } catch {\n continue\n }\n let agrees = figure.agreesWith(value) || figure.agreesWith(assessed)\n if (!agrees && figure.bound === null) agrees = figure.agreesWith(1 - value) || figure.agreesWith(1 - assessed)\n if (!agrees) differing.push(snippet)\n }\n const absolute = ABSOLUTE_WORDS.exec(folded)\n if (absolute !== null) {\n const exact = higherIsBetter ? 1 : 0\n if (value !== exact && assessed !== exact) differing.push(absolute[0])\n }\n return differing\n}\n"]}
|
package/dist/exact.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/** x = mantissa * 2^exponent exactly, for a finite x >= 0. */
|
|
2
|
+
export declare function splitDouble(x: number): {
|
|
3
|
+
mantissa: bigint;
|
|
4
|
+
exponent: number;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* True when |x - D / 10^scale| <= 1/2 * 10^-places + 1 ulp of x, judged on the
|
|
8
|
+
* exact value of the double x. `digits` is D as a BigInt and the printed figure
|
|
9
|
+
* is D / 10^scale; `places` is the decimal place of the printed last digit (so
|
|
10
|
+
* half a unit of it is 0.5 * 10^-places). The one-ulp allowance is the spacing
|
|
11
|
+
* of doubles at x (2^exponent of its mantissa), so a value that is the double
|
|
12
|
+
* nearest to a figure a person rounded by hand is never rejected for the
|
|
13
|
+
* double's own representation error, while a real gap many ulps wide still is.
|
|
14
|
+
* No floating-point tolerance is involved.
|
|
15
|
+
*/
|
|
16
|
+
export declare function withinHalfUnit(x: number, digits: bigint, scale: number, places: number): boolean;
|
|
17
|
+
/** True when x + 1 ulp >= digits / 10^scale (x is at or above the decimal, up to representation error). */
|
|
18
|
+
export declare function atLeastDecimal(x: number, digits: bigint, scale: number): boolean;
|
|
19
|
+
/** True when x - 1 ulp <= digits / 10^scale (x is at or below the decimal, up to representation error). */
|
|
20
|
+
export declare function atMostDecimal(x: number, digits: bigint, scale: number): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* How far a claimed value may sit from the decimal or the rate it was computed
|
|
23
|
+
* as, and still be that figure: 2^-50, about 8.9e-16. A double built as
|
|
24
|
+
* 1 - correct / n, (k / n * 100) / 100 or the mean of ten folds carries a few
|
|
25
|
+
* units of rounding error in the last place of 1, and a subtraction from 1
|
|
26
|
+
* turns that into an absolute error that is hundreds of ulps when the result is
|
|
27
|
+
* small, so the allowance is absolute, not a count of ulps of the value. It is
|
|
28
|
+
* about eight times the spacing of doubles just below 1, and it is far below
|
|
29
|
+
* anything a count can resolve (it would take 1e15 items). A value within it of
|
|
30
|
+
* 0 or 1 is read as 0 or 1 (see snapAbsolute): an absolute claim is never
|
|
31
|
+
* excused as an honest rate. COMPARISON_TOLERANCE (1e-12) is the separate,
|
|
32
|
+
* wider allowance for comparing a claim with an interval limit.
|
|
33
|
+
*/
|
|
34
|
+
export declare const VALUE_NOISE_EXPONENT = -50;
|
|
35
|
+
export declare const VALUE_NOISE: number;
|
|
36
|
+
/**
|
|
37
|
+
* 0 or 1 when `x` is within VALUE_NOISE of it, else `x`. A computed 100%
|
|
38
|
+
* (ten 0.1s added up, 1 - 2^-52) is the absolute claim of 100%, and a computed
|
|
39
|
+
* 0% (1 - 0.9999999999999999) is the absolute claim of 0%.
|
|
40
|
+
*/
|
|
41
|
+
export declare function snapAbsolute(x: number): number;
|
|
42
|
+
/**
|
|
43
|
+
* True when `a` is more favorable than `b` by more than VALUE_NOISE.
|
|
44
|
+
* "More favorable" is higher when `higherIsBetter`, else lower. A value built
|
|
45
|
+
* as printedPercent / 100, or as 1 - something, can sit a little off the figure
|
|
46
|
+
* it was printed as; that is the same figure, not a more favorable one.
|
|
47
|
+
*/
|
|
48
|
+
export declare function beyondNoise(a: number, b: number, higherIsBetter: boolean): boolean;
|
package/dist/exact.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// Exact views of a double, so comparisons and roundings that must be right to
|
|
2
|
+
// the last digit do not go through floating-point multiplication.
|
|
3
|
+
const SCRATCH = new DataView(new ArrayBuffer(8));
|
|
4
|
+
/** x = mantissa * 2^exponent exactly, for a finite x >= 0. */
|
|
5
|
+
export function splitDouble(x) {
|
|
6
|
+
SCRATCH.setFloat64(0, x);
|
|
7
|
+
const bits = SCRATCH.getBigUint64(0);
|
|
8
|
+
const biased = Number((bits >> 52n) & 0x7ffn);
|
|
9
|
+
const fraction = bits & ((1n << 52n) - 1n);
|
|
10
|
+
return {
|
|
11
|
+
mantissa: biased === 0 ? fraction : fraction | (1n << 52n),
|
|
12
|
+
exponent: (biased === 0 ? 1 : biased) - 1075,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* True when |x - D / 10^scale| <= 1/2 * 10^-places + 1 ulp of x, judged on the
|
|
17
|
+
* exact value of the double x. `digits` is D as a BigInt and the printed figure
|
|
18
|
+
* is D / 10^scale; `places` is the decimal place of the printed last digit (so
|
|
19
|
+
* half a unit of it is 0.5 * 10^-places). The one-ulp allowance is the spacing
|
|
20
|
+
* of doubles at x (2^exponent of its mantissa), so a value that is the double
|
|
21
|
+
* nearest to a figure a person rounded by hand is never rejected for the
|
|
22
|
+
* double's own representation error, while a real gap many ulps wide still is.
|
|
23
|
+
* No floating-point tolerance is involved.
|
|
24
|
+
*/
|
|
25
|
+
export function withinHalfUnit(x, digits, scale, places) {
|
|
26
|
+
// Work in units of 10^-scale and double everything to keep 1/2 an integer:
|
|
27
|
+
// 2 |x * 10^scale - D| <= 10^(scale - places) + 2 * ulp * 10^scale
|
|
28
|
+
const { mantissa, exponent } = splitDouble(x);
|
|
29
|
+
const ten = 10n ** BigInt(scale);
|
|
30
|
+
const half = 10n ** BigInt(scale - places); // twice the half-unit
|
|
31
|
+
if (exponent >= 0) {
|
|
32
|
+
const lhs = 2n * mantissa * (1n << BigInt(exponent)) * ten - 2n * digits;
|
|
33
|
+
return (lhs < 0n ? -lhs : lhs) <= half + 2n * (1n << BigInt(exponent)) * ten;
|
|
34
|
+
}
|
|
35
|
+
const s = 1n << BigInt(-exponent); // 1 / ulp
|
|
36
|
+
const lhs = 2n * mantissa * ten - 2n * digits * s;
|
|
37
|
+
return (lhs < 0n ? -lhs : lhs) <= half * s + 2n * ten;
|
|
38
|
+
}
|
|
39
|
+
/** True when x + 1 ulp >= digits / 10^scale (x is at or above the decimal, up to representation error). */
|
|
40
|
+
export function atLeastDecimal(x, digits, scale) {
|
|
41
|
+
const { mantissa, exponent } = splitDouble(x);
|
|
42
|
+
const ten = 10n ** BigInt(scale);
|
|
43
|
+
if (exponent >= 0)
|
|
44
|
+
return ((mantissa + 1n) << BigInt(exponent)) * ten >= digits;
|
|
45
|
+
return (mantissa + 1n) * ten >= digits * (1n << BigInt(-exponent));
|
|
46
|
+
}
|
|
47
|
+
/** True when x - 1 ulp <= digits / 10^scale (x is at or below the decimal, up to representation error). */
|
|
48
|
+
export function atMostDecimal(x, digits, scale) {
|
|
49
|
+
const { mantissa, exponent } = splitDouble(x);
|
|
50
|
+
const ten = 10n ** BigInt(scale);
|
|
51
|
+
if (mantissa === 0n)
|
|
52
|
+
return true;
|
|
53
|
+
if (exponent >= 0)
|
|
54
|
+
return ((mantissa - 1n) << BigInt(exponent)) * ten <= digits;
|
|
55
|
+
return (mantissa - 1n) * ten <= digits * (1n << BigInt(-exponent));
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* How far a claimed value may sit from the decimal or the rate it was computed
|
|
59
|
+
* as, and still be that figure: 2^-50, about 8.9e-16. A double built as
|
|
60
|
+
* 1 - correct / n, (k / n * 100) / 100 or the mean of ten folds carries a few
|
|
61
|
+
* units of rounding error in the last place of 1, and a subtraction from 1
|
|
62
|
+
* turns that into an absolute error that is hundreds of ulps when the result is
|
|
63
|
+
* small, so the allowance is absolute, not a count of ulps of the value. It is
|
|
64
|
+
* about eight times the spacing of doubles just below 1, and it is far below
|
|
65
|
+
* anything a count can resolve (it would take 1e15 items). A value within it of
|
|
66
|
+
* 0 or 1 is read as 0 or 1 (see snapAbsolute): an absolute claim is never
|
|
67
|
+
* excused as an honest rate. COMPARISON_TOLERANCE (1e-12) is the separate,
|
|
68
|
+
* wider allowance for comparing a claim with an interval limit.
|
|
69
|
+
*/
|
|
70
|
+
export const VALUE_NOISE_EXPONENT = -50;
|
|
71
|
+
export const VALUE_NOISE = 2 ** VALUE_NOISE_EXPONENT;
|
|
72
|
+
/**
|
|
73
|
+
* 0 or 1 when `x` is within VALUE_NOISE of it, else `x`. A computed 100%
|
|
74
|
+
* (ten 0.1s added up, 1 - 2^-52) is the absolute claim of 100%, and a computed
|
|
75
|
+
* 0% (1 - 0.9999999999999999) is the absolute claim of 0%.
|
|
76
|
+
*/
|
|
77
|
+
export function snapAbsolute(x) {
|
|
78
|
+
if (Math.abs(x) <= VALUE_NOISE)
|
|
79
|
+
return 0;
|
|
80
|
+
if (Math.abs(1 - x) <= VALUE_NOISE)
|
|
81
|
+
return 1;
|
|
82
|
+
return x;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* True when `a` is more favorable than `b` by more than VALUE_NOISE.
|
|
86
|
+
* "More favorable" is higher when `higherIsBetter`, else lower. A value built
|
|
87
|
+
* as printedPercent / 100, or as 1 - something, can sit a little off the figure
|
|
88
|
+
* it was printed as; that is the same figure, not a more favorable one.
|
|
89
|
+
*/
|
|
90
|
+
export function beyondNoise(a, b, higherIsBetter) {
|
|
91
|
+
const lead = higherIsBetter ? a : b;
|
|
92
|
+
const trail = higherIsBetter ? b : a;
|
|
93
|
+
if (!(lead > trail))
|
|
94
|
+
return false;
|
|
95
|
+
// exact: both doubles and the noise in units of 2^-p, so no rounding in the subtraction
|
|
96
|
+
const x = splitDouble(lead);
|
|
97
|
+
const y = splitDouble(trail);
|
|
98
|
+
const p = Math.max(-x.exponent, -y.exponent, -VALUE_NOISE_EXPONENT);
|
|
99
|
+
const gap = (x.mantissa << BigInt(x.exponent + p)) - (y.mantissa << BigInt(y.exponent + p));
|
|
100
|
+
return gap > 1n << BigInt(p + VALUE_NOISE_EXPONENT);
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=exact.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exact.js","sourceRoot":"","sources":["../src/exact.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,kEAAkE;AAElE,MAAM,OAAO,GAAG,IAAI,QAAQ,CAAC,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC,CAAA;AAEhD,8DAA8D;AAC9D,MAAM,UAAU,WAAW,CAAC,CAAS;IACnC,OAAO,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;IACxB,MAAM,IAAI,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC,CAAA;IACpC,MAAM,MAAM,GAAG,MAAM,CAAC,CAAC,IAAI,IAAI,GAAG,CAAC,GAAG,MAAM,CAAC,CAAA;IAC7C,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,CAAC,EAAE,IAAI,GAAG,CAAC,GAAG,EAAE,CAAC,CAAA;IAC1C,OAAO;QACL,QAAQ,EAAE,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,GAAG,CAAC,EAAE,IAAI,GAAG,CAAC;QAC1D,QAAQ,EAAE,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,IAAI;KAC7C,CAAA;AACH,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,cAAc,CAAC,CAAS,EAAE,MAAc,EAAE,KAAa,EAAE,MAAc;IACrF,2EAA2E;IAC3E,mEAAmE;IACnE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,W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W,CAAC,CAAS,EAAE,CAAS,EAAE,cAAuB;IACvE,MAAM,IAAI,GAAG,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IACnC,MAAM,KAAK,GAAG,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IACpC,IAAI,CAAC,CAAC,IAAI,GAAG,KAAK,CAAC;QAAE,OAAO,KAAK,CAAA;IACjC,wFAAwF;IACxF,MAAM,CAAC,GAAG,WAAW,CAAC,IAAI,CAAC,CAAA;IAC3B,MAAM,CAAC,GAAG,WAAW,CAAC,KAAK,CAAC,CAAA;IAC5B,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,oBAAoB,CAAC,CAAA;IACnE,MAAM,GAAG,GAAG,CAAC,CAAC,CAAC,QAAQ,IAAI,MAAM,CAAC,CAAC,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,QAAQ,IAAI,MAAM,CAAC,CAAC,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC,CAAA;IAC3F,OAAO,GAAG,GAAG,EAAE,IAAI,MAAM,CAAC,CAAC,GAAG,oBAAoB,CAAC,CAAA;AACrD,CAAC","sourcesContent":["// Exact views of a double, so comparisons and roundings that must be right to\n// the last digit do not go through floating-point multiplication.\n\nconst SCRATCH = new DataView(new ArrayBuffer(8))\n\n/** x = mantissa * 2^exponent exactly, for a finite x >= 0. */\nexport function splitDouble(x: number): { mantissa: bigint; exponent: number } {\n SCRATCH.setFloat64(0, x)\n const bits = SCRATCH.getBigUint64(0)\n const biased = Number((bits >> 52n) & 0x7ffn)\n const fraction = bits & ((1n << 52n) - 1n)\n return {\n mantissa: biased === 0 ? fraction : fraction | (1n << 52n),\n exponent: (biased === 0 ? 1 : biased) - 1075,\n }\n}\n\n/**\n * True when |x - D / 10^scale| <= 1/2 * 10^-places + 1 ulp of x, judged on the\n * exact value of the double x. `digits` is D as a BigInt and the printed figure\n * is D / 10^scale; `places` is the decimal place of the printed last digit (so\n * half a unit of it is 0.5 * 10^-places). The one-ulp allowance is the spacing\n * of doubles at x (2^exponent of its mantissa), so a value that is the double\n * nearest to a figure a person rounded by hand is never rejected for the\n * double's own representation error, while a real gap many ulps wide still is.\n * No floating-point tolerance is involved.\n */\nexport function withinHalfUnit(x: number, digits: bigint, scale: number, places: number): boolean {\n // Work in units of 10^-scale and double everything to keep 1/2 an integer:\n // 2 |x * 10^scale - D| <= 10^(scale - places) + 2 * ulp * 10^scale\n const { mantissa, exponent } = splitDouble(x)\n const ten = 10n ** BigInt(scale)\n const half = 10n ** BigInt(scale - places) // twice the half-unit\n if (exponent >= 0) {\n const lhs = 2n * mantissa * (1n << BigInt(exponent)) * ten - 2n * digits\n return (lhs < 0n ? -lhs : lhs) <= half + 2n * (1n << BigInt(exponent)) * ten\n }\n const s = 1n << BigInt(-exponent) // 1 / ulp\n const lhs = 2n * mantissa * ten - 2n * digits * s\n return (lhs < 0n ? -lhs : lhs) <= half * s + 2n * ten\n}\n\n/** True when x + 1 ulp >= digits / 10^scale (x is at or above the decimal, up to representation error). */\nexport function atLeastDecimal(x: number, digits: bigint, scale: number): boolean {\n const { mantissa, exponent } = splitDouble(x)\n const ten = 10n ** BigInt(scale)\n if (exponent >= 0) return ((mantissa + 1n) << BigInt(exponent)) * ten >= digits\n return (mantissa + 1n) * ten >= digits * (1n << BigInt(-exponent))\n}\n\n/** True when x - 1 ulp <= digits / 10^scale (x is at or below the decimal, up to representation error). */\nexport function atMostDecimal(x: number, digits: bigint, scale: number): boolean {\n const { mantissa, exponent } = splitDouble(x)\n const ten = 10n ** BigInt(scale)\n if (mantissa === 0n) return true\n if (exponent >= 0) return ((mantissa - 1n) << BigInt(exponent)) * ten <= digits\n return (mantissa - 1n) * ten <= digits * (1n << BigInt(-exponent))\n}\n\n/**\n * How far a claimed value may sit from the decimal or the rate it was computed\n * as, and still be that figure: 2^-50, about 8.9e-16. A double built as\n * 1 - correct / n, (k / n * 100) / 100 or the mean of ten folds carries a few\n * units of rounding error in the last place of 1, and a subtraction from 1\n * turns that into an absolute error that is hundreds of ulps when the result is\n * small, so the allowance is absolute, not a count of ulps of the value. It is\n * about eight times the spacing of doubles just below 1, and it is far below\n * anything a count can resolve (it would take 1e15 items). A value within it of\n * 0 or 1 is read as 0 or 1 (see snapAbsolute): an absolute claim is never\n * excused as an honest rate. COMPARISON_TOLERANCE (1e-12) is the separate,\n * wider allowance for comparing a claim with an interval limit.\n */\nexport const VALUE_NOISE_EXPONENT = -50\nexport const VALUE_NOISE = 2 ** VALUE_NOISE_EXPONENT\n\n/**\n * 0 or 1 when `x` is within VALUE_NOISE of it, else `x`. A computed 100%\n * (ten 0.1s added up, 1 - 2^-52) is the absolute claim of 100%, and a computed\n * 0% (1 - 0.9999999999999999) is the absolute claim of 0%.\n */\nexport function snapAbsolute(x: number): number {\n if (Math.abs(x) <= VALUE_NOISE) return 0\n if (Math.abs(1 - x) <= VALUE_NOISE) return 1\n return x\n}\n\n/**\n * True when `a` is more favorable than `b` by more than VALUE_NOISE.\n * \"More favorable\" is higher when `higherIsBetter`, else lower. A value built\n * as printedPercent / 100, or as 1 - something, can sit a little off the figure\n * it was printed as; that is the same figure, not a more favorable one.\n */\nexport function beyondNoise(a: number, b: number, higherIsBetter: boolean): boolean {\n const lead = higherIsBetter ? a : b\n const trail = higherIsBetter ? b : a\n if (!(lead > trail)) return false\n // exact: both doubles and the noise in units of 2^-p, so no rounding in the subtraction\n const x = splitDouble(lead)\n const y = splitDouble(trail)\n const p = Math.max(-x.exponent, -y.exponent, -VALUE_NOISE_EXPONENT)\n const gap = (x.mantissa << BigInt(x.exponent + p)) - (y.mantissa << BigInt(y.exponent + p))\n return gap > 1n << BigInt(p + VALUE_NOISE_EXPONENT)\n}\n"]}
|
package/dist/format.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** A proportion as a percent, rounded down to `decimals` places. */
|
|
2
|
+
export declare function pctFloor(proportion: number, decimals: number): string;
|
|
3
|
+
/** A proportion as a percent, rounded up to `decimals` places. */
|
|
4
|
+
export declare function pctCeil(proportion: number, decimals: number): string;
|
|
5
|
+
/** Nearest, for figures that are not bounds (the claim itself, a width). */
|
|
6
|
+
export declare function pctNearest(proportion: number, decimals: number): string;
|
|
7
|
+
/**
|
|
8
|
+
* A number to `decimals` places, cut in a chosen direction instead of rounded to
|
|
9
|
+
* the nearest, from the digits it is written with (0.57 is 0.57, not 0.5699...):
|
|
10
|
+
* 'down' never shows more than the number (0.9996 is 0.99, -0.456 is -0.46),
|
|
11
|
+
* 'up' never shows less (a required minimum of 0.8004 is 0.801).
|
|
12
|
+
*/
|
|
13
|
+
export declare function decimalCut(x: number, decimals: number, direction: 'down' | 'up'): string;
|
|
14
|
+
/**
|
|
15
|
+
* A confidence level as a percent label, rounded down to at most 9 decimals
|
|
16
|
+
* and never 100%. It works on the shortest decimal text of the number within
|
|
17
|
+
* float noise (0.95 reads "95%", not "94.999999999%", and 1 - 0.07 reads "93%"),
|
|
18
|
+
* so a level a person typed or computed keeps the digits they meant.
|
|
19
|
+
*/
|
|
20
|
+
export declare function confidenceLabel(confidence: number): string;
|
|
21
|
+
/**
|
|
22
|
+
* Most decimals a percent figure can print before its last digit is finer
|
|
23
|
+
* than one tenth of the interval width. A figure with D decimals has a last
|
|
24
|
+
* digit worth 10^-D percentage points; it is supported while
|
|
25
|
+
* 10^-D >= (width in percentage points) / 10. Whole-percent figures (0
|
|
26
|
+
* decimals) are always allowed. `width` is the interval width as a
|
|
27
|
+
* proportion (high - low).
|
|
28
|
+
*/
|
|
29
|
+
export declare function maxDisplayDecimals(width: number): number;
|