lua: math library (abs/trig/log/pow/min/max/fmod/modf/random/...) +17 tests
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled

This commit is contained in:
2026-04-24 18:53:28 +00:00
parent 8c25527205
commit 6dfef34a4b
3 changed files with 170 additions and 1 deletions

View File

@@ -878,3 +878,116 @@
(dict-set! string "gmatch" lua-string-gmatch)
(dict-set! string "gsub" lua-string-gsub)
(dict-set! string "format" lua-string-format)
;; ── math library ──────────────────────────────────────────────
(define math {})
(define lua-math-pi 3.141592653589793)
(define lua-math-huge (/ 1.0 0.0))
(define lua-math-abs (fn (x) (abs x)))
(define lua-math-ceil (fn (x) (ceil x)))
(define lua-math-floor (fn (x) (floor x)))
(define lua-math-sqrt (fn (x) (sqrt x)))
(define lua-math-exp (fn (x) (exp x)))
(define lua-math-sin (fn (x) (sin x)))
(define lua-math-cos (fn (x) (cos x)))
(define lua-math-tan (fn (x) (tan x)))
(define lua-math-asin (fn (x) (asin x)))
(define lua-math-acos (fn (x) (acos x)))
(define lua-math-atan (fn (x) (atan x)))
(define lua-math-atan2 (fn (y x) (atan2 y x)))
(define lua-math-pow (fn (a b) (pow a b)))
(define lua-math-log
(fn (&rest args)
(cond
((= (len args) 1) (log (first args)))
(else (/ (log (first args)) (log (nth args 1)))))))
(define lua-math-log10
(fn (x) (/ (log x) (log 10))))
(define lua-math-deg (fn (x) (* x (/ 180 lua-math-pi))))
(define lua-math-rad (fn (x) (* x (/ lua-math-pi 180))))
(define lua-math-min
(fn (&rest args)
(cond
((= (len args) 0) (error "lua: min: no values"))
((= (len args) 1) (first args))
(else
(let ((m (first args)))
(begin
(define
loop
(fn (i)
(when (< i (len args))
(begin
(when (< (nth args i) m) (set! m (nth args i)))
(loop (+ i 1))))))
(loop 1)
m))))))
(define lua-math-max
(fn (&rest args)
(cond
((= (len args) 0) (error "lua: max: no values"))
((= (len args) 1) (first args))
(else
(let ((m (first args)))
(begin
(define
loop
(fn (i)
(when (< i (len args))
(begin
(when (> (nth args i) m) (set! m (nth args i)))
(loop (+ i 1))))))
(loop 1)
m))))))
(define lua-math-fmod
(fn (a b) (- a (* b (if (> b 0) (floor (/ a b)) (ceil (/ a b)))))))
(define lua-math-modf
(fn (x)
(let ((i (if (>= x 0) (floor x) (ceil x))))
(list (quote lua-multi) i (- x i)))))
(define __rand-scale 1048576)
(define lua-math-random
(fn (&rest args)
(cond
((= (len args) 0)
(/ (random-int 0 (- __rand-scale 1)) (* 1.0 __rand-scale)))
((= (len args) 1) (random-int 1 (first args)))
(else (random-int (first args) (nth args 1))))))
(define lua-math-randomseed (fn (s) nil))
(dict-set! math "pi" lua-math-pi)
(dict-set! math "huge" lua-math-huge)
(dict-set! math "abs" lua-math-abs)
(dict-set! math "ceil" lua-math-ceil)
(dict-set! math "floor" lua-math-floor)
(dict-set! math "sqrt" lua-math-sqrt)
(dict-set! math "exp" lua-math-exp)
(dict-set! math "log" lua-math-log)
(dict-set! math "log10" lua-math-log10)
(dict-set! math "pow" lua-math-pow)
(dict-set! math "sin" lua-math-sin)
(dict-set! math "cos" lua-math-cos)
(dict-set! math "tan" lua-math-tan)
(dict-set! math "asin" lua-math-asin)
(dict-set! math "acos" lua-math-acos)
(dict-set! math "atan" lua-math-atan)
(dict-set! math "atan2" lua-math-atan2)
(dict-set! math "deg" lua-math-deg)
(dict-set! math "rad" lua-math-rad)
(dict-set! math "min" lua-math-min)
(dict-set! math "max" lua-math-max)
(dict-set! math "fmod" lua-math-fmod)
(dict-set! math "modf" lua-math-modf)
(dict-set! math "random" lua-math-random)
(dict-set! math "randomseed" lua-math-randomseed)