UNPKG

kestrel.markets

Version:

A typed, token-efficient language + runtime for agentic trading: agents author bounded plans, the runtime fires them at the tick. CLI + typed library + MCP server.

52 lines 3.21 kB
/** * # fair/detmath — vendored, bit-deterministic `exp` / `log` for the certified-grade path * * ## Why this file exists * A certified Grade must be **byte-identical across every runtime we grade on** — V8 on Node, * V8 on Cloudflare Workers, and JSC on Bun. JavaScript's `+ − × ÷` and `Math.sqrt` are pinned * by IEEE-754 (correctly-rounded binary64) and reproduce bit-for-bit everywhere. The *library* * transcendentals `Math.exp` and `Math.log` are **not** pinned — the ECMAScript spec explicitly * allows implementation-defined results, so V8 and JSC can (and do) return answers that differ * in the last ulp. On the certified-grade path that is a cross-runtime float divergence: two * engines could stamp two different grades on the same tape. * * This module removes that risk by **vendoring a fixed, portable double-precision `exp`/`log`** * (the SunPro / fdlibm `__ieee754_exp` and `__ieee754_log`, the same code musl and the BSDs * ship) reimplemented in terms of *only* the pinned IEEE-754 primitives. Because every operation * inside is a correctly-rounded binary64 op, the result is the same bits on every conforming * engine — which the checked-in cross-engine golden bit-vectors (`tests/detmath.golden.test.ts`) * assert. * * These are drop-in replacements for `Math.exp` / `Math.log` on the pricing + fill-hazard path * ({@link ../fair/black76.ts}, {@link ../fill/model.ts}). They are pure (no I/O, no clock, no * RNG — RUNTIME §0); the shared scratch buffer is single-threaded mutable state used only to read * a double's raw words, never observable output. * * ## Provenance * Ported from FreeBSD/SunPro `msun` `e_exp.c` and `e_log.c` (fdlibm). Original notice: * ``` * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. * Developed at SunPro, a Sun Microsystems, Inc. business. Permission to use, copy, modify, and * distribute this software is freely granted, provided that this notice is preserved. * ``` * The algorithm, constants and comments are Sun's; only the surface syntax is TypeScript. */ /** * Bit-deterministic `Math.exp`. Same value on every IEEE-754 engine (< 1 ulp of the true exp), * used everywhere `Math.exp` sat on the certified-grade path. Edge cases match the library: * `exp(NaN)=NaN`, `exp(+Inf)=+Inf`, `exp(-Inf)=0`, overflow → `+Inf`, underflow → `0`. */ export declare function dexp(x: number): number; /** * Bit-deterministic `Math.log` (natural log). Same value on every IEEE-754 engine (< 1 ulp), * used everywhere `Math.log` sat on the certified-grade path. Edge cases match the library: * `log(+-0)=-Inf`, `log(x<0)=NaN`, `log(NaN)=NaN`, `log(+Inf)=+Inf`. */ export declare function dlog(x: number): number; /** Raw IEEE-754 bits of a double as a stable 16-hex-digit string (big-endian, high word first). * The canonical form the cross-engine golden bit-vectors are recorded and compared in — two * engines agree iff these strings are equal. */ export declare function f64ToHex(x: number): string; /** Inverse of {@link f64ToHex}: reconstruct the double a golden hex string encodes. */ export declare function hexToF64(hex: string): number; //# sourceMappingURL=detmath.d.ts.map