Numeric Functions

Integer Functions

The following standard functions are defined.

FunctionNot available underPackageDescription
is_odd method and propertyArithmeticPackagereturns true if the value is an odd number, otherwise false
is_even method and propertyArithmeticPackagereturns true if the value is an even number, otherwise false
minLogicPackagereturns the smaller of two numbers
maxLogicPackagereturns the larger of two numbers
to_floatno_floatBasicMathPackageconvert the value into f64 (f32 under f32_float)
to_decimalnon-decimalBasicMathPackageconvert the value into Decimal

Signed Numeric Functions

The following standard functions are defined in the ArithmeticPackage (excluded when using a raw Engine) and operate on i8, i16, i32, i64, f32, f64 and Decimal (requires decimal) only.

FunctionDescription
absabsolute value
signreturns (INT) −1 if negative, +1 if positive, 0 if zero
is_zero method and propertyreturns true if the value is zero, otherwise false

Floating-Point Functions

The following standard functions are defined in the BasicMathPackage (excluded when using a raw Engine) and operate on f64 (f32 under f32_float) and Decimal (requires decimal) only.

CategorySupports DecimalFunctions
Trigonometryyessin, cos, tan
Trigonometrynosinh, cosh, tanh in radians, hypot(x,y)
Arc-trigonometrynoasin, acos, atan(v), atan(x,y), asinh, acosh, atanh in radians
Square rootyessqrt
Exponentialyesexp (base e)
Logarithmicyesln (base e)
Logarithmicyeslog (base 10)
Logarithmicnolog(x,base)
Roundingyesfloor, ceiling, round, int, fraction methods and properties
Conversionyesto_int, to_decimal (requires decimal), to_float (not under no_float)
Conversionnoto_degrees, to_radians
Comparisonyesmin, max (also inter-operates with integers)
Testingnois_nan, is_finite, is_infinite methods and properties

Decimal Rounding Functions

The following rounding methods are defined in the BasicMathPackage (excluded when using a raw Engine) and operate on Decimal only, which requires the decimal feature.

Rounding typeBehaviorMethods
Nonefloor, ceiling, int, fraction methods and properties
Banker’s roundinground to integerround method and property
Banker’s roundinground to specified number of decimal pointsround(decimal points)
Round upaway from zeroround_up(decimal points)
Round downtowards zeroround_down(decimal points)
Round half-upmid-point away from zeroround_half_up(decimal points)
Round half-downmid-point towards zeroround_half_down(decimal points)

Parsing Functions

The following standard functions are defined in the BasicMathPackage (excluded when using a raw Engine) to parse numbers.

FunctionNo available underDescription
parse_intconverts a string to INT with an optional radix
parse_floatno_float and non-decimalconverts a string to FLOAT (Decimal under no_float and decimal)
parse_decimalnon-decimalconverts a string to Decimal

Formatting Functions

The following standard functions are defined in the BasicStringPackage (excluded when using a raw Engine) to convert integer numbers into a string of hex, octal or binary representations.

FunctionDescription
to_binaryconverts an integer number to binary
to_octalconverts an integer number to octal
to_hexconverts an integer number to hex

These formatting functions are defined for all available integer numbers – i.e. INT, u8, i8, u16, i16, u32, i32, u64, i64, u128 and i128 unless disabled by feature flags.

Floating-point Constants

The following functions return standard mathematical constants.

FunctionDescription
PIreturns the value of π
Ereturns the value of e

Numerical Functions for Scientific Computing

Check out the rhai-sci crate for more numerical functions.