js-on-sx: expanded Math + Number globals
Math gains sqrt/pow/trunc/sign/cbrt/hypot plus LN2/LN10/LOG2E/ LOG10E/SQRT2/SQRT1_2 constants and full-precision PI/E. Number global: isFinite/isNaN/isInteger/isSafeInteger plus MAX_VALUE/MIN_VALUE/MAX_SAFE_INTEGER/MIN_SAFE_INTEGER/EPSILON/ POSITIVE_INFINITY/NEGATIVE_INFINITY/NaN. Global isFinite, isNaN, Infinity, NaN. Wired into js-global. 329/331 unit (+21), 148/148 slice unchanged.
This commit is contained in:
@@ -1183,7 +1183,86 @@
|
||||
|
||||
(define js-math-random (fn () 0))
|
||||
|
||||
(define Math {:random js-math-random :floor js-math-floor :PI 3.14159 :round js-math-round :abs js-math-abs :ceil js-math-ceil :max js-math-max :min js-math-min :E 2.71828})
|
||||
(define js-math-sqrt (fn (x) (sqrt (js-to-number x))))
|
||||
|
||||
(define js-math-pow (fn (a b) (pow (js-to-number a) (js-to-number b))))
|
||||
|
||||
(define
|
||||
js-math-trunc
|
||||
(fn
|
||||
(x)
|
||||
(let ((n (js-to-number x))) (if (< n 0) (ceil n) (floor n)))))
|
||||
|
||||
(define
|
||||
js-math-sign
|
||||
(fn
|
||||
(x)
|
||||
(let
|
||||
((n (js-to-number x)))
|
||||
(cond ((> n 0) 1) ((< n 0) -1) (else n)))))
|
||||
|
||||
(define
|
||||
js-math-cbrt
|
||||
(fn
|
||||
(x)
|
||||
(let
|
||||
((n (js-to-number x)))
|
||||
(if (< n 0) (- 0 (pow (- 0 n) (/ 1 3))) (pow n (/ 1 3))))))
|
||||
|
||||
(define js-math-hypot (fn (&rest args) (sqrt (js-math-hypot-loop args 0))))
|
||||
|
||||
(define
|
||||
js-math-hypot-loop
|
||||
(fn
|
||||
(args acc)
|
||||
(if
|
||||
(empty? args)
|
||||
acc
|
||||
(let
|
||||
((n (js-to-number (first args))))
|
||||
(js-math-hypot-loop (rest args) (+ acc (* n n)))))))
|
||||
|
||||
(define Math {:random js-math-random :trunc js-math-trunc :LN10 2.30259 :SQRT1_2 0.707107 :floor js-math-floor :PI 3.14159 :sqrt js-math-sqrt :hypot js-math-hypot :LOG2E 1.4427 :round js-math-round :ceil js-math-ceil :abs js-math-abs :pow js-math-pow :max js-math-max :LOG10E 0.434294 :SQRT2 1.41421 :cbrt js-math-cbrt :min js-math-min :sign js-math-sign :E 2.71828 :LN2 0.693147})
|
||||
|
||||
(define
|
||||
js-number-is-finite
|
||||
(fn
|
||||
(v)
|
||||
(and
|
||||
(number? v)
|
||||
(not (js-number-is-nan v))
|
||||
(not (= v (/ 1 0)))
|
||||
(not (= v (/ -1 0))))))
|
||||
|
||||
(define js-number-is-nan (fn (v) (and (number? v) (not (= v v)))))
|
||||
|
||||
(define
|
||||
js-number-is-integer
|
||||
(fn
|
||||
(v)
|
||||
(and (number? v) (js-number-is-finite v) (= v (js-math-trunc v)))))
|
||||
|
||||
(define
|
||||
js-number-is-safe-integer
|
||||
(fn
|
||||
(v)
|
||||
(and (js-number-is-integer v) (<= (js-math-abs v) 9007199254740991))))
|
||||
|
||||
(define
|
||||
js-global-is-finite
|
||||
(fn (v) (js-number-is-finite (js-to-number v))))
|
||||
|
||||
(define js-global-is-nan (fn (v) (js-number-is-nan (js-to-number v))))
|
||||
|
||||
(define Number {:isFinite js-number-is-finite :MAX_SAFE_INTEGER 9007199254740991 :EPSILON 2.22045e-16 :MAX_VALUE 0 :POSITIVE_INFINITY inf :isInteger js-number-is-integer :isNaN js-number-is-nan :isSafeInteger js-number-is-safe-integer :NEGATIVE_INFINITY -inf :NaN 0 :MIN_VALUE 4.94066e-324 :MIN_SAFE_INTEGER -9007199254740991})
|
||||
|
||||
(define isFinite js-global-is-finite)
|
||||
|
||||
(define isNaN js-global-is-nan)
|
||||
|
||||
(define Infinity inf)
|
||||
|
||||
(define NaN 0)
|
||||
|
||||
(define __js_microtask_queue__ (dict))
|
||||
|
||||
@@ -1686,4 +1765,4 @@
|
||||
(str "/" (get rx "source") "/" (get rx "flags")))
|
||||
(else js-undefined))))
|
||||
|
||||
(define js-global {:console console :Math Math :NaN 0 :Infinity (/ 1 0) :undefined js-undefined})
|
||||
(define js-global {:isFinite js-global-is-finite :console console :Number Number :Math Math :NaN 0 :Infinity inf :isNaN js-global-is-nan :undefined js-undefined})
|
||||
|
||||
Reference in New Issue
Block a user