Create numbers.lisp with some basic numeric functions
authorOwen Rodley <Strigoides@gmail.com>
Sat, 18 May 2013 13:05:54 +0000 (01:05 +1200)
committerOwen Rodley <Strigoides@gmail.com>
Sun, 19 May 2013 23:06:26 +0000 (11:06 +1200)
jscl.lisp
src/numbers.lisp [new file with mode: 0644]
tests/numbers.lisp [new file with mode: 0644]

index 5e393d6..7a5e35a 100644 (file)
--- a/jscl.lisp
+++ b/jscl.lisp
@@ -33,6 +33,7 @@
     ("package"          :target)
     ("ffi"              :target)
     ("misc"             :target)
+    ("numbers"          :target)
     ("read"             :both)
     ("defstruct"        :both)
     ("lambda-list"      :both)
diff --git a/src/numbers.lisp b/src/numbers.lisp
new file mode 100644 (file)
index 0000000..24882b7
--- /dev/null
@@ -0,0 +1,51 @@
+;;; numbers.lisp
+
+;; JSCL is free software: you can redistribute it and/or
+;; modify it under the terms of the GNU General Public License as
+;; published by the Free Software Foundation, either version 3 of the
+;; License, or (at your option) any later version.
+;;
+;; JSCL is distributed in the hope that it will be useful, but
+;; WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+;; General Public License for more details.
+;;
+;; You should have received a copy of the GNU General Public License
+;; along with JSCL.  If not, see <http://www.gnu.org/licenses/>.
+
+;;;; Various numeric functions and constants
+
+;; TODO: Use MACROLET when it exists
+(defmacro defcomparison (operator)
+  `(defun ,operator (x &rest args)
+     (while (not (null args))
+       (if (,operator x (car args))
+         (setq x    (car args)
+               args (cdr args))
+         (return-from ,operator nil)))
+     t))
+
+(defcomparison >)
+(defcomparison >=)
+(defcomparison <)
+(defcomparison <=)
+
+(defconstant pi 3.141592653589793) 
+
+(defun evenp (x) (= (mod x 2) 0))
+(defun oddp  (x) (not (evenp x)))
+
+(flet ((%max-min (x xs func)
+         (while (not (null xs))
+           (setq x  (if (funcall func x (car xs))
+                      x
+                      (car xs))
+                 xs (cdr xs)))
+         x))
+  (defun max (x &rest xs) (%max-min x xs #'>))
+  (defun min (x &rest xs) (%max-min x xs #'<))) 
+
+(defun abs (x) (if (> x 0) x (- x)))
+
+(defun expt (base power) (expt base              power))
+(defun exp  (power)      (expt 2.718281828459045 power))
diff --git a/tests/numbers.lisp b/tests/numbers.lisp
new file mode 100644 (file)
index 0000000..bccde55
--- /dev/null
@@ -0,0 +1,27 @@
+;;;; Tests for numeric functions
+
+; ABS
+(test (= (abs  3) 3))
+(test (= (abs -3) 3))
+
+; MAX
+(test (= (max 1)     1))
+(test (= (max 1 2 3) 3))
+(test (= (max 3 2 1) 3))
+
+; MIN
+(test (= (min 1)     1))
+(test (= (min 1 2 3) 1))
+(test (= (min 3 2 1) 1))
+
+; EVENP
+(test      (evenp  2))
+(test      (evenp -2))
+(test (not (evenp  1)))
+(test      (evenp  0))
+
+; ODDP
+(test      (oddp  3))
+(test      (oddp -3))
+(test (not (oddp  2)))
+(test (not (oddp  0)))