1. Arithmetic Functions
Function | Description | Examples | ||
---|---|---|---|---|
\((\texttt{+}\;x_1\;x_2\;\ldots\;x_n)\) |
Returns \(x_1 + x_2 + \cdots + x_n\). Returns 0 if no arguments are specified. Throws an |
|
||
\((\texttt{+'}\;x_1\;x_2\;\ldots\;x_n)\) |
Returns \(x_1 + x_2 + \cdots + x_n\). Returns 0 if no arguments are specified. Converts result automatically into |
|
||
\((\texttt{inc}\;x)\) |
Returns \(x + 1\). Throws an |
|
||
\((\texttt{inc'}\;x)\) |
Returns \(x + 1\). Converts result automatically into |
|
||
\((\texttt{-}\;x_1\;x_2\;\ldots\;x_n)\) |
Returns \(x_1 - x_2 - \cdots - x_n\). Returns \(-x_1\) if only one argument is provided. Throws an |
|
||
\((\texttt{-'}\;x_1\;x_2\;\ldots\;x_n)\) |
Returns \(x_1 - x_2 - \cdots - x_n\). Returns \(-x_1\) if only one argument is provided. Converts result automatically into |
|
||
\((\texttt{dec}\;x)\) |
Returns \(x - 1\). Throws an |
|
||
\((\texttt{dec'}\;x)\) |
Returns \(x - 1\). Converts result automatically into |
|
||
\((\texttt{*}\;x_1\;x_2\;\ldots\;x_n)\) |
Returns \(x_1 \times x_2 \times \cdots \times x_n\). Returns 1 if no arguments are specified. Throws an |
|
||
\((\texttt{*'}\;x_1\;x_2\;\ldots\;x_n)\) |
Returns \(x_1 \times x_2 \times \cdots \times x_n\). Returns 1 if no arguments are specified. Converts result automatically into |
|
||
\((\texttt{/}\;x_1\;x_2\;\ldots\;x_n)\) |
Returns \(x_1 \div x_2 \div \cdots \div x_n\). Returns \(\frac{1}{x_1}\) if only one argument is provided. |
|
||
\((\texttt{quot}\;x\;y)\) |
Returns the integer quotient of dividing \(x\) by \(y\). |
|
||
\((\texttt{rem}\;x\;y)\) |
Returns the remainder of dividing \(x\) by \(y\).
|
|
||
\((\texttt{mod}\;x\;y)\) |
Returns \(x \bmod y \) (modulus of \(x\) and \(y\)). This is basically equivalent to: \(\texttt{(- } x \;\texttt{(* (}\texttt{Math/floor }\texttt{(/ } x\;y\texttt{)) }y\texttt{))}\)
|
|
2. Numerical Predicates
Function | Description | Examples |
---|---|---|
\((\texttt{zero?}\;x)\) |
Returns |
|
\((\texttt{pos?}\;x)\) |
Returns |
|
\((\texttt{neg?}\;x)\) |
Returns |
|
\((\texttt{even?}\;x)\) |
Returns |
|
\((\texttt{odd?}\;x)\) |
Returns |
|
3. Comparison Functions
Function | Description | Examples | ||
---|---|---|---|---|
\((\texttt{=}\;x_1\;x_2\;\ldots\;x_n)\) |
Returns
|
|
||
\((\texttt{==}\;x_1\;x_2\;\ldots\;x_n)\) |
Returns |
|
||
\((\texttt{not=}\;x_1\;x_2\;\ldots\;x_n)\) |
Same as \((\texttt{not}\;(\texttt{=}\;x_1\;x_2\;\ldots\;x_n))\).
|
|
||
\((\texttt{<}\;x_1\;x_2\;\ldots\;x_n)\) |
Returns |
|
||
\((\texttt{<=}\;x_1\;x_2\;\ldots\;x_n)\) |
Returns |
|
||
\((\texttt{>}\;x_1\;x_2\;\ldots\;x_n)\) |
Returns |
|
||
\((\texttt{>=}\;x_1\;x_2\;\ldots\;x_n)\) |
Returns |
|
4. Random Numbers
Function | Description | Examples |
---|---|---|
\((\texttt{rand})\) \((\texttt{rand}\;n)\) |
Returns a random floating point number \(r\), where \(0 \leqslant r < n\). If \(n\) is omitted it defaults to 1. |
|
\((\texttt{rand-int}\;n)\) |
Returns a random integer number \(r\), where \(0 \leqslant r < n\). |
|
5. Numeric Tower Functions
These are math functions that deal intelligently with the various types in Clojure’s numeric tower.
To use these functions, you need to import the corresponding namespace, something like this:
(use 'clojure.math.numeric-tower)
Function | Description | Examples |
---|---|---|
\((\texttt{abs}\;x)\) |
Returns \(\left | x \right |\) (absolute value of \(x\)). |
|
\((\texttt{sqrt}\;x)\) |
Returns \(\sqrt{x}\) (square root of \(x\)), exact if possible. |
|
\((\texttt{expt}\;x\;y)\) |
Returns \(x^y\) (\(x\) raised to the power \(y\)). |
|
\((\texttt{round}\;x)\) |
Returns \(\left \lfloor x \right \rceil\) (\(x\) rounded to the nearest integer). Rounds up for values exactly in between two integers. The result is always an integer. |
|
\((\texttt{ceil}\;x)\) |
Returns \(\left \lceil x \right \rceil\) (least integer greater than or equal to \(x\), or in other words, \(x\) rounded towards \(+\infty\)). If \(x\) is an exact number, returns an integer, otherwise a double. |
|
\((\texttt{floor}\;x)\) |
Returns \(\left \lfloor x \right \rfloor\) (greatest integer less than or equal to \(x\), or in other words, \(x\) rounded towards \(-\infty\)). If \(x\) is an exact number, returns an integer, otherwise a double. |
|
\((\texttt{gcd}\;x\;y)\) |
Returns the greatest common divisor of \(x\) and \(y\). Arguments must be integers. |
|
\((\texttt{lcm}\;x\;y)\) |
Returns the least common multiple of \(x\) and \(y\). Arguments must be integers. |
|