mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-24 03:58:12 +08:00
* Improve the displayed precision of float number labels * Show actual tiny values in high-precision number fields
16 lines
858 B
TypeScript
16 lines
858 B
TypeScript
// Recovers the intended number from floating point imprecision noise when that can be done reliably, e.g. 0.30000000000000004 -> 0.3.
|
|
// Rounding to each significant digit count from 1 to 12, the first candidate within a relative 1e-13 of the original is accepted.
|
|
// Actual high-precision values (like 0.3333333333333333) never pass the tolerance and are returned unchanged.
|
|
export function roundAwayFloatNoise(value: number): number {
|
|
if (value === 0 || !Number.isFinite(value)) return value === 0 ? 0 : value;
|
|
|
|
const exponent = Math.floor(Math.log10(Math.abs(value)));
|
|
for (let significantDigits = 1; significantDigits <= 12; significantDigits += 1) {
|
|
const scale = 10 ** (significantDigits - 1 - exponent);
|
|
const rounded = Math.round(value * scale) / scale;
|
|
if (Math.abs((rounded - value) / value) < 1e-13) return rounded;
|
|
}
|
|
|
|
return value;
|
|
}
|