Core math

@core/math provides finite F64 constants and functions for trigonometry, powers, rounding and clamping. Unlike host math libraries, every result must satisfy Moth's finite-number contract before source code observes it.

Choose the explanation level: Math helpers

Math helpers

@core/math gives you F64 constants and helper functions for common math operations. Bind it explicitly when you need trigonometry, rounding or clamping.

Binding

Bind the symbols you need with a direct-selection clause.

@core/math PI, sin, clamp

wave = sin(PI / 2.0)
bounded = clamp(wave, 0.0, 1.0)

io.line([:bounded=[bounded]])

You can also bind the whole namespace and access members through field access.

@core/math

angle = math.PI / 2.0
result = math.sin(angle)

Basic constants

PI, TAU, E, SQRT_2, LN_2 and LN_10 form compile-time F64 values. Use them directly in expressions.

Common functions

  • sin, cos, tan for trigonometry
  • atan2(y, x) for arc tangent of two values
  • sqrt, pow, log for powers and logarithms
  • abs, floor, ceil, round, trunc for rounding
  • asin, acos, atan for inverse trigonometry
  • cbrt, hypot(x, y) for roots and norms
  • expm1, log1p for precise small-value helpers
  • sinh, cosh, tanh, asinh, acosh, atanh for the hyperbolics
  • min(a, b), max(a, b), clamp(x, min, max) for comparisons

All functions take F64 values and return F64. Angles use radians. Undefined or infinite results fail the shared finite-result boundary rather than returning NaN or infinity. That boundary is a fatal integrity guard, not an ordinary catchable failure: it halts execution unless an enclosing declared builtin Error! contract returns it. For the full function list, backend behaviour and deferred surfaces, see Core math advanced.

@core/math provides F64 constants and helper functions. It belongs to the binding-backed Core package family with no prelude alias, so it requires an explicit dependency clause.

@core/math carries Core origin with ExternalBinding backing. See Package origins and backing for origin and backing rules.

Dependency bindings

Bind individual symbols or the whole namespace:

@core/math PI, sin, clamp

@core/math

angle = math.PI / 2.0
result = math.sin(angle)

A namespace alias creates a file-local name:

@core/math as math

Constants

Constant

Type

Value

PI

F64

pi

TAU

F64

2 * pi

E

F64

Euler's number

SQRT_2

F64

square root of 2

LN_2

F64

natural log of 2

LN_10

F64

natural log of 10

Constants form compile-time F64 values. Projection preserves their exact binary64 identity and checks finiteness before they enter Moth expressions. They fold into expressions without runtime lookups. This does not make calls to the package's functions compile-time operations.

Functions

All functions take F64 parameters and return F64. Parameters stay positional-only. Named arguments lack support because binding-backed packages do not expose source-function features.

Function

Parameters

Returns

sin(x)

F64

F64

cos(x)

F64

F64

tan(x)

F64

F64

atan2(y, x)

F64, F64

F64

log(x)

F64

F64

log2(x)

F64

F64

log10(x)

F64

F64

exp(x)

F64

F64

pow(base, exponent)

F64, F64

F64

sqrt(x)

F64

F64

abs(x)

F64

F64

floor(x)

F64

F64

ceil(x)

F64

F64

round(x)

F64

F64

trunc(x)

F64

F64

min(a, b)

F64, F64

F64

max(a, b)

F64, F64

F64

clamp(x, min, max)

F64, F64, F64

F64

asin(x)

F64

F64

acos(x)

F64

F64

atan(x)

F64

F64

cbrt(x)

F64

F64

hypot(x, y)

F64, F64

F64

expm1(x)

F64

F64

log1p(x)

F64

F64

sinh(x)

F64

F64

cosh(x)

F64

F64

tanh(x)

F64

F64

asinh(x)

F64

F64

acosh(x)

F64

F64

atanh(x)

F64

F64

The functions declare no typed error slot. A non-finite result is still rejected at the shared external F64 boundary before ordinary Moth code observes it. That boundary is a fatal integrity guard, not implicit built-in failure: it halts unless a declared builtin Error! contract returns it, with code 304 for a non-finite result. NaN, positive infinity and negative infinity never become valid Moth F64 values. See Progress for current compiler support.

This guard adds no recoverable adapter for a foreign implementation that throws unexpectedly or otherwise violates its declared value contract.

Authored safety-catch eligibility does not manufacture a recoverable failure or make this fatal integrity guard catchable.

Numerical contract

Angles are radians for sin, cos, tan, asin, acos, atan and atan2. PI is the radian measure of a half turn and TAU of a full turn.

A mathematical result that is undefined or infinite produces no Moth F64. The shared external F64 boundary rejects the non-finite host result with builtin error code 304 before source observes it. The functions with restricted domains are:

  • sqrt(x) requires x >= 0
  • log(x), log2(x) and log10(x) require x > 0
  • log1p(x) requires x > -1
  • asin(x) and acos(x) require x between -1 and 1 inclusive
  • acosh(x) requires x >= 1
  • atanh(x) requires x strictly between -1 and 1
  • pow(base, exponent) requires base >= 0 when exponent is not a whole number, and base other than zero when exponent is negative

Overflow to an infinite result, such as exp, cosh or sinh of a large argument, fails at the same boundary.

round(x) breaks ties toward positive infinity. round(3.5) is 4, round(-3.5) is -3 and round(-2.5) is -2.

clamp(x, min, max) is the composition min(max(x, min), max). When min exceeds max the upper bound wins, so the result is max.

atan2(y, x) returns the angle of the point (x, y) from the positive x axis, between negative PI and PI.

hypot(x, y) returns the square root of x * x + y * y and avoids intermediate overflow.

The sign of a zero result is not part of this contract.

A result too small to be a normal F64 stays finite. It becomes a subnormal value or zero and never fails the finite boundary. Which of the two occurs is unspecified.

Transcendental results are approximations with target-defined accuracy, so comparisons use a tolerance. These identities are exact: exp(0) is 1, log(1) is 0, log2(8) is 3, log10(100) is 2 and pow(2, 3) is 8. An approximating function may still round when its mathematical result is exactly representable, so a result that happens to be a whole number is not guaranteed exact: hypot(20, 99) need not be exactly 101. Every constant is the nearest F64 to its mathematical value.

Access and return contracts

All parameters use shared access. No function requires mutable access. Every function returns a fresh F64 value.

Backend behaviour

HTML-JS lowers each function to a JavaScript Math expression. clamp lowers to Math.min(Math.max(x, min), max). Constants fold into the expression at compile time.

HTML-Wasm lacks a Wasm lowering for these functions. See progress matrix for current target availability.

Unsupported source forms

  • Direct symbol-path clauses such as @core/math/sin face rejection. Use direct selections or a namespace clause.
  • Named arguments lack support. Binding-backed calls stay positional-only.
  • The package does not expose receiver methods, opaque types or Error! channels.

Deferred surfaces

This package has an F64-only API. It provides no F16/F32 overloads or generic Math API. An F64 parameter receives an F64 value, not an implicit conversion from a bound integer or narrower float. Use plain cast at a concrete typed receiver when conversion is needed. A cast around an F64 result doesn't add native lower-precision support. Richer Math and Wasm lowerings remain deferred. Fast Math has a separate contract and doesn't change these regular helpers. See Casts and Packages and builders progress.

Related concepts