js-on-sx: Math.round/max/min spec edges (NaN, +/-Infinity, +/-0)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 29s

This commit is contained in:
2026-05-10 10:17:12 +00:00
parent 85414df868
commit 551c24c5a0
2 changed files with 41 additions and 7 deletions

View File

@@ -3879,45 +3879,77 @@
(define js-math-ceil (fn (x) (ceil (js-to-number x))))
(define js-math-round (fn (x) (floor (+ (js-to-number x) 0.5))))
(define
js-math-round
(fn
(x)
(let
((n (js-to-number x)))
(cond
((js-number-is-nan n) (js-nan-value))
((= n (js-infinity-value)) n)
((= n (- 0 (js-infinity-value))) n)
((= n 0) n)
(else (floor (+ n 0.5)))))))
(define
js-math-max
(fn
(&rest args)
(cond
((empty? args) -inf)
(else (js-math-max-loop (first args) (rest args))))))
((empty? args) (- 0 (js-infinity-value)))
(else (js-math-max-loop (js-to-number (first args)) (rest args))))))
(define
js-math-max-loop
(fn
(acc xs)
(cond
((js-number-is-nan acc) (js-nan-value))
((empty? xs) acc)
(else
(let
((h (js-to-number (first xs))))
(js-math-max-loop (if (> h acc) h acc) (rest xs)))))))
(cond
((js-number-is-nan h) (js-nan-value))
((> h acc) (js-math-max-loop h (rest xs)))
((and (= h 0) (= acc 0))
(js-math-max-loop (if (js-is-positive-zero? h) h acc) (rest xs)))
(else (js-math-max-loop acc (rest xs)))))))))
(define
js-math-min
(fn
(&rest args)
(cond
((empty? args) inf)
(else (js-math-min-loop (first args) (rest args))))))
((empty? args) (js-infinity-value))
(else (js-math-min-loop (js-to-number (first args)) (rest args))))))
(define
js-math-min-loop
(fn
(acc xs)
(cond
((js-number-is-nan acc) (js-nan-value))
((empty? xs) acc)
(else
(let
((h (js-to-number (first xs))))
(js-math-min-loop (if (< h acc) h acc) (rest xs)))))))
(cond
((js-number-is-nan h) (js-nan-value))
((< h acc) (js-math-min-loop h (rest xs)))
((and (= h 0) (= acc 0))
(js-math-min-loop (if (js-is-positive-zero? h) acc h) (rest xs)))
(else (js-math-min-loop acc (rest xs)))))))))
(define
js-is-positive-zero?
(fn
(n)
(cond
((not (= n 0)) false)
((= (type-of n) "rational") true)
(else (= (/ 1.0 (exact->inexact n)) (js-infinity-value))))))
(define js-math-random (fn () 0))