3 ;; JSCL is free software: you can redistribute it and/or
4 ;; modify it under the terms of the GNU General Public License as
5 ;; published by the Free Software Foundation, either version 3 of the
6 ;; License, or (at your option) any later version.
8 ;; JSCL is distributed in the hope that it will be useful, but
9 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;; General Public License for more details.
13 ;; You should have received a copy of the GNU General Public License
14 ;; along with JSCL. If not, see <http://www.gnu.org/licenses/>.
16 ;;;; Various numeric functions and constants
18 ;; TODO: Use MACROLET when it exists
19 (defmacro define-variadic-op (operator initial-value)
20 (let ((init-sym (gensym))
21 (dolist-sym (gensym)))
22 `(defun ,operator (&rest args)
23 (let ((,init-sym ,initial-value))
24 (dolist (,dolist-sym args)
25 (setq ,init-sym (,operator ,init-sym ,dolist-sym)))
28 (define-variadic-op + 0)
29 (define-variadic-op * 1)
31 ;; - and / work differently from the above macro.
32 ;; If only one arg is given, it negates it or takes its reciprocal.
33 ;; Otherwise all the other args are subtracted from or divided by it.
34 ;; TODO: Use MACROLET when it exists
35 (defmacro define-sub-or-div (operator unary-form)
36 `(defun ,operator (x &rest args)
38 ((null args) ,unary-form)
40 (setq x (,operator x y)))
43 (define-sub-or-div - (- x))
44 (define-sub-or-div / (/ 1 x))
46 (defun 1+ (x) (+ x 1))
47 (defun 1- (x) (- x 1))
49 (defun truncate (x &optional (y 1))
53 (and (numberp x) (= (floor x) x)))
56 (and (numberp x) (not (integerp x))))
58 (defun minusp (x) (< x 0))
59 (defun zerop (x) (= x 0))
60 (defun plusp (x) (< 0 x))
62 ;; TODO: Use MACROLET when it exists
63 (defmacro defcomparison (operator)
64 `(defun ,operator (x &rest args)
68 (return-from ,operator nil)))
78 (defconstant pi 3.141592653589793)
80 (defun evenp (x) (= (mod x 2) 0))
81 (defun oddp (x) (not (evenp x)))
83 (flet ((%max-min (x xs func)
85 (setq x (if (funcall func x (car xs)) x y)))
87 (defun max (x &rest xs) (%max-min x xs #'>))
88 (defun min (x &rest xs) (%max-min x xs #'<)))
90 (defun abs (x) (if (> x 0) x (- x)))
92 (defun expt (base power) (expt base power))
93 (defun exp (power) (expt 2.718281828459045 power))