Num

The Num class is the core numeric type in Wren. All numbers are double-precision floating point. The class provides arithmetic, trigonometric, bitwise, rounding, query, conversion, and formatting methods.

Static Constants

Num.pi → Num

The ratio of a circle's circumference to its diameter (3.14159...).

Num.tau → Num

Two times pi (6.28318...).

Num.e → Num

Euler's number (2.71828...).

Num.infinity → Num

Positive infinity.

Num.nan → Num

Not-a-number value.

Num.largest → Num

The largest representable double value.

Num.smallest → Num

The smallest positive representable double value.

Num.maxSafeInteger → Num

The largest integer representable without loss of precision (9007199254740991).

Num.minSafeInteger → Num

The smallest integer representable without loss of precision (-9007199254740991).

Static Methods

Num.fromString(string) → Num|null

Parses a string as a number. Returns null if the string is not a valid number.

Num.fromString("42")    // 42
Num.fromString("3.14")  // 3.14
Num.fromString("nope")  // null

Query Methods

isInteger → Bool

Whether the number is an integer (has no fractional part).

isNan → Bool

Whether the number is NaN.

isInfinity → Bool

Whether the number is positive or negative infinity.

isFinite → Bool

Whether the number is finite (not infinity and not NaN).

42.isFinite      // true
(1/0).isFinite   // false
(0/0).isFinite   // false
isZero → Bool

Whether the number equals zero.

isPositive → Bool

Whether the number is strictly greater than zero.

isNegative → Bool

Whether the number is strictly less than zero.

isEven → Bool

Whether the number is an even integer.

isOdd → Bool

Whether the number is an odd integer.

isBetween(min, max) → Bool

Whether the number is within the inclusive range [min, max].

5.isBetween(1, 10)   // true
0.isBetween(1, 10)   // false
10.isBetween(1, 10)  // true
sign → Num

Returns 1 for positive, -1 for negative, or 0 for zero.

Arithmetic

abs → Num

Absolute value.

min(other) → Num

Returns the smaller of this number and other.

max(other) → Num

Returns the larger of this number and other.

clamp(min, max) → Num

Constrains this number to the range [min, max].

15.clamp(0, 10)  // 10
5.clamp(0, 10)   // 5
(-3).clamp(0, 10) // 0
pow(exponent) → Num

Raises this number to the given power.

Rounding

ceil → Num

Rounds up to the nearest integer.

floor → Num

Rounds down to the nearest integer.

round → Num

Rounds to the nearest integer.

truncate → Num

Removes the fractional part, rounding toward zero.

fraction → Num

Returns the fractional part of the number.

Roots & Exponents

sqrt → Num

Square root.

cbrt → Num

Cube root.

exp → Num

Returns e raised to this power.

Logarithms

log → Num

Natural logarithm (base e).

log2 → Num

Base-2 logarithm.

log10 → Num

Base-10 logarithm.

100.log10   // 2
1000.log10  // 3
1.log10     // 0

Trigonometry

sin → Num

Sine (argument in radians).

cos → Num

Cosine (argument in radians).

tan → Num

Tangent (argument in radians).

asin → Num

Arc sine, returns radians.

acos → Num

Arc cosine, returns radians.

atan → Num

Arc tangent, returns radians.

atan(x) → Num

Two-argument arc tangent (atan2). Returns the angle between the positive x-axis and the point (x, this).

Hyperbolic Functions

sinh → Num

Hyperbolic sine.

cosh → Num

Hyperbolic cosine.

tanh → Num

Hyperbolic tangent.

0.sinh  // 0
0.cosh  // 1
0.tanh  // 0

Angle Conversion

toDegrees → Num

Converts radians to degrees.

toRadians → Num

Converts degrees to radians.

Num.pi.toDegrees  // 180
180.toRadians     // 3.14159...

Conversion

toString → String

Converts the number to its string representation.

toChar → String

Returns the Unicode character for this code point.

65.toChar  // "A"
97.toChar  // "a"
toBase(radix) → String

Converts the integer part to a string in the given base (2-36).

255.toBase(16)  // "ff"
255.toBase(2)   // "11111111"
255.toBase(8)   // "377"
toHex → String

Shorthand for toBase(16).

toBinary → String

Shorthand for toBase(2).

toOctal → String

Shorthand for toBase(8).

Formatting

format(decimals) → String

Formats the number with a fixed number of decimal places (0-20).

3.14159.format(2)  // "3.14"
42.format(3)       // "42.000"
(-1.5).format(1)   // "-1.5"

Integer Operations

gcd(other) → Num

Greatest common divisor using the Euclidean algorithm. Operates on absolute values.

12.gcd(8)   // 4
54.gcd(24)  // 6
7.gcd(13)   // 1
lcm(other) → Num

Least common multiple.

4.lcm(6)  // 12
3.lcm(5)  // 15
digits → List

Returns a list of the individual digits of the integer's absolute value. Aborts if the number is not an integer.

123.digits     // [1, 2, 3]
0.digits       // [0]
(-456).digits  // [4, 5, 6]

Bitwise Operations

Bitwise operators work on 32-bit unsigned integers.

Operator Description
&(other)Bitwise AND
|(other)Bitwise OR
^(other)Bitwise XOR
<<(other)Left shift
>>(other)Right shift
~Bitwise NOT

Ranges

..(to) → Range

Creates an inclusive range from this number to to.

...(to) → Range

Creates an exclusive range from this number to to (excludes the end).

for (i in 1..5) System.print(i)   // 1 2 3 4 5
for (i in 1...5) System.print(i)  // 1 2 3 4