mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-10 22:41:58 +03:00
document why evalRandom/formatFraction use native float
These two are the only intentional exceptions to the decimal.js-only rule in the engine; leaving that unstated made them look like bugs during review.
This commit is contained in:
parent
0353ae7bf6
commit
744d6b61d5
2 changed files with 6 additions and 0 deletions
|
|
@ -547,6 +547,9 @@ function evalCall(name: string, args: Value[], ctx: EvalCtx): Value {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Native float, not Decimal: this is a PRNG (mulberry32/strHash bit-twiddling
|
||||||
|
// over 32-bit ints), not arithmetic on user quantities — decimal.js has no
|
||||||
|
// bitwise ops and would add nothing but overhead here.
|
||||||
function evalRandom(args: Value[], ctx: EvalCtx): Value {
|
function evalRandom(args: Value[], ctx: EvalCtx): Value {
|
||||||
const seed = strHash(ctx.line.lineText) ^ (ctx.line.index * 0x9e3779b9);
|
const seed = strHash(ctx.line.lineText) ^ (ctx.line.index * 0x9e3779b9);
|
||||||
const rand = mulberry32(seed);
|
const rand = mulberry32(seed);
|
||||||
|
|
|
||||||
|
|
@ -109,6 +109,9 @@ function withUnit(num: string, f: string): string {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Best rational approximation via continued fractions: 0.75 -> "3/4". */
|
/** Best rational approximation via continued fractions: 0.75 -> "3/4". */
|
||||||
|
// Native float, not Decimal: the continued-fraction algorithm is tuned to
|
||||||
|
// float epsilon by design, and this is display formatting, not arithmetic
|
||||||
|
// carried forward into the result.
|
||||||
function formatFraction(v: Decimal): string {
|
function formatFraction(v: Decimal): string {
|
||||||
const neg = v.isNegative();
|
const neg = v.isNegative();
|
||||||
const x = v.abs().toNumber();
|
const x = v.abs().toNumber();
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue