Math functions over quantities and intervals — the ones that carry dimensions.
(require '[commensura.math :as m]) (m/sqrt (c/pow (u/meter 1) 2)) ;=> 1 meter (exact when a perfect root) (m/abs (u/meter -5)) ;=> 5 meter (m/min (u/foot 1) (u/inch 6)) ;=> 6 inch (physical order, keeps the unit) (m/mod (u/hour 25) (u/hour 24));=> 1 hour
Only functions that touch dimensions live here — preserving them (abs/mod/min/max/
floor/ceil/round), scaling them (sqrt/root/pow), or crossing the boundary (sign:
dimensioned → dimensionless). Transcendentals (exp/ln/sin/…) are intentionally absent:
they only ever map dimensionless → dimensionless, so they belong to plain numeric code.
Over intervals, the monotone functions lift by mapping the endpoints — sign/floor/ceil/round
directly, abs with a special case when the interval spans zero — and sqrt/root/pow scale
through. mod/rem are the exception: scalar-only, and they reject an interval argument, because
modular reduction is discontinuous and cannot be soundly lifted ([23,25] mod 24 is {23} ∪ [0,1],
not a single interval). Names shadow clojure.core, so use this namespace qualified (m/abs), never
:refer. Comparisons come from commensura.core.
Math functions over quantities and intervals — the ones that *carry dimensions*.
(require '[commensura.math :as m])
(m/sqrt (c/pow (u/meter 1) 2)) ;=> 1 meter (exact when a perfect root)
(m/abs (u/meter -5)) ;=> 5 meter
(m/min (u/foot 1) (u/inch 6)) ;=> 6 inch (physical order, keeps the unit)
(m/mod (u/hour 25) (u/hour 24));=> 1 hour
Only functions that touch dimensions live here — preserving them (`abs`/`mod`/`min`/`max`/
`floor`/`ceil`/`round`), scaling them (`sqrt`/`root`/`pow`), or crossing the boundary (`sign`:
dimensioned → dimensionless). Transcendentals (`exp`/`ln`/`sin`/…) are intentionally absent:
they only ever map dimensionless → dimensionless, so they belong to plain numeric code.
Over intervals, the monotone functions lift by mapping the endpoints — `sign`/`floor`/`ceil`/`round`
directly, `abs` with a special case when the interval spans zero — and `sqrt`/`root`/`pow` scale
through. `mod`/`rem` are the exception: **scalar-only, and they reject an interval argument**, because
modular reduction is discontinuous and cannot be soundly lifted (`[23,25] mod 24` is `{23} ∪ [0,1]`,
not a single interval). Names shadow `clojure.core`, so use this namespace qualified (`m/abs`), never
`:refer`. Comparisons come from `commensura.core`.(abs x)Absolute value; dimension-preserving. Over a zero-spanning interval the lower bound is 0.
Absolute value; dimension-preserving. Over a zero-spanning interval the lower bound is 0.
(ceil x)Smallest integer ≥ x, in x's unit.
Smallest integer ≥ x, in x's unit.
(floor x)Largest integer ≤ x, in x's unit.
Largest integer ≤ x, in x's unit.
(max x)(max x y)(max x y & more)The physically larger value (variadic); keeps the winner's unit. Conforming.
The physically larger value (variadic); keeps the winner's unit. Conforming.
(min x)(min x y)(min x y & more)The physically smaller value (variadic); keeps the winner's unit. Conforming.
The physically smaller value (variadic); keeps the winner's unit. Conforming.
(mod x y)x modulo y — conforming, dimension-preserving (keeps x's unit). Scalar-only: an interval argument is rejected (modular reduction is discontinuous, so it cannot be soundly lifted).
x modulo y — conforming, dimension-preserving (keeps x's unit). Scalar-only: an interval argument is rejected (modular reduction is discontinuous, so it cannot be soundly lifted).
(pow x n)Raise to an integer or rational exponent.
Raise to an integer or rational exponent.
(rem x y)Remainder of x by y — conforming, dimension-preserving. Scalar-only: an interval argument is
rejected (see mod).
Remainder of x by y — conforming, dimension-preserving. Scalar-only: an interval argument is rejected (see `mod`).
(round x)Nearest integer (half → +∞), in x's unit.
Nearest integer (half → +∞), in x's unit.
(sign x)Sign of a value: a plain -1, 0, or 1, for a value of any dimension.
Sign of a value: a plain -1, 0, or 1, for a value of any dimension.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |