Skip to content

Commit

Permalink
feat: update math cell to display user selected base units
Browse files Browse the repository at this point in the history
Still need to force recalculation when default base units are changed. Also, will need to update plot cells which will required updates to the Python code to include user base units (requires update to PlotData type).
  • Loading branch information
mgreminger committed Dec 3, 2023
1 parent 94a1241 commit eed0d0c
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/MathCell.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { isFiniteImagResult, type Result, type FiniteImagResult,
type PlotResult, type MatrixResult, isMatrixResult } from "./resultTypes";
import type { CodeFunctionQueryStatement, QueryStatement } from "./parser/types";
import { convertUnits, unitsValid } from "./utility";
import { convertUnits } from "./utility";
import type { MathCellConfig } from "./sheet/Sheet";
import type MathCell from "./cells/MathCell";
import PlotCell from "./cells/PlotCell";
Expand Down Expand Up @@ -133,13 +133,20 @@
};
}
if ( statement.units ) {
// unit conversion required to user supplied units
resultUnitsLatex = statement.unitsLatex;
resultUnits = statement.units;
if ( statement.units || result.customUnitsDefined) {
// unit conversion required to user supplied units or sheet level user default units
// user supplied units in the statement takes precedence over sheet level user default units
if (statement.units) {
resultUnitsLatex = statement.unitsLatex;
resultUnits = statement.units;
} else {
resultUnitsLatex = result.customUnitsLatex;
resultUnits = result.customUnits;
}
if (result.numeric && result.real && result.finite && !numberConfig.symbolicOutput) {
const {newValue: localNewValue, unitsMismatch: localUnitsMismatch} = convertUnits(result.value, result.units, statement.units);
const {newValue: localNewValue, unitsMismatch: localUnitsMismatch} = convertUnits(result.value, result.units, resultUnits);
if (!localUnitsMismatch) {
resultLatex = customFormat(localNewValue, numberConfig.formatOptions);
Expand All @@ -152,9 +159,9 @@
} else if (isFiniteImagResult(result) && !numberConfig.symbolicOutput) {
// handle unit conversion for imaginary number
const {newValue: newRealValue, unitsMismatch: realUnitsMismatch} =
convertUnits(result.realPart, result.units, statement.units);
convertUnits(result.realPart, result.units, resultUnits);
const {newValue: newImagValue, unitsMismatch: imagUnitsMismatch} =
convertUnits(result.imagPart, result.units, statement.units);
convertUnits(result.imagPart, result.units, resultUnits);
if (!realUnitsMismatch && !imagUnitsMismatch) {
resultLatex = formatImag(newRealValue, newImagValue, numberConfig);
Expand Down

0 comments on commit eed0d0c

Please sign in to comment.