share the Decimal PI constant with unitdata.ts

The degree-to-radian ratio computed via native Math.PI/180 instead of
the Decimal PI already defined for evaluator's own trig — moved PI to
types.ts so both can import it without a cycle.
This commit is contained in:
Horizontal42 2026-08-12 20:08:28 +03:00
parent d4c34bf4af
commit 7dfb58b989
3 changed files with 6 additions and 4 deletions

View file

@ -1,6 +1,6 @@
// Walks the AST with the document context (variables, line results for // Walks the AST with the document context (variables, line results for
// sum/avg/prev) and produces unit-aware values. // sum/avg/prev) and produces unit-aware values.
import { Decimal, EvalError, Quantity, Unit, Value, XRefError, qty, pct } from "./types"; import { Decimal, EvalError, PI, Quantity, Unit, Value, XRefError, qty, pct } from "./types";
import { Node, ConvTarget } from "./parser"; import { Node, ConvTarget } from "./parser";
import { Registry } from "./registry"; import { Registry } from "./registry";
import { resolveZone, startOfToday, addToDate, isCalendarUnit } from "./datetime"; import { resolveZone, startOfToday, addToDate, isCalendarUnit } from "./datetime";
@ -28,7 +28,6 @@ export interface EvalCtx {
export type XRefResolution = { ok: true; value: Value } | { ok: false; reason: string }; export type XRefResolution = { ok: true; value: Value } | { ok: false; reason: string };
export type XRefResolver = (sheet: string, key: string) => XRefResolution; export type XRefResolver = (sheet: string, key: string) => XRefResolution;
const PI = new Decimal("3.14159265358979323846264338327950288419716939937510");
const E = new Decimal("2.71828182845904523536028747135266249775724709369995"); const E = new Decimal("2.71828182845904523536028747135266249775724709369995");
export function evaluate(node: Node, ctx: EvalCtx): Value { export function evaluate(node: Node, ctx: EvalCtx): Value {

View file

@ -4,6 +4,9 @@ Decimal.set({ precision: 40, toExpNeg: -9, toExpPos: 21 });
export { Decimal }; export { Decimal };
/** Shared here (not in evaluator.ts) so unitdata.ts can use it too without a cycle. */
export const PI = new Decimal("3.14159265358979323846264338327950288419716939937510");
export type Dimension = export type Dimension =
| "scalar" | "scalar"
| "length" | "length"

View file

@ -1,7 +1,7 @@
// Conversion ratios. Base units per dimension: length=meter, area=m², // Conversion ratios. Base units per dimension: length=meter, area=m²,
// volume=m³, weight=gram, time=second, angle=radian, data=bit, // volume=m³, weight=gram, time=second, angle=radian, data=bit,
// temperature=kelvin (ratio+offset). // temperature=kelvin (ratio+offset).
import { Decimal, Dimension } from "./types"; import { Decimal, Dimension, PI } from "./types";
export interface UnitData { export interface UnitData {
id: string; id: string;
@ -79,7 +79,7 @@ export const UNIT_DATA: UnitData[] = [
// Angle (base: radian) // Angle (base: radian)
{ id: "radian", category: "Angle", dimension: "angle", ratio: "1" }, { id: "radian", category: "Angle", dimension: "angle", ratio: "1" },
{ id: "degree", category: "Angle", dimension: "angle", ratio: String(Math.PI / 180) }, { id: "degree", category: "Angle", dimension: "angle", ratio: PI.div(180).toString() },
// Data (base: bit) // Data (base: bit)
{ id: "bit", category: "Data", dimension: "data", ratio: "1", prefixes: "data" }, { id: "bit", category: "Data", dimension: "data", ratio: "1", prefixes: "data" },