Useful Math Function

Started by Jeremy Dunn, November 23, 2007, 01:31:07 PM

Previous topic - Next topic

Jeremy Dunn

This function is basically a counterpart to the SQRT function and serves a useful purpose. I originally had two separate functions but then realized that I could combine them both.



;; This function takes one or more numbers as arguments. If there is a single
;; number the number is squared. If there is more than one number then the square
;; root of the sum of the squares (a^2 + b^2 + c^2 + ...)^1/2 is returned.
;; Example: (sq 3) -> 9
;;          (sq 2 3) -> 3.605551275
(define-macro (sq)
  (if (= (length (args)) 1)
      (mul (args 0)(args 0))
      (sqrt (apply add (map mul (args)(args))))))