Math v0.1.0

Source
Mathematical constants and functions

Installation

Add Math to the [Dependencies] table of your package's Rux.toml:

[Dependencies]
Math = { Namespace = "Rux", Version = "0.1.0" }

Standard packages are declared by hand: the registry does not serve them, so rux add Rux/Math cannot add them.

Dependencies

Guide

Portable mathematical constants, elementary functions, floating-point utilities, and generic integer operations for Rux. The package implements its elementary functions directly instead of forwarding them to the host system's libm. Argument reduction, polynomial kernels, and IEEE 754 edge-case handling therefore remain consistent across supported targets. The implementation allocates no memory and depends only on Rux/Core.

Example

import Math::{ DegToRad, Hypot, SinCos };

let angle = DegToRad(30.0);
let (sine, cosine) = SinCos(angle);
let diagonal = Hypot(3.0, 4.0);

SinCos performs argument reduction once and returns (sine, cosine), which is more efficient than calling Sin and Cos separately for the same angle.

API overview

Most floating-point functions provide both float64 and float32 overloads. The float32 forms use a wide intermediate where doing so improves accuracy, then round once to the requested width.

Constants and angles

All constants are float64 values rounded to the nearest representable value. See the constants reference for the complete list.

Elementary functions

CategoryFunctions
TrigonometricSin, Cos, Tan, Cotan, SinCos
Inverse trigonometricArcSin, ArcCos, ArcTan, ArcTan2, ArcCot
HyperbolicSinh, Cosh, Tanh, Cotanh
Inverse hyperbolicArcSinh, ArcCosh, ArcTanh
ExponentialExp, Exp2, Expm1
LogarithmicLog, Log2, Log10, Log1p
Powers and rootsPow, PowInteger, Sqrt, Cbrt, Hypot
Magnitude and comparisonAbs, Min, Max

Expm1(x) computes Exp(x) - 1, and Log1p(x) computes Log(1 + x), without discarding the small result near zero. PowInteger uses repeated squaring and preserves exact integer powers when the intermediate values are exactly representable; use Pow for a general floating-point exponent.

Rounding and conversion

ToIntegerChecked<T> returns true on failure and writes zero to its output. The destination pointer must be non-null and writable:

import Math::{ RoundingMode, ToIntegerChecked };

var answer: int32 = 0;
let failed = ToIntegerChecked<int32>(42.5, 0, RoundingMode::ToNearestTiesEven, @answer);

Floating-point inspection and manipulation

ExponentOf reports NoExponent for zero and IndeterminateExponent for a NaN or infinity. UlpDistance counts representable steps rather than subtracting values, making it useful for numerical error checks across the full floating-point range.

Generic integer operations

The integer APIs are generic over signed and unsigned integer widths:

Checked operations follow the same convention as checked arithmetic in Rux/Core: they return true when the mathematical result is not representable. Their raw output pointers must be non-null, correctly aligned, and writable for one value for the duration of the call.

Complex numbers

Complex over float64 and Complex32 over float32 have +, -, * and /, Abs, Arg, Norm, Conjugate, Negate, Scale, FromPolar, and Exp, Log, Sqrt and Pow on the principal branch. Division uses Smith's method and the magnitude goes through Hypot, so neither overflows for parts far from one.

Numerical behavior

  • IEEE 754 special values are part of each contract. Signed zeros, infinities, NaNs, subnormals, and domain errors are handled deliberately rather than left to host-library behavior.
  • Accuracy is documented per function. The primary trigonometric, exponential, logarithmic, and root operations target an error of at most one ulp; inverse hyperbolic functions document a two-ulp bound.
  • Large-angle reduction retains precision. Trigonometric functions reduce large finite inputs without first rounding them to a low-precision multiple of Pi / 2.
  • No allocation or hidden global state. Every operation works on ordinary values and caller-provided outputs.

The bit-level implementation currently supports x86-64 and AArch64 targets.

Value and ownership model

The package owns no storage. FloatClass and RoundingMode are structural Copy values, and multi-result operations such as SinCos return tuples. Ordinary inputs are passed by value. Raw pointers appear only in checked operations and decomposition helpers that write an additional result supplied by the caller.

Index

Types

NameSummary
FloatClassThe five kinds IEEE 754 sorts every value into.
ComplexA complex number real + imaginary·i over float64.
Complex32A complex number real + imaginary·i over float32.
RoundingModeWhich way a value that falls between two integers should go.

Functions

