Browse documentation
aner.math

Scalar mathematics

Use checked native Float64 mathematics and explicit numerical closeness with documented domains, tolerances and errors.

Implemented

aner.math provides native CPU scalar mathematics for Float64 values. Import the module for approximate comparison, square roots, powers, natural/base-two/base-ten logarithms, rounding, exponential and trigonometric functions, and the immutable math.pi, math.e, and math.tau constants. Inputs and results are checked against each function's domain and the finite Float64 range. math.log always means the natural logarithm, with base e.

The new math.close comparison, logarithm/rounding functions and constants require a runtime rebuilt from this source and compatible editor support. Earlier binaries may support only part of aner.math; match the executable to this documentation.

Run a complete example

After installing Aner, save this program as math_basic.aner, or use the complete bundled source:

aner
import aner.math

let hypotenuse = math.sqrt(math.pow(base: 3.0, exponent: 2.0) + math.pow(4.0, 2.0))
print(hypotenuse)
print(math.abs(-3.5))
print(math.pow(exponent: 3.0, base: -2.0))
print(math.exp(0.0))
print(math.log(1.0))
print(math.sin(0.0))
print(math.cos(0.0))
print(math.tan(0.0))
print(math.sqrt(Float64(4)))
print(math.pow(0.0, 0.0))

print(math.abs(math.sin(math.pi / 6.0) - 0.5) < 0.000000000001)
print(math.log2(8))
print(math.log10(1000))
print(math.round(3.7))
print(math.round(2.5))
print(math.floor(-3.2))
print(math.ceil(3.2))
print(type(math.pi))
print(math.tau == 2.0 * math.pi)
print(math.abs(math.log(math.e) - 1.0) < 0.000000000001)
print(7 / 2)
sh
aner check math_basic.aner
aner run math_basic.aner

Expected output:

text
5.0
3.5
-8.0
1.0
0.0
0.0
1.0
0.0
2.0
1.0
true
3.0
3.0
4.0
2.0
-4.0
4.0
Float64
true
true
3.5

Numerical parameters use Float64. Int64 and Number arguments are accepted through checked exact Float64 promotion, so math.sqrt(4) and math.sqrt(4.0) are both valid. An integer that cannot be represented exactly fails with R1003; Float64(value) explicitly permits rounding before the call. Named arguments bind by the labels below and are evaluated once in source order.

Sampling belongs to aner.random, with caller owned generators or explicit seed/draw identities. aner.math remains deterministic and owns no random state.

Native API

SignatureResult and domain
math.close(a: Float64, b: Float64, relative: Float64 = 1.7763568394002505e-15, absolute: Float64 = 0.0)Bool; symmetric approximate comparison. All inputs must be finite; 0 <= relative < 1 and absolute >= 0.
math.sqrt(value: Float64)Float64; square root of a nonnegative value. Signed zero is accepted.
math.abs(value: Float64)Float64; absolute magnitude, including positive zero for -0.0.
math.pow(base: Float64, exponent: Float64)Float64; real power. A negative base requires an integral Float64 exponent. Zero with a negative exponent fails; 0.0 raised to 0.0 is explicitly 1.0.
math.exp(value: Float64)Float64; natural exponential.
math.log(value: Float64)Float64; natural logarithm of a strictly positive value.
math.log2(value: Float64)Float64; base two logarithm of a strictly positive value.
math.log10(value: Float64)Float64; base ten logarithm of a strictly positive value.
math.round(value: Float64)Float64; nearest integer value, resolving exact halfway cases toward the even integer.
math.floor(value: Float64)Float64; integer value rounded toward negative infinity.
math.ceil(value: Float64)Float64; integer value rounded toward positive infinity.
math.sin(value: Float64)Float64; sine of an angle expressed in radians.
math.cos(value: Float64)Float64; cosine of an angle expressed in radians.
math.tan(value: Float64)Float64; tangent of an angle expressed in radians.

Trigonometric arguments are raw Float64 radians. aner.units provides scalar quantities, absolute temperature points and unit literals through a bounded registry; math calls require an explicit .value() numerical boundary. Unit typed angles remain planned. Use SemanticTensor and EvidenceTensor for coordinates and the supported source aware uncertainty model.

Approximate comparison

Binary floating arithmetic rounds, so 0.1 + 0.2 == 0.3 is false. Import free 0.1 + 0.2 ~== 0.3 is true. Use local approx(relative: ..., absolute: ...) blocks to select immutable runtime tolerances for operators written inside a block. Keep == for exact numeric equality; use math.close for an explicit function call when a calculation permits an error margin. Run the complete comparison example:

aner
import aner.math

print(0.1 + 0.2 == 0.3)           // false
print(math.close(0.1 + 0.2, 0.3)) // true

let relative_tolerance = 1e-6
let absolute_tolerance = 1e-10
print(math.close(1000000.0, 1000000.5, relative: relative_tolerance)) // true
print(math.close(1e-12, 0.0, relative: 0.0, absolute: absolute_tolerance)) // true
print(math.close(1e-12, 0.0)) // false
print(math.close(0.1 + 0.2, 0.3, relative: 0.0, absolute: 0.0)) // false

The comparison accepts either an absolute difference or a difference relative to the larger magnitude:

text
|a - b| <= absolute  OR  |a - b| <= relative * max(|a|, |b|)

