342c12dad8656c44125775c2cf556ed6dea2c92d
[jscl.git] / src / numbers.lisp
1 ;;; numbers.lisp
2
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.
7 ;;
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.
12 ;;
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/>.
15
16 ;;;; Various numeric functions and constants
17
18 ;; TODO: Use MACROLET when it exists
19 (defmacro defcomparison (operator)
20   `(defun ,operator (x &rest args)
21      (dolist (y args) 
22        (if (,operator x y)
23          (setq x    (car args))
24          (return-from ,operator nil)))
25      t))
26
27 (defcomparison >)
28 (defcomparison >=)
29 (defcomparison <)
30 (defcomparison <=)
31
32 (defconstant pi 3.141592653589793) 
33
34 (defun evenp (x) (= (mod x 2) 0))
35 (defun oddp  (x) (not (evenp x)))
36
37 (flet ((%max-min (x xs func)
38          (dolist (y xs) 
39            (setq x  (if (funcall func x (car xs)) x y)))
40          x))
41   (defun max (x &rest xs) (%max-min x xs #'>))
42   (defun min (x &rest xs) (%max-min x xs #'<))) 
43
44 (defun abs (x) (if (> x 0) x (- x)))
45
46 (defun expt (base power) (expt base              power))
47 (defun exp  (power)      (expt 2.718281828459045 power))