Create numbers.lisp with some basic numeric functions
[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      (while (not (null args))
22        (if (,operator x (car args))
23          (setq x    (car args)
24                args (cdr args))
25          (return-from ,operator nil)))
26      t))
27
28 (defcomparison >)
29 (defcomparison >=)
30 (defcomparison <)
31 (defcomparison <=)
32
33 (defconstant pi 3.141592653589793) 
34
35 (defun evenp (x) (= (mod x 2) 0))
36 (defun oddp  (x) (not (evenp x)))
37
38 (flet ((%max-min (x xs func)
39          (while (not (null xs))
40            (setq x  (if (funcall func x (car xs))
41                       x
42                       (car xs))
43                  xs (cdr xs)))
44          x))
45   (defun max (x &rest xs) (%max-min x xs #'>))
46   (defun min (x &rest xs) (%max-min x xs #'<))) 
47
48 (defun abs (x) (if (> x 0) x (- x)))
49
50 (defun expt (base power) (expt base              power))
51 (defun exp  (power)      (expt 2.718281828459045 power))