METHOD-CALL JS macro
[jscl.git] / src / numbers.lisp
index 56328c8..f5dee9a 100644 (file)
@@ -13,6 +13,8 @@
 ;; You should have received a copy of the GNU General Public License
 ;; along with JSCL.  If not, see <http://www.gnu.org/licenses/>.
 
+(/debug "loading numbers.lisp!")
+
 ;;;; Various numeric functions and constants
 
 (macrolet ((def (operator initial-value)
@@ -56,6 +58,9 @@
 (defun zerop (x) (= x 0))
 (defun plusp (x) (< 0 x))
 
+(defun signum (x)
+  (if (zerop x) x (/ x (abs x))))
+
 (macrolet ((def (operator)
              `(defun ,operator (x &rest args)
                 (dolist (y args) 
 (defun evenp (x) (= (mod x 2) 0))
 (defun oddp  (x) (not (evenp x)))
 
-(flet ((%max-min (x xs func)
-         (dolist (y xs) 
-           (setq x  (if (funcall func x (car xs)) x y)))
-         x))
-  (defun max (x &rest xs) (%max-min x xs #'>))
-  (defun min (x &rest xs) (%max-min x xs #'<))) 
+(macrolet ((def (name comparison)
+             `(defun ,name (x &rest xs)
+                (dolist (y xs) 
+                  (when (,comparison y x)
+                    (setq x y)))
+                x)))
+  (def max >)
+  (def min <))
 
 (defun abs (x) (if (> x 0) x (- x)))
 
 (defun expt (base power) (expt base              power))
 (defun exp  (power)      (expt 2.718281828459045 power))
+
+(defun gcd-2 (a b)
+  (if (zerop b)
+      (abs a)
+    (gcd-2 b (mod a b))))
+
+(defun gcd (&rest integers)
+  (cond ((null integers)
+        0)
+       ((null (cdr integers))
+        (abs (first integers)))
+       ((null (cddr integers))
+        (gcd-2 (first integers) (second integers)))
+       (t
+        (apply #'gcd (gcd (first integers) (second integers)) (nthcdr 2 integers)))))
+
+(defun lcm-2 (a b)
+  (if (or (zerop a) (zerop b))
+      0
+    (/ (abs (* a b)) (gcd a b))))
+
+(defun lcm (&rest integers)
+  (cond ((null integers)
+        1)
+       ((null (cdr integers))
+        (abs (first integers)))
+       ((null (cddr integers))
+        (lcm-2 (first integers) (second integers)))
+       (t
+        (apply #'lcm (lcm (first integers) (second integers)) (nthcdr 2 integers)))))