Files
2026-03-09 23:09:46 -04:00

142 lines
2.3 KiB
Clojure

(ns math
"Erlang :math module — mathematical functions.
In CljElixir: (math/sqrt 2), (math/pow 2 10), etc.
All functions return floats.")
(defn pi
"Returns the value of pi.
(math/pi) ;=> 3.141592653589793"
[])
(defn e
"Returns Euler's number.
(math/e) ;=> 2.718281828459045"
[])
(defn tau
"Returns tau (2*pi).
(math/tau) ;=> 6.283185307179586"
[])
;; --- Powers & Roots ---
(defn pow
"Returns `x` raised to `y`.
(math/pow 2 10) ;=> 1024.0"
[x y])
(defn sqrt
"Returns the square root.
(math/sqrt 4) ;=> 2.0"
[x])
(defn exp
"Returns e^x.
(math/exp 1) ;=> 2.718281828459045"
[x])
(defn log
"Returns the natural logarithm (base e).
(math/log 2.718281828459045) ;=> 1.0"
[x])
(defn log2
"Returns the base-2 logarithm.
(math/log2 1024) ;=> 10.0"
[x])
(defn log10
"Returns the base-10 logarithm.
(math/log10 1000) ;=> 3.0"
[x])
;; --- Trigonometry ---
(defn sin
"Returns the sine (radians).
(math/sin (/ (math/pi) 2)) ;=> 1.0"
[x])
(defn cos
"Returns the cosine (radians).
(math/cos 0) ;=> 1.0"
[x])
(defn tan
"Returns the tangent (radians).
(math/tan 0) ;=> 0.0"
[x])
(defn asin
"Returns the arcsine (radians).
(math/asin 1) ;=> 1.5707963267948966"
[x])
(defn acos
"Returns the arccosine (radians).
(math/acos 1) ;=> 0.0"
[x])
(defn atan
"Returns the arctangent (radians).
(math/atan 1) ;=> 0.7853981633974483"
[x])
(defn atan2
"Returns the two-argument arctangent (radians).
(math/atan2 1 1) ;=> 0.7853981633974483"
[y x])
;; --- Hyperbolic ---
(defn sinh
"Returns the hyperbolic sine."
[x])
(defn cosh
"Returns the hyperbolic cosine."
[x])
(defn tanh
"Returns the hyperbolic tangent."
[x])
(defn asinh
"Returns the inverse hyperbolic sine."
[x])
(defn acosh
"Returns the inverse hyperbolic cosine."
[x])
(defn atanh
"Returns the inverse hyperbolic tangent."
[x])
;; --- Rounding ---
(defn ceil
"Returns the smallest integer >= x (as float).
(math/ceil 3.2) ;=> 4.0"
[x])
(defn floor
"Returns the largest integer <= x (as float).
(math/floor 3.8) ;=> 3.0"
[x])
(defn fmod
"Returns the floating-point remainder of x/y.
(math/fmod 10.0 3.0) ;=> 1.0"
[x y])
(defn erf
"Returns the error function.
(math/erf 1) ;=> 0.8427007929497149"
[x])
(defn erfc
"Returns the complementary error function."
[x])