NameSummary
AbsComputes the absolute value of x at float32 precision.
DegToRadConverts an angle from degrees to radians.
RadToDegConverts an angle from radians to degrees.
ArcCosComputes the inverse cosine of x in radians.
ArcCotComputes the inverse cotangent of x in radians.
ArcSinhThe inverse hyperbolic sine: the value whose hyperbolic sine is x.
ArcCoshThe inverse hyperbolic cosine: the non-negative value whose hyperbolic cosine is x.
ArcTanhThe inverse hyperbolic tangent: the value whose hyperbolic tangent is x.
ArcSinComputes the inverse sine of x in radians.
ArcTanComputes the inverse tangent of x in radians.
ArcTan2The angle from the positive x axis to the point (x, y), in radians, in (-Pi, Pi].
CbrtComputes the real cube root of x.
CeilRounds x toward positive infinity to an integral value.
IsNanWhether value is a NaN.
IsInfiniteDetermines whether value is an infinity at float32 precision.
IsFiniteWhether value is an ordinary number: neither a NaN nor an infinity.
IsZeroWhether value is either zero.
IsSubnormalWhether value is subnormal: nonzero, but too small to carry a full significand.
IsNormalWhether value is an ordinary value at full precision.
ClassifyWhich of the five kinds value belongs to.
SignBitWhether value's sign bit is set.
CopySignmagnitude carrying the sign of sign.
NextAfterThe value adjacent to from in the direction of toward.
NextUpFinds the next float32 value above value.
NextDownFinds the next float32 value below value.
PositiveInfinity64Positive infinity.
PositiveInfinity32Positive infinity at the narrow width.
QuietNan64A quiet NaN.
QuietNan32A quiet NaN at the narrow width.
ScaleByPowerOfTwovalue multiplied by two raised to exponent, with one rounding.
ExponentOfThe exponent of value: the power of two of its most significant bit.
DecomposeSplits value into a fraction in [0.5, 1) and a power of two, so that fraction * 2^exponent == value.
SplitIntegralSplits value into its integral and fractional parts, both carrying its sign.
UlpDistanceHow many representable values lie between left and right.
CosComputes the cosine of x, where x is in radians.
CoshComputes the hyperbolic cosine of x.
CotanComputes the cotangent of x, where x is in radians.
CotanhComputes the hyperbolic cotangent of x.
ExpComputes e raised to x.
Exp2Computes two raised to x.
Expm1Computes Exp(x) - 1 without losing precision near zero.
FloorRounds x toward negative infinity to an integral value.
HypotComputes the Euclidean length Sqrt(x * x + y * y) without intermediate overflow or underflow.
AbsChecked<T>The magnitude of value, reporting whether it is representable.
AbsWrapping<T>The magnitude of value, wrapping where it is not representable.
Minimum<T>The smaller of two integers.
Maximum<T>The larger of two integers.
Clamp<T>value brought into [low, high].
GcdChecked<T>The greatest common divisor of left and right, reporting whether it is representable.
LcmChecked<T>The least common multiple of left and right, reporting whether it is representable.
PowChecked<T>base raised to exponent, reporting whether the true power is representable.
LogComputes the natural logarithm of x.
Log10Computes the base-ten logarithm of x.
Log1pComputes Log(1 + x) without losing precision near zero.
Log2Computes the base-two logarithm of x.
MaxSelects the larger of a and b.
MinSelects the smaller of a and b.
ModComputes the floating-point remainder of x / y, with the sign of x.
PowRaises x to the power y.
PowIntegerbase raised to the integer power exponent.
RoundRounds x to the nearest integral value, with halfway cases away from zero.
RoundTiesToEvenThe nearest integer to value, with a halfway value going to the even neighbour.
RoundToRounds value in the requested direction at float32 precision.
ToIntegerChecked<T>value rounded in the named direction and converted to an integer, reporting whether it fits.
SinComputes the sine of x, where x is in radians.
SinCosComputes the sine and cosine of angle from one argument reduction.
SinhComputes the hyperbolic sine of x.
SqrtComputes the correctly rounded square root of x at float32 precision.
TanComputes the tangent of x, where x is in radians.
TanhComputes the hyperbolic tangent of x.
TruncRounds x toward zero to an integral value at float32 precision.

Constants

NameSummary
NoExponentWhat ExponentOf reports for zero, which has no exponent at all.
IndeterminateExponentWhat ExponentOf reports for a NaN or an infinity.
EEuler's number, the base of the natural logarithm, rounded to the nearest float64.
Ln2The natural logarithm of two, rounded to the nearest float64.
Log2EThe base-two logarithm of E, rounded to the nearest float64.
Ln10The natural logarithm of ten, rounded to the nearest float64.
Log10EThe base-ten logarithm of E, rounded to the nearest float64.
PiThe ratio of a circle's circumference to its diameter, rounded to the nearest float64.
TauOne full turn in radians, equal to 2 * Pi and rounded to the nearest float64.
HalfPiOne quarter turn in radians, equal to Pi / 2 and rounded to the nearest float64.
QuarterPiOne eighth turn in radians, equal to Pi / 4 and rounded to the nearest float64.
InvPiThe reciprocal of Pi, rounded to the nearest float64.
InvTauThe reciprocal of Tau, rounded to the nearest float64.
Sqrt2The square root of two, rounded to the nearest float64.
InvSqrt2The reciprocal of Sqrt2, rounded to the nearest float64.
RadPerDegRadians per degree, equal to Pi / 180 and rounded to the nearest float64.
DegPerRadDegrees per radian, equal to 180 / Pi and rounded to the nearest float64.