Math v0.1.0
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
Core0.1.0
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
- Exponential and logarithmic —
E,Ln2,Log2E,Ln10, andLog10E. - Circular —
Pi,Tau,HalfPi,QuarterPi,InvPi, andInvTau. - Radicals —
Sqrt2andInvSqrt2. - Angle conversion —
RadPerDeg,DegPerRad,DegToRad, andRadToDeg.
All constants are float64 values rounded to the nearest representable value. See the constants reference for the complete list.
Elementary functions
| Category | Functions |
|---|---|
| Trigonometric | Sin, Cos, Tan, Cotan, SinCos |
| Inverse trigonometric | ArcSin, ArcCos, ArcTan, ArcTan2, ArcCot |
| Hyperbolic | Sinh, Cosh, Tanh, Cotanh |
| Inverse hyperbolic | ArcSinh, ArcCosh, ArcTanh |
| Exponential | Exp, Exp2, Expm1 |
| Logarithmic | Log, Log2, Log10, Log1p |
| Powers and roots | Pow, PowInteger, Sqrt, Cbrt, Hypot |
| Magnitude and comparison | Abs, 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
Floor,Ceil,Trunc, andRoundprovide the usual directed and ties-away rounding rules.RoundTiesToEvenimplements IEEE 754's default halfway rule.RoundToselects a rule throughRoundingMode.ToIntegerChecked<T>rounds and converts without silently saturating an out-of-range value.Modcomputes the exact floating-point remainder with the sign of the dividend.
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
| Purpose | API |
|---|---|
| Classification | IsNan, IsInfinite, IsFinite, IsZero, IsSubnormal, IsNormal, Classify, FloatClass |
| Sign handling | SignBit, CopySign |
| Adjacent values | NextAfter, NextUp, NextDown |
| Special values | PositiveInfinity64, PositiveInfinity32, QuietNan64, QuietNan32 |
| Decomposition | ExponentOf, Decompose, SplitIntegral |
| Scaling | ScaleByPowerOfTwo |
| Error measurement | UlpDistance |
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:
AbsChecked<T>andAbsWrapping<T>compute a magnitude with explicit overflow behavior.Minimum<T>,Maximum<T>, andClamp<T>provide scalar ordering operations.GcdChecked<T>andLcmChecked<T>implement number-theory operations.PowChecked<T>raises an integer to a non-negative integer power.
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
| Name | Summary |
|---|---|
FloatClass | The five kinds IEEE 754 sorts every value into. |
Complex | A complex number real + imaginary·i over float64. |
Complex32 | A complex number real + imaginary·i over float32. |
RoundingMode | Which way a value that falls between two integers should go. |
Functions
| Name | Summary |
|---|---|
Abs | Computes the absolute value of x at float32 precision. |
DegToRad | Converts an angle from degrees to radians. |
RadToDeg | Converts an angle from radians to degrees. |
ArcCos | Computes the inverse cosine of x in radians. |
ArcCot | Computes the inverse cotangent of x in radians. |
ArcSinh | The inverse hyperbolic sine: the value whose hyperbolic sine is x. |
ArcCosh | The inverse hyperbolic cosine: the non-negative value whose hyperbolic cosine is x. |
ArcTanh | The inverse hyperbolic tangent: the value whose hyperbolic tangent is x. |
ArcSin | Computes the inverse sine of x in radians. |
ArcTan | Computes the inverse tangent of x in radians. |
ArcTan2 | The angle from the positive x axis to the point (x, y), in radians, in (-Pi, Pi]. |
Cbrt | Computes the real cube root of x. |
Ceil | Rounds x toward positive infinity to an integral value. |
IsNan | Whether value is a NaN. |
IsInfinite | Determines whether value is an infinity at float32 precision. |
IsFinite | Whether value is an ordinary number: neither a NaN nor an infinity. |
IsZero | Whether value is either zero. |
IsSubnormal | Whether value is subnormal: nonzero, but too small to carry a full significand. |
IsNormal | Whether value is an ordinary value at full precision. |
Classify | Which of the five kinds value belongs to. |
SignBit | Whether value's sign bit is set. |
CopySign | magnitude carrying the sign of sign. |
NextAfter | The value adjacent to from in the direction of toward. |
NextUp | Finds the next float32 value above value. |
NextDown | Finds the next float32 value below value. |
PositiveInfinity64 | Positive infinity. |
PositiveInfinity32 | Positive infinity at the narrow width. |
QuietNan64 | A quiet NaN. |
QuietNan32 | A quiet NaN at the narrow width. |
ScaleByPowerOfTwo | value multiplied by two raised to exponent, with one rounding. |
ExponentOf | The exponent of value: the power of two of its most significant bit. |
Decompose | Splits value into a fraction in [0.5, 1) and a power of two, so that fraction * 2^exponent == value. |
SplitIntegral | Splits value into its integral and fractional parts, both carrying its sign. |
UlpDistance | How many representable values lie between left and right. |
Cos | Computes the cosine of x, where x is in radians. |
Cosh | Computes the hyperbolic cosine of x. |
Cotan | Computes the cotangent of x, where x is in radians. |
Cotanh | Computes the hyperbolic cotangent of x. |
Exp | Computes e raised to x. |
Exp2 | Computes two raised to x. |
Expm1 | Computes Exp(x) - 1 without losing precision near zero. |
Floor | Rounds x toward negative infinity to an integral value. |
Hypot | Computes 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. |
Log | Computes the natural logarithm of x. |
Log10 | Computes the base-ten logarithm of x. |
Log1p | Computes Log(1 + x) without losing precision near zero. |
Log2 | Computes the base-two logarithm of x. |
Max | Selects the larger of a and b. |
Min | Selects the smaller of a and b. |
Mod | Computes the floating-point remainder of x / y, with the sign of x. |
Pow | Raises x to the power y. |
PowInteger | base raised to the integer power exponent. |
Round | Rounds x to the nearest integral value, with halfway cases away from zero. |
RoundTiesToEven | The nearest integer to value, with a halfway value going to the even neighbour. |
RoundTo | Rounds 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. |
Sin | Computes the sine of x, where x is in radians. |
SinCos | Computes the sine and cosine of angle from one argument reduction. |
Sinh | Computes the hyperbolic sine of x. |
Sqrt | Computes the correctly rounded square root of x at float32 precision. |
Tan | Computes the tangent of x, where x is in radians. |
Tanh | Computes the hyperbolic tangent of x. |
Trunc | Rounds x toward zero to an integral value at float32 precision. |
Constants
| Name | Summary |
|---|---|
NoExponent | What ExponentOf reports for zero, which has no exponent at all. |
IndeterminateExponent | What ExponentOf reports for a NaN or an infinity. |
E | Euler's number, the base of the natural logarithm, rounded to the nearest float64. |
Ln2 | The natural logarithm of two, rounded to the nearest float64. |
Log2E | The base-two logarithm of E, rounded to the nearest float64. |
Ln10 | The natural logarithm of ten, rounded to the nearest float64. |
Log10E | The base-ten logarithm of E, rounded to the nearest float64. |
Pi | The ratio of a circle's circumference to its diameter, rounded to the nearest float64. |
Tau | One full turn in radians, equal to 2 * Pi and rounded to the nearest float64. |
HalfPi | One quarter turn in radians, equal to Pi / 2 and rounded to the nearest float64. |
QuarterPi | One eighth turn in radians, equal to Pi / 4 and rounded to the nearest float64. |
InvPi | The reciprocal of Pi, rounded to the nearest float64. |
InvTau | The reciprocal of Tau, rounded to the nearest float64. |
Sqrt2 | The square root of two, rounded to the nearest float64. |
InvSqrt2 | The reciprocal of Sqrt2, rounded to the nearest float64. |
RadPerDeg | Radians per degree, equal to Pi / 180 and rounded to the nearest float64. |
DegPerRad | Degrees per radian, equal to 180 / Pi and rounded to the nearest float64. |