diff --git a/src/pages/tools/number/generic-calc/data/area_volume.ts b/src/pages/tools/number/generic-calc/data/area_volume.ts index 487b80a..8d71fa1 100644 --- a/src/pages/tools/number/generic-calc/data/area_volume.ts +++ b/src/pages/tools/number/generic-calc/data/area_volume.ts @@ -15,6 +15,7 @@ export const areaSphere: GenericCalcType = { { name: 'r', title: 'Radius', + formula: 'r = sqrt(A/pi) / 2', unit: 'mm', default: 1 } diff --git a/src/pages/tools/number/generic-calc/data/types.ts b/src/pages/tools/number/generic-calc/data/types.ts index 9742e5b..45a3d1a 100644 --- a/src/pages/tools/number/generic-calc/data/types.ts +++ b/src/pages/tools/number/generic-calc/data/types.ts @@ -36,6 +36,10 @@ export interface GenericCalcType { // If present and false, don't allow user to select this as output solvable?: boolean; + // Alternate rearrangement of the formula, to be used when calculating this. + // If missing, the main formula is used with auto derivation. + formula?: string; + // Alternates are alternate ways of entering the exact same thing, // like the diameter or radius. The formula for an alternate // can use only one variable, always called v, which is the main diff --git a/src/pages/tools/number/generic-calc/index.tsx b/src/pages/tools/number/generic-calc/index.tsx index caf99d2..9c51797 100644 --- a/src/pages/tools/number/generic-calc/index.tsx +++ b/src/pages/tools/number/generic-calc/index.tsx @@ -25,6 +25,7 @@ import 'nerdamer-prime/Algebra'; import 'nerdamer-prime/Solve'; import 'nerdamer-prime/Calculus'; import Qty from 'js-quantities'; +import { error } from 'console'; function numericSolveEquationFor( equation: string, @@ -444,10 +445,29 @@ export default async function makeTool( setResult('Please select a solve for variable'); return; } - let expr = nerdamer(calcData.formula); + let expr: nerdamer.Expression | null = null; + + for (const i of calcData.variables) { + if (i.name === values.outputVariable) { + if (i.formula !== undefined) { + expr = nerdamer(i.formula); + } + } + } + + if (expr == null) { + expr = nerdamer(calcData.formula); + } + if (expr == null) { + throw new Error('No formula found'); + return; + } Object.keys(values.vars).forEach((key) => { if (key === values.outputVariable) return; + if (expr === null) { + throw new Error('Math fail'); + } expr = expr.sub(key, values.vars[key].value.toString()); });