The implementation avoids overflow and preserves comparisons between subnormal values. Both tolerances are ordinary runtime expressions, may be supplied by name in either order, and are evaluated once in source order. Omitting either keeps its default; for example, math.close(a, b, absolute: tolerance) is valid. No process wide tolerance setting affects other calls or workers. An approx block affects only its lexical ~== expressions; explicit math.close calls always use their own arguments or fixed defaults.

The relative default is eight times binary64 machine epsilon, about 1.78e-15; it targets small arithmetic rounding differences. The absolute default is zero, so a nonzero value is not automatically close to zero. Choose explicit tolerances from the required accuracy, numerical algorithm and measurement scale. Float32 inputs widen exactly, but their earlier arithmetic retains Float32 error: supply a tolerance appropriate to that precision. Widening does not recover lost information.

Signed zeros compare close. Two zero tolerances require exact equality of the admitted Float64 values. All four inputs are validated even when a == b: infinity or NaN fails with R3601/E_MATH_NONFINITE, and a negative tolerance or relative >= 1 fails with R3601/E_MATH_DOMAIN. Checked integer promotion applies before comparison, so an integer that cannot be represented exactly still fails with R1003.

Mathematical constants

ValueTypeMeaning
math.piFloat64Binary64 approximation to pi, about 3.141592653589793.
math.eFloat64Binary64 approximation to Euler's number, about 2.718281828459045; the base of natural logarithms.
math.tauFloat64Two times math.pi, about 6.283185307179586.

Use lowercase math, consistent with import aner.math. These are immutable values, not functions: write math.pi, not Math.pi or math.pi(). They require the import, have no mutable global state, and can be used in expressions, function bodies, collections, and isolated repetition tasks. Existing function parameter defaults remain literal only; a constant expression does not expand that separate grammar.

Rounding and division

Rounding functions return Float64 even for integer arguments: math.floor(3.7) is 3.0. Use an explicit checked integer constructor when integer storage is needed, for example Int64(math.floor(3.7)). A rounded Float64 outside Int64's range still fails that conversion. Large finite floating values that already have no fractional bits remain unchanged.

math.round uses nearest with ties to even: 2.5 becomes 2.0, 3.5 becomes 4.0, -2.5 becomes -2.0, and -3.5 becomes -4.0. Ties refer to the actual binary floating value. math.floor(-3.2) is -4.0; math.ceil(-3.7) is -3.0. The functions preserve signed zero, including math.round(-0.5) and math.ceil(-0.1) returning negative zero. Their results do not depend on the ambient floating point rounding mode, and they do not change it. Rounding to zero is a valid result, including for subnormal inputs.

Division retains its existing contract: / returns Float64 for integer and binary floating scalars, so 7 / 2 is 3.5; integer_div(7, 2) gives integer 3. integer_div truncates toward zero, and % remains its paired integer remainder. Float32 scalar division also returns Float64. Decimal has its own decimal arithmetic and division result; it is not implicitly converted by math calls. See numeric conversion and arithmetic.

Numerical behavior and errors

Every input must be finite. Float64 source literals are checked by the parser, while ordinary scalar arithmetic can produce infinity or NaN; passing either into a math function fails explicitly. Domain and range failures produce a source located R3601 diagnostic containing the native code and operation:

Native codeMeaning and example
E_MATH_NONFINITEAn argument is infinity or NaN, for example math.sqrt(1.0 / 0.0).
E_MATH_DOMAINAn argument is outside the declared domain, for example math.sqrt(-1.0), math.log(0.0), math.pow(-4.0, 0.5) or math.close(1.0, 1.0, relative: -1.0).
E_MATH_RANGEA result overflows the finite Float64 range, or a mathematically nonzero exp/pow result rounds to zero. Examples include math.exp(1000.0), math.exp(-1000.0) and math.pow(0.5, 2000.0).

Finite representable subnormal results remain valid. math.pow(2.0, -1074.0) returns the smallest positive Float64 subnormal on the supported IEEE Float64 platforms. The exact zero result of a positive power of zero is valid. No implicit clamping, epsilon substitution or conversion of errors into a missing value occurs.

Tangent uses the supplied floating point angle. A finite approximation to pi / 2 can produce a large finite result and is accepted; the API applies no guessed near pole cutoff. Users can apply a domain specific tolerance before evaluating an ill conditioned expression.

Results follow the platform's native mathematical library, with normal Float64 rounding. Cross platform bitwise identity and correctly rounded transcendental results are not guaranteed. Last bit differences and argument reduction behavior can depend on the native library. The functions do not inspect stale errno or floating point exception flags to decide admission.

Computation boundaries

These functions perform scalar CPU operations and return plain Float64, except math.close, which returns Bool. They do not create Tensor differentiation graphs or propagate measurement uncertainty. Array vectorization, accelerator math and typed scalar units require their own supported APIs.

The functions are permitted in checked isolated repetition tasks, including admitted helper functions. A math failure in an iteration fails the repetition through its normal source located task diagnostic; no incomplete result batch is returned. Worker count does not change a task's explicit inputs, random draws or mathematical formulas.

Continue with Tensor for numerical arrays and differentiation, aner.science for measurement uncertainty, or the heat diffusion study for a complete numerical research workflow.

See the runnable Math example page and download its source.

See the complete callable index for the native module and method name catalogue.

Aner documentation · Reference and guides