Basic math facilities.

Operations throw on overflow and don’t auto-promote. Concrete number types are host specific and are specified in dunaj.host.number.

Depending on the needs, there are several other namespaces with similar functionalities:

Design notes:

  • Number types are not extensible (there are no number protocols), due to host optimizations.

  • No built-in number types. Utilizes host number types.

  • clojure.core/*unchecked-math* is omited by design.

Primary

Decimal

Available since version 1.0 (view source)

VAR of type Signature

A type signature for decimal numbers.

Float

Available since version 1.0 (view source)

VAR of type Signature

A type signature for floating point numbers.

This is not a host Float type, but rather a type signature for any floating point type (e.g. JVM Float, Double).
This type signature is not available in Dunaj lite, please use Float+ instead.

Float+

Available since version 1.0 (view source)

VAR of type Signature

A type signature for floating point numbers.

Identical to Float type signature, meant to be used in Dunaj lite.

Integer

Available since version 1.0 (view source)

VAR of type Signature

A type signature for integer numbers.

This is not a host Integer type, but rather a type signature for any integer type (e.g. JVM Integer, Byte, Short).
This type signature is not available in Dunaj lite, please use Integer+ instead.

Integer+

Available since version 1.0 (view source)

VAR of type Signature

A type signature for integer numbers.

Identical to Integer type signature, meant to be used in Dunaj lite.

Number

Available since version 1.0 (view source)

VAR of type Signature

A type signature for numbers.

This type signature is not available in Dunaj lite, please use Number+ instead.

Number+

Available since version 1.0 (view source)

VAR of type Signature

A type signature for numbers.

Identical to Number type signature, meant to be used in Dunaj lite.

Rational

Available since version 1.0 (view source)

VAR of type Signature

A type signature for rational numbers (not just Clojure ratios).

decimal?

Available since version 1.0 (view source)

Usage:
  • (decimal? x)

Type signature:
  • (Any) → Boolean+

Returns true if x is a decimal number, otherwise returns false.

denominator

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (denominator x)

Type signature:
  • (Rational) → Integer+

Returns the denominator part of a given rational number x.

float?

Available since version 1.0 (view source)

Usage:
  • (float? x)

Type signature:
  • (Any) → Boolean+

Returns true if x is a floating point number, otherwise returns false.

integer?

Available since version 1.0 (view source)

Usage:
  • (integer? x)

Type signature:
  • (Any) → Boolean+

Returns true if x is an integer number, otherwise returns false.

num

Available since version 1.0 (view source)

Usage:
  • (num x)

Type signatures:
  • (nil) → nil

  • (Number) → Number

  • (INumerical) → Number

Returns a numerical value for x. Returns x if it is already a number. Returns nil if x is nil.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
(num 10)
;;=> 10

(num \Q)
;;=> 81

(char 81)
;;=> \Q

(num #inst "2013-10-20")
;;=> 1382227200000

(instant 1382227200000)
;;=> #<BasicInstant 2013-10-20T00:00:00.000000000-00:00>

(num #uuid "462e019f-e7f2-4971-8ba6-14150556f147")
;;=> 93284838311908100738254531818413289799

(uuid 93284838311908100738254531818413289799)
;;=> #uuid "462e019f-e7f2-4971-8ba6-14150556f147"

number?

Available since version 1.0 (view source)

Usage:
  • (number? x)

Type signature:
  • (Any) → Boolean+

Returns true if x is a number, otherwise returns false.

numerator

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (numerator x)

Type signature:
  • (Rational) → Integer+

Returns the numerator part of a given rational number x.

numerical?

Available since version 1.0 (view source)

Usage:
  • (numerical? x)

Type signature: Predicate

Returns true if object x satisfies INumerical protocol, false otherwise.

rational?

Available since version 1.0 (view source)

Usage:
  • (rational? x)

Type signature:
  • (Any) → Boolean+

Returns true if x is a rational number, otherwise returns false.

rationalize

Available since version 1.0 (alias of clojure.core/rationalize)

not referred automatically

Usage:
  • (rationalize num)

Type signature:
  • (Number) → Rational

Returns the rational value of num.

Comparison

<

Available since version 1.0 (alias of clojure.core/<)

Usage:
  • (< x)

  • (< x y)

  • (< x y & more)

Type signatures:
  • (Number) → Boolean+

  • (Number ⨯ Number) → Boolean+

  • (Number ⨯ Number ⨯ (Va Number)) → Boolean+

Returns true if nums are in monotonically increasing order, otherwise returns false.

See also: <=, ==, >, dunaj.host.int/i<

<=

Available since version 1.0 (alias of clojure.core/⇐)

Usage:
  • (<= x)

  • (<= x y)

  • (<= x y & more)

Type signatures:
  • (Number) → Boolean+

  • (Number ⨯ Number) → Boolean+

  • (Number ⨯ Number ⨯ (Va Number)) → Boolean+

Returns true if nums are in monotonically non-decreasing order, otherwise returns false.

See also: <, ==, >=, dunaj.host.int/i<=

==

Available since version 1.0 (alias of clojure.core/==)

Usage:
  • (== x)

  • (== x y)

  • (== x y & more)

Type signatures:
  • (Number) → Boolean+

  • (Number ⨯ Number) → Boolean+

  • (Number ⨯ Number ⨯ (Va Number)) → Boolean+

Returns true if nums all have the equivalent value (type-independent), otherwise returns false.

See also: <, >, dunaj.host.int/i==

>

Available since version 1.0 (alias of clojure.core/>)

Usage:
  • (> x)

  • (> x y)

  • (> x y & more)

Type signatures:
  • (Number) → Boolean+

  • (Number ⨯ Number) → Boolean+

  • (Number ⨯ Number ⨯ (Va Number)) → Boolean+

Returns true if nums are in monotonically decreasing order, otherwise returns false.

See also: <, ==, >=, dunaj.host.int/i>

>=

Available since version 1.0 (alias of clojure.core/>=)

Usage:
  • (>= x)

  • (>= x y)

  • (>= x y & more)

Type signatures:
  • (Number) → Boolean+

  • (Number ⨯ Number) → Boolean+

  • (Number ⨯ Number ⨯ (Va Number)) → Boolean+

Returns true if nums are in monotonically non-increasing order, otherwise returns false.

See also: <=, ==, >, dunaj.host.int/i>=

divisible?

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (divisible? num div)

Type signature:
  • (Number ⨯ Number) → Boolean+

Returns true if num is divisible by div, otherwise returns false.

See also: indivisible?, gcd, lcm, mod, rem

even?

Available since version 1.0 (alias of clojure.core/even?)

Usage:
  • (even? n)

Type signature:
  • (Integer+) → Boolean+

Returns true if n is even, otherwise returns false.

max

Available since version 1.0 (alias of clojure.core/max)

Usage:
  • (max x)

  • (max x y)

  • (max x y & more)

Type signatures:
  • (Number) → Number

  • (Number ⨯ Number) → Number

  • (Number ⨯ Number ⨯ (Va Number)) → Number

Returns the greatest of the nums.

min

Available since version 1.0 (alias of clojure.core/min)

Usage:
  • (min x)

  • (min x y)

  • (min x y & more)

Type signatures:
  • (Number) → Number

  • (Number ⨯ Number) → Number

  • (Number ⨯ Number ⨯ (Va Number)) → Number

Returns the least of the nums.

See also: max, <, dunaj.host.int/imin

neg?

Available since version 1.0 (alias of clojure.core/neg?)

Usage:
  • (neg? x)

Type signature:
  • (Number) → Boolean+

Returns true if x is less than zero, otherwise returns false.

nneg?

Available since version 1.0 (view source)

Usage:
  • (nneg? x)

Type signature:
  • (Number) → Boolean+

Returns true if x is not negative, false otherwise.

npos?

Available since version 1.0 (view source)

Usage:
  • (npos? x)

Type signature:
  • (Number) → Boolean+

Returns true if x is not positive, false otherwise.

odd?

Available since version 1.0 (alias of clojure.core/odd?)

Usage:
  • (odd? n)

Type signature:
  • (Integer+) → Boolean+

Returns true if n is odd, otherwise returns false.

one?

Available since version 1.0 (view source)

Usage:
  • (one? x)

Type signature:
  • (Number) → Boolean+

Returns true if x is equal to 1, false otherwise.

pos?

Available since version 1.0 (alias of clojure.core/pos?)

Usage:
  • (pos? x)

Type signature:
  • (Number) → Boolean+

Returns true if x is greater than zero, otherwise returns false.

zero?

Available since version 1.0 (alias of clojure.core/zero?)

Usage:
  • (zero? x)

Type signature:
  • (Number) → Boolean+

Returns true if x is zero, otherwise returns false.

Operations

*

Available since version 1.0 (alias of clojure.core/*)

Usage:
  • (*)

  • (* x)

  • (* x y)

  • (* x y & more)

Type signatures:
  • () → Number

  • (Number) → Number

  • (Number ⨯ Number) → Number

  • (Number ⨯ Number ⨯ (Va Number)) → Number

Returns the product of nums. (*) returns 1. Does not auto-promote, will throw on overflow.

+

Available since version 1.0 (alias of clojure.core/+)

Usage:
  • (+)

  • (+ x)

  • (+ x y)

  • (+ x y & more)

Type signatures:
  • () → Number

  • (Number) → Number

  • (Number ⨯ Number) → Number

  • (Number ⨯ Number ⨯ (Va Number)) → Number

Returns the sum of nums. (+) returns 0. Does not auto-promote, will throw on overflow.

-

Available since version 1.0 (alias of clojure.core/-)

Usage:
  • (- x)

  • (- x y)

  • (- x y & more)

Type signatures:
  • (Number) → Number

  • (Number ⨯ Number) → Number

  • (Number ⨯ Number ⨯ (Va Number)) → Number

If no ys are supplied, returns the negation of x, else subtracts the ys from x and returns the result. Does not auto-promote, will throw on overflow.

/

Available since version 1.0 (alias of clojure.core//)

Usage:
  • (/ x)

  • (/ x y)

  • (/ x y & more)

Type signatures:
  • (Number) → Number

  • (Number ⨯ Number) → Number

  • (Number ⨯ Number ⨯ (Va Number)) → Number

If no denominators are supplied, returns 1/x, else returns x divided by all of the denominators.

abs

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (abs x)

Type signature:
  • (Number) → Number

Returns the absolute value of a number x.

See also: *, dunaj.host.int/iabs

add

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (add x y)

Type signature:
  • (Number ⨯ Number) → Number

Returns the sum of x and y. Does not auto-promote, will throw on overflow.

cbrt

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (cbrt x)

Type signature:
  • (Float+) → Float+

Returns the cube root of a floating point number x.

See also: sqrt, pow

dec

Available since version 1.0 (alias of clojure.core/dec)

Usage:
  • (dec x)

Type signature:
  • (Number) → Number

Returns a number one less than x. Does not auto-promote, will throw on overflow.

divide

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (divide x y)

Type signature:
  • (Number ⨯ Number) → Number

Returns the result of division of x by y.

See also: /, dunaj.host.int/idiv

exp

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (exp x)

Type signature:
  • (Float+) → Float+

Returns ex, the e raised to the power of a floating point number x.

See also: expm1, log

expm1

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (expm1 x)

Type signature:
  • (Float+) → Float+

Returns (dec (exp x)), which yields more precise result.

See also: exp, log1p

gcd

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (gcd x y)

  • (gcd x y & more)

Type signatures:
  • (Integer+ ⨯ Integer+) → Integer+

  • (Integer+ ⨯ Integer+ ⨯ (Va Integer+)) → Integer+

Returns the greatest common divisor of given integer numbers.

inc

Available since version 1.0 (alias of clojure.core/inc)

Usage:
  • (inc x)

Type signature:
  • (Number) → Number

Returns a number one greater than x. Does not auto-promote, will throw on overflow.

lcm

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (lcm x y)

  • (lcm x y & more)

Type signatures:
  • (Integer+ ⨯ Integer+) → Integer+

  • (Integer+ ⨯ Integer+ ⨯ (Va Integer+)) → Integer+

Returns the least common multiple of given integer numbers.

log

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (log x)

Type signature:
  • (Float+) → Float+

Returns the natural logarithm of a floating point number x.

See also: log1p, log10, exp

log10

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (log10 x)

Type signature:
  • (Float+) → Float+

Returns the base 10 logarithm of a floating point number x.

See also: log, log1p

log1p

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (log1p x)

Type signature:
  • (Float+) → Float+

Returns (inc (exp x)), which yields more precise result.

See also: log, log10, expm1

mod

Available since version 1.0 (alias of clojure.core/mod)

Usage:
  • (mod num div)

Type signature:
  • (Number ⨯ Number) → Number

Returns the modulus of num and div. Truncates toward negative infinity.

multiply

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (multiply x y)

Type signature:
  • (Number ⨯ Number) → Number

Returns the result of multiplication of x and y. Does not auto-promote, will throw on overflow.

negate

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (negate x)

Type signature:
  • (Number) → Number

Returns the negation of x. Does not auto-promote, will throw on overflow.

pow

Available since version 1.0 (view source)

Usage:
  • (pow x y)

Type signature:
  • (Number ⨯ Number) → Number

Returns xy, the x raised to power of y.

See also: sqrt, cbrt, exp

quot

Available since version 1.0 (alias of clojure.core/quot)

Usage:
  • (quot num div)

Type signature:
  • (Number ⨯ Number) → Number

Returns the quotient of dividing num by div.

rem

Available since version 1.0 (alias of clojure.core/rem)

Usage:
  • (rem num div)

Type signature:
  • (Number ⨯ Number) → Number

Returns the remainder of dividing num by div.

sqrt

Available since version 1.0 (view source)

Usage:
  • (sqrt x)

Type signature:
  • (Float+) → Float+

Returns the square root of a floating point number x.

See also: cbrt, pow

subtract

Available since version 1.0 (view source)

not referred automatically

Usage:
  • (subtract x y)

Type signature:
  • (Number ⨯ Number) → Number

Returns the result of subtraction of y from x. Does not auto-promote, will throw on overflow.

Rounding

PrecisionConfig

Available since version 1.0 (view source)

not referred automatically

VAR of type Signature

A type signature for precision config.

See also: round, with-precision

ceil

Available since version 1.0 (view source)

Usage:
  • (ceil x)

Type signature:
  • (Number) → Number

Returns the ceiling of a given number x.

See also: round, floor, trunc

floor

Available since version 1.0 (view source)

Usage:
  • (floor x)

Type signature:
  • (Number) → Number

Returns the floor of a given number x.

See also: round, ceil, trunc

round

Available since version 1.0 (view source)

Usage:
  • (round x)

  • (round x config)

  • (round x key val & keyvals)

Type signatures:
  • (Number) → Number

  • (Number ⨯ PrecisionConfig) → Number

  • (Number ⨯ (U :precision :type :mode) ⨯ Any ⨯ (Va Any)) → Number

Returns rounded number of same type according to given precision config.

Precision config is a map with following keys:

  • :precision - Any integer. Nonnegative in case of :significant type. Defaults to 0 (0 precision with significant type means that any number of significant digits is OK).

  • :type - :significant or :decimal. Defaults to :significant.

  • :mode - :ceiling, :floor, :half-up, :half-down, :half-even, :up, :down and :unnecessary. Defaults to :half-up.

(round x) is equal to (round x {:type :decimal}). Note that this is different from (round x {}), which equals to (round x {:precision 0 :type :significant :mode :half-up})

1
2
3
4
5
6
7
8
9
10
11
(round 123.456 {:precision 5})
;;=> 123.46

(round 123.456 :precision 5 :mode :down)
;;=> 123.45

(round 123.456)
;;=> 123.0

(round 123.456 {})
;;=> 123.456

trunc

Available since version 1.0 (view source)

Usage:
  • (trunc x)

Type signature:
  • (Number) → Integer+

Returns an integer number created from given floating or decimal number x by truncating its decimal part. Returns unchanged if x is integer or rational.

See also: round, rationalize, ceil, floor

with-precision

Available since version 1.0 (view source)

MACRO (with-precision config & body)

Sets the precision config to be used for BigDecimal operations.

Precision config is a map with following keys:

  • :precision - A nonnegative integer. Defaults to 0 (no changes)

  • :mode - One of :ceiling, :floor, :half-up, :half-down, :half-even, :up, :down and :unnecessary. Defaults to :half-up.

1
2
3
4
5
6
7
(with-precision {:precision 10}
  (/ 1M 3))
;;=> 0.3333333333M

(with-precision {:precision 5 :mode :up}
  (/ 1M 3))
;;=> 0.33334M

See also: round, PrecisionConfig

Other

In this section: e pi

e

Available since version 1.0 (view source)

not referred automatically

VAR of type Float+

The e constant as defined by the host.

See also: pi, exp, expm1, log, log1p, log10

pi

Available since version 1.0 (view source)

not referred automatically

VAR of type Float+

The PI constant as defined by the host.

See also: e, dunaj.math.angle/deg