Skip to content

Commit 38c10de

Browse files
committed
feat(unit): numeric evaluation
1 parent 06f12d6 commit 38c10de

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

src/compute-engine/library/units.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ export const UNITS_LIBRARY: SymbolDefinitions = {
9494

9595
return ce._fn('Quantity', [mag, unitArg.canonical]);
9696
},
97-
evaluate: (ops, { engine: ce }) => {
98-
return ce._fn('Quantity', [...ops]);
97+
evaluate: (ops, { numericApproximation, engine: ce }) => {
98+
const mag = numericApproximation ? ops[0].N() : ops[0].evaluate();
99+
return ce._fn('Quantity', [mag, ops[1]]);
99100
},
100101
},
101102

test/playground.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
simplify,
66
compile,
77
parse,
8+
evaluate,
89
} from '../src/compute-engine';
910
import { expand } from '../src/compute-engine/boxed-expression/expand';
1011

@@ -13,6 +14,8 @@ const engine = ce;
1314

1415
console.log(parse('3 \\mathrm{cm} + 5\\mathrm{m}').evaluate().json);
1516

17+
console.log(evaluate('\\frac{123 \\mathrm{J}}{10\\mathrm{m}}').latex);
18+
1619
// 1. sin(theta)**2 + cos(theta)**2 → 1 — Clean trig identity, but too simple.
1720
// 2. (alpha**2 - beta**2) / (alpha - beta) → didn't simplify. Engine doesn't cancel the
1821
// difference of squares factoring here.

0 commit comments

Comments
 (0)