diff options
Diffstat (limited to 'module/language/lua/standard/math.scm')
-rw-r--r-- | module/language/lua/standard/math.scm | 49 |
1 files changed, 19 insertions, 30 deletions
diff --git a/module/language/lua/standard/math.scm b/module/language/lua/standard/math.scm index 91ccd174f..dd2bbbce2 100644 --- a/module/language/lua/standard/math.scm +++ b/module/language/lua/standard/math.scm @@ -21,14 +21,14 @@ (define-module (language lua standard math) #:use-module (language lua runtime)) -;; TODO: math.modf -;; TODO: math.deg,rad,frexp,random not tested +;; TODO: math.frexp ;; NOTE: as opposed to lua, math.sqrt accepts negative arguments, as ;; guile's numeric tower is capable of representing complex numbers (define huge +inf.0) (define *nan* (nan)) +;; We define some constants here to more closely match Lua's behavior (define pi 3.14159265358979323846) (define radians_per_degree (/ pi 180.0)) @@ -91,27 +91,9 @@ (define (atan2 x y) (atan (/ x y))) -;; copy the global random state for this module so we don't mutate it -(define randomstate (copy-random-state *random-state*)) - -(define (randomseed seed . _) - (set! randomstate (seed->random-state seed))) - -(define* (random #:optional m n #:rest _) - ;; this can be a little confusing because guile's random number - ;; generator only allows [0, N) but we need [0,1), [1,m] and [m,n] - (cond ((and (not m) (not n)) ((@ (guile) random) 1.0)) - ;; this is really [1,M) - ((and m) (+ 1 ((@ (guile) random) m))) - ((and m n) (+ m ((@ (guile) random) n))) - (else (error #:RANDOM "should not happen")))) - (define (deg x) (/ x radians_per_degree)) -(define (rad x) - (* x radians_per_degree)) - (define (ldexp x exp) (cond ((= exp 0) x) ((= exp *nan*) *nan*) @@ -124,13 +106,20 @@ (lambda (x) (/ (log x) log2)))) -(define (frexp x) - (if (zero? x) - 0.0 - (let* ((l2 (log2 x)) - (e (floor (log2 x))) - (e (if (= l2 e) - (inexact->exact e) - (+ (inexact->exact e) 1))) - (f (/ x (expt 2 e)))) - f))) +(define (rad x) + (* x radians_per_degree)) + +;; copy the global random state for this module so we don't mutate it +(define randomstate (copy-random-state *random-state*)) + +(define (randomseed seed . _) + (set! randomstate (seed->random-state seed))) + +(define* (random #:optional m n #:rest _) + ;; this can be a little confusing because guile's random number + ;; generator only allows [0, N) but we need [0,1), [1,m] and [m,n] + (cond ((and (not m) (not n)) ((@ (guile) random) 1.0)) + ;; this is really [1,M) + ((and m) (+ 1 ((@ (guile) random) m))) + ((and m n) (+ m ((@ (guile) random) n))) + (else (error #:RANDOM "should not happen")))) |