MCPcopy Create free account
hub / github.com/cortex-js/compute-engine / relativeError

Function relativeError

test/big-decimal/comprehensive-benchmark.ts:64–77  ·  view source on GitHub ↗

Compute relative error: |approx - exact| / |exact|

(approx: string, exact: string)

Source from the content-addressed store, hash-verified

62
63/** Compute relative error: |approx - exact| / |exact| */
64function relativeError(approx: string, exact: string): number {
65 // Use BigDecimal at high precision for the comparison
66 const saved = BigDecimal.precision;
67 BigDecimal.precision = 1000;
68 const a = new BigDecimal(approx);
69 const e = new BigDecimal(exact);
70 if (e.isZero()) {
71 BigDecimal.precision = saved;
72 return a.isZero() ? 0 : Infinity;
73 }
74 const err = a.sub(e).abs().div(e.abs()).toNumber();
75 BigDecimal.precision = saved;
76 return err;
77}
78
79/** Derive the number of correct significant digits from relative error. */
80function correctDigits(relErr: number): number {

Callers 2

identityDigitsFunction · 0.85

Calls 6

isZeroMethod · 0.95
subMethod · 0.95
absMethod · 0.95
toNumberMethod · 0.80
divMethod · 0.65
absMethod · 0.65

Tested by

no test coverage detected