jolt/host/chez/math.ss
Yogthos 467ad75ff7 Chez numeric tower: exact ints / Ratio / double for JVM parity (jolt-n6al)
jolt was all-flonum (one :number type, inherited from Janet whose only number
type is a double). The Chez runtime has a full numeric tower, so the zero-Janet
path now carries it = JVM Clojure semantics:

  (/ 1 2)      => 1/2      (exact Ratio, was 0.5)
  (integer? 3) => true   (integer? 3.0) => false   (float? 3.0) => true
  (ratio? (/ 1 2)) => true   (= 3 3.0) => false   (== 3 3.0) => true
  (+ 1 2) => 3 (exact)   (/ 1.0 2) => 0.5 (double)

jolt= was already exactness-aware (values.ss) and == is value-equality, so
=/== match the JVM split. The reader preserves exactness (integer literals exact,
a/b ratios exact rationals, decimals/exponents flonums); backend_scheme emit-const
renders exact ints/ratios and flonums faithfully; the value-position arithmetic,
count, int, compare, bit ops, parseLong, string .length/.indexOf, range,
timestamps, and array bytes return exact integers (= JVM int/long) instead of
coercing to flonum. double/parseDouble/clojure.math floor|ceil|signum stay double.

Only the zero-Janet path carries the tower (the Janet reader loses exactness into
a double before emit). The prelude/all-flonum path is unaffected for compiled code;
the runtime reader is shared, so a couple of all-flonum reader assertions become
value (==) assertions. ~16 numeric corpus cases now give the JVM tower value vs the
Janet-era :expected and are allowlisted as tower divergences (Chez == reference
JVM) pending the corpus flip to JVM (jolt-ecz0). No BigDecimal type (1M).

Re-minted. zero-janet 2682 (floor 2698->2682, the reclassified tower cases), 0 new
divergences; fixpoint 10/10, bootstrap 6/6, spine 35/35, cli 49/49; Janet gate 155
files 0 failed.
2026-06-20 23:09:27 -04:00

64 lines
3 KiB
Scheme

;; clojure.math (jolt-22vo) — Chez host shim over native flonum math.
;;
;; On the Janet seed clojure.math is registered as native math/ bindings
;; (api.janet install-clojure-math!, jolt-h79), NOT a .clj file — so there's no
;; source tier to emit. Chez provides its own def-var! shims here, one per
;; clojure.math fn, over Chez's native procedures. The analyzer already knows the
;; clojure.math ns exists (init interns the same fns on the Janet side), so a ref
;; like clojure.math/sqrt lowers to a var-deref; these cells back it at runtime.
;;
;; jolt is all-flonum, so every result is a flonum (inputs arrive as flonums; Chez
;; sqrt/sin/expt/... return flonums for flonum args). Semantics match the seed
;; (Clojure 1.11 clojure.math): round = floor(x+0.5), rint = round-half-even,
;; floor/ceil/floor-div return doubles, to-degrees/to-radians via PI.
(define jolt-math-pi (acos -1.0))
(define jolt-math-e (exp 1.0))
(define (jolt-math-cbrt x)
;; sign-aware so negative inputs stay real (expt of a negative flonum to a
;; fractional power goes complex).
(if (< x 0.0)
(- (expt (- x) (/ 1.0 3.0)))
(expt x (/ 1.0 3.0))))
;; clojure.math/round returns a long (exact); floor/ceil/signum/rint return doubles.
(define (jolt-math-round x) (exact (floor (+ x 0.5))))
(define (jolt-math-signum x) (cond ((< x 0.0) -1.0) ((> x 0.0) 1.0) (else 0.0)))
(define (jolt-math-to-degrees r) (/ (* r 180.0) jolt-math-pi))
(define (jolt-math-to-radians d) (/ (* d jolt-math-pi) 180.0))
(define (jolt-math-hypot a b) (sqrt (+ (* a a) (* b b))))
(define (jolt-math-floor-div a b) (floor (/ a b)))
(define (jolt-math-floor-mod a b) (- a (* b (floor (/ a b)))))
(def-var! "clojure.math" "sqrt" sqrt)
(def-var! "clojure.math" "cbrt" jolt-math-cbrt)
(def-var! "clojure.math" "pow" expt)
(def-var! "clojure.math" "exp" exp)
(def-var! "clojure.math" "expm1" (lambda (x) (- (exp x) 1.0)))
(def-var! "clojure.math" "log" log)
(def-var! "clojure.math" "log10" (lambda (x) (log x 10.0)))
(def-var! "clojure.math" "log1p" (lambda (x) (log (+ 1.0 x))))
(def-var! "clojure.math" "sin" sin)
(def-var! "clojure.math" "cos" cos)
(def-var! "clojure.math" "tan" tan)
(def-var! "clojure.math" "asin" asin)
(def-var! "clojure.math" "acos" acos)
(def-var! "clojure.math" "atan" atan)
;; clojure.math/atan2 is atan2(y, x); Chez's 2-arg atan is (atan y x).
(def-var! "clojure.math" "atan2" (lambda (y x) (atan y x)))
(def-var! "clojure.math" "sinh" sinh)
(def-var! "clojure.math" "cosh" cosh)
(def-var! "clojure.math" "tanh" tanh)
(def-var! "clojure.math" "floor" floor)
(def-var! "clojure.math" "ceil" ceiling)
(def-var! "clojure.math" "rint" round)
(def-var! "clojure.math" "round" jolt-math-round)
(def-var! "clojure.math" "signum" jolt-math-signum)
(def-var! "clojure.math" "to-degrees" jolt-math-to-degrees)
(def-var! "clojure.math" "to-radians" jolt-math-to-radians)
(def-var! "clojure.math" "hypot" jolt-math-hypot)
(def-var! "clojure.math" "floor-div" jolt-math-floor-div)
(def-var! "clojure.math" "floor-mod" jolt-math-floor-mod)
(def-var! "clojure.math" "E" jolt-math-e)
(def-var! "clojure.math" "PI" jolt-math-pi)