engine/bitexact_math library

Bit-exact maths for theme generation (research theme-engine.md §2.5, Recommendation 10).

dart:math sin/cos/atan2 differ in the last bits between the VM (platform libm) and the web (V8's fdlibm port) on 4–20 % of inputs, and other OS libms are untested. Everything here uses only IEEE-754 + − × ÷ (correctly rounded on every Dart backend, no FMA contraction), so outputs are identical on VM, AOT, dart2js and dart2wasm.

Constants

detPi → const double
π to double precision.
srgbDecode → const List<double>
sRGB decode: linear-light value of each 8-bit code.
srgbEncodeThreshold → const List<double>
Encode thresholds: decode((v + 0.5) / 255) for v in 0..254.

Functions

clamp01(double v) → double
Clamps v to [0, 1].
decode8(int code) → double
sRGB decode of an 8-bit code to linear light (table, correctly rounded).
detAtan2Deg(double y, double x) → double
atan2(y, x) in degrees, in [0, 360) (0 when both are 0). Deterministic; used to read a colour's OKLCH hue (e.g. for names).
detCbrt(double x) → double
Cube root, sign-matched (detCbrt(-8) == -2): range reduction by 8, then 7 Newton steps. Max relative error vs pow ≈ 3.2e-16.
detCosDeg(double deg) → double
Cosine of an angle in degrees (see detSinCosDeg).
detSinCosDeg(double deg) → (double, double)
(sin, cos) of an angle in degrees: octant reduction in degrees, then Taylor series to x¹⁷ / x¹⁸. Max absolute error ≈ 1.3e-15.
detSinDeg(double deg) → double
Sine of an angle in degrees (see detSinCosDeg).
detSqrt(double x) → double
Square root by Newton iteration (deterministic; 0 for x ≤ 0).
encode8(double x) → int
round(255 · encode(x)) for linear-light x, without pow: binary search over the 255 thresholds t_v = decode((v + 0.5)/255). Inputs outside 0, 1 clamp to 0 / 255.