Math#
Module access#
abs, round, min and max are global. Every other function and constant here is a member of the math module, reached through its import.
A call with a count its arities do not hold is refused with DS_VALIDATE_BAD_ARITY.
Refused with DS_VALIDATE_BAD_ARITY
abs(r.n, 1)
(Spec: [LEXP.3.1], [LIMP.3.3.4])
Constants#
math constants#
Constant |
Value |
Literal |
|---|---|---|
|
π |
|
|
e |
|
|
2π |
|
Each is a Float, the binary64 nearest its value.
import math
math.pi * r.level
(Spec: [LIMP.3.3.4], [LEXP.4.1])
inf#
Floating-point infinity, a permitted Float value. inf == inf holds.
nan#
A reserved identifier that is not a usable value: in a slot value, a let or a :: cast target it is refused with DS_VALIDATE_NAN_VALUE.
Refused with DS_VALIDATE_NAN_VALUE
let level = nan
log info "{r.id}"
NaN values#
An expression may compute a NaN: inf - inf, inf / inf and 0.0 * inf are NaN, and no Float operation checks for it. A NaN never enters working memory:
a NaN
Floatarriving by a host assert is refused withDS_ERR_NAN_REJECTED;a NaN a rule’s activation would put in a slot is a
TypeError, and the engine abandons the activation;a NaN
Floatcall argument refuses the call withDS_ERR_NAN_REJECTED, and the engine does not issue it. A±Infargument passes.
A ±Inf, or a NaN an expression produced, forced into a JSON data-plane body is a NOT_REPRESENTABLE trap at the verb, and a host call that would build such a value is refused with DS_ERR_JSON_NONFINITE.
A NaN’s sign and payload are not observable.
(Spec: [LEXP.4.2], [LEXP.4.3], [LDET.3.2.7])
Arithmetic and rounding#
A rounding of a Float returns an Int. When the mathematical result lies outside the Int range, or the argument is not finite, the engine raises a ComputationError of kind NOT_REPRESENTABLE and abandons the activation.
(Spec: [LEXP.3.2.1])
abs#
abs(x)
The absolute value, of the argument’s type.
(Spec: [LEXP.3.2])
round#
round(x)
The nearest integer, ties to even.
round(r.level)
(Spec: [LEXP.3.2])
floor#
math.floor(x)
The largest integer not greater than x.
import math
math.floor(r.level)
(Spec: [LEXP.3.2])
ceil#
math.ceil(x)
The smallest integer not less than x.
import math
math.ceil(r.level)
(Spec: [LEXP.3.2])
trunc#
math.trunc(x)
x truncated toward zero.
import math
math.trunc(r.level)
(Spec: [LEXP.3.2])
sign#
math.sign(x)
An Int: 1 when x > 0, −1 when x < 0, otherwise 0. -0.0 and a NaN give 0.
import math
math.sign(r.level)
(Spec: [LEXP.3.2])
sqrt#
math.sqrt(x)
The correctly rounded square root. A negative x gives NaN.
import math
math.sqrt(r.level)
(Spec: [LEXP.3.2])
fma#
math.fma(x, y, z)
IEEE 754’s fused multiply-add: x × y + z, rounded once to binary64.
The operands are those x * y + z admits, under that expression’s unit rules. An Int operand converts to Float, and the result is a Float. An infinite or NaN operand, or 0 × ∞, gives the infinity or NaN IEEE 754 defines, and nothing traps. A null operand yields null, and the call is nullable where an operand is.
import math
math.fma(r.level, 2.0, 1.0)
(Spec: [LEXP.3.2.2])
pow#
math.pow(x, y)
The function form of **, with its result types. With a non-negative integer exponent the result is an exact Int. With any other exponent it is the correctly rounded Float under Transcendental functions.
import math
math.pow(r.n, 2)
(Spec: [LEXP.3.2], [LEXP.3.9.1])
Binary min and max#
min(a, b), max(a, b)
The smaller or larger of two numeric operands, keeping the winning operand’s kind: max(5, 3.0) is 5, and max(2, 3.5) is 3.5.
When the operands compare equal, the result is a, kind and sign included: max(2, 2.0) is 2, and min(0.0, -0.0) is 0.0. Two Int operands compare exactly. An Int beside a Float is promoted first.
One argument, a cursor, is the series aggregation.
max(min(r.n, 10), 0)
(Spec: [LEXP.3.8.1], [LEXP.3.8.2], [LEXP.12.11])
Angle conversion#
degrees#
math.degrees(x)
Radians to degrees: x times the binary64 nearest 180/π̂, rounded once, where π̂ is math.pi.
import math
math.degrees(r.level)
(Spec: [LEXP.3.6], [LEXP.3.6.1])
radians#
math.radians(x)
Degrees to radians: x times the binary64 nearest π̂/180, rounded once.
An Int argument to either function converts to Float first. At a non-finite or NaN argument, the result is the IEEE 754 product’s.
import math
math.radians(r.level)
(Spec: [LEXP.3.6], [LEXP.3.6.1])
Integer members#
x.NAME, x.NAME(args)
Members of an Int value, reading x as its 64-bit two’s-complement pattern, with bit 0 the least significant.
Member |
Result |
Domain |
|---|---|---|
|
the number of 1 bits; |
every |
|
the number of bits the magnitude of |
every |
|
the number of 0 bits above the highest 1 bit; 64 for 0, 0 for a negative |
every |
|
the number of 0 bits below the lowest 1 bit; 64 for 0 |
every |
|
the |
|
|
the low |
|
|
the low |
|
Every argument is an Int. A null receiver or argument yields null. bits and swap_bytes are non-negative unless w is 64.
An argument outside its domain raises a ComputationError of kind DOMAIN and abandons the activation. Compile-time constant arguments that place a call outside the domain whatever its other arguments hold are refused with DS_VALIDATE_BIT_FIELD_RANGE: x.swap_bytes(24) is refused, and x.bits(lo, 8) with a run-time lo is not.
On any other receiver type these names are refused with DS_VALIDATE_UNKNOWN_MEMBER. A Json leaf is cast to Int first.
Refused with DS_VALIDATE_BIT_FIELD_RANGE
r.n.swap_bytes(24)
(Spec: [LEXP.3.7.1], [LEXP.3.7.2], [LEXP.3.7.3], [LEXP.3.7.4])
Transcendental functions#
Each takes Float arguments, an Int argument converting to Float, and returns a Float. An angle argument is its value in radians.
The result is the exact mathematical value rounded once to the nearest binary64, ties to even: an overflowing value rounds to an infinity, and an underflowing one to a subnormal or a zero.
The operands with no real value, or whose value is a limit, are at LEXP.3.9.3. A NaN operand gives NaN, except in that table’s pow rows. A NaN result flows on through the expression.
Function |
Meaning |
|---|---|
|
|
|
the natural logarithm |
|
the base-2 logarithm |
|
the base-10 logarithm |
|
the trigonometric functions of |
|
the inverse trigonometric functions, in radians |
|
the angle of the point ( |
|
the hyperbolic functions |
(Spec: [LEXP.3.9.1], [LEXP.3.9.2], [LEXP.3.9.3], [LEXP.3.9.4])