Skip to content

math #

Description

math provides commonly used mathematical functions for trigonometry, logarithms, etc.

factorial(n) returns n! for nonnegative integers and uses gamma(n + 1) for other values. When the result overflows f64, it returns positive infinity, including for n >= 171 and positive infinity. For nonnegative inputs, use is_inf(result, 1) to detect this overflow; factorial(170) remains finite. The gamma extension also returns positive infinity at the pole n = -1, so infinity alone does not indicate overflow for arbitrary inputs.

For floating point values, math.abs maps both signs of zero to positive zero. math.expm1 preserves its input's zero sign, so math.expm1(-0.0) returns negative zero. math.min and math.max return NaN when either floating point argument is NaN. For equal non-NaN values, including opposite signs of zero, both return their second argument.

modf preserves the input sign on both its integer and fractional results, including zero. log_gamma returns the logarithm of the absolute value of Gamma; either positive or negative infinity as input returns positive infinity. log2(x) returns an exact integer exponent for every representable positive power of two, including subnormal inputs. log1p(x) returns positive infinity for positive infinity, negative infinity for -1, and NaN for values below -1. logf(x) provides the natural logarithm for f32 inputs, including in pure V builds. Its special cases are the same as for log. sqrt(x) rounds its result to the nearest f64, including in pure V builds and for subnormal inputs. It preserves signed zero and returns NaN for negative inputs.

The trigonometric functions sin, cos, sincos, tan and cot reduce large finite angles with multiword arithmetic before evaluation, including inputs near a multiple of pi/2 and values up to math.max_f64. Infinite angles produce NaN.

lgamma aliases log_gamma. remainder(x, y) uses the IEEE nearest-integer, ties-to-even remainder and preserves the dividend's sign on zero. On the C backend, fma(x, y, z) computes one rounded multiply-add, including intermediate product overflow. The C backend uses its math library for both operations. Other backends use portable remainder and a two-round multiply-add fallback. floor_mod(x, y) instead follows the divisor's sign, including zero. to_degrees and to_radians convert angle units. is_power_of_two and next_power_of_two operate on u64; the latter returns zero when the next power cannot fit. classify distinguishes NaN, infinite, normal, subnormal and zero.

wrapping(value) and saturating(value) create integer arithmetic wrappers for i8, i16, i32, i64, u8, u16, u32, u64, and int. Their +, -, and * operators wrap modulo the integer width or clamp to its bounds, respectively. neg() applies the corresponding negation policy. For signed minimum divided by -1, wrapping returns the minimum and saturation returns the maximum. Both % operators return zero for that pair without overflowing the backend's division operation. Zero divisors follow ordinary integer division behavior.

gcd(a, b) and lcm(a, b) accept matching integer types from i8 through i64, u8 through u64, and int. They compute with unsigned magnitudes, including signed minimum values, and clamp an unrepresentable result to the type maximum. gcd_many(values) computes the common divisor before applying that clamp; its empty result is zero. lcm_many(values) returns one for an empty input and zero when any element is zero.

Constants #

const epsilon = 2.2204460492503130808472633361816E-16
const e = 2.71828182845904523536028747135266249775724709369995957496696763
const pi = 3.14159265358979323846264338327950288419716939937510582097494459
const pi_2 = pi / 2.0
const pi_4 = pi / 4.0
const phi = 1.61803398874989484820458683436563811772030917980576286213544862
const tau = 6.28318530717958647692528676655900576839433879875021164194988918
const one_over_tau = 1.0 / tau
const one_over_pi = 1.0 / pi
const tau_over2 = tau / 2.0
const tau_over4 = tau / 4.0
const tau_over8 = tau / 8.0
const sqrt2 = 1.41421356237309504880168872420969807856967187537694807317667974
const sqrt_3 = 1.73205080756887729352744634150587236694280525381038062805580697
const sqrt_5 = 2.23606797749978969640917366873127623544061835961152572427089724
const sqrt_e = 1.64872127070012814684865078781416357165377610071014801157507931
const sqrt_pi = 1.77245385090551602729816748334114518279754945612238712821380779
const sqrt_tau = 2.50662827463100050241576528481104525300698674060993831662992357
const sqrt_phi = 1.27201964951406896425242246173749149171560804184009624861664038
const ln2 = 0.693147180559945309417232121458176568075500134360255254120680009
const log2_e = 1.0 / ln2
const ln10 = 2.30258509299404568401799145468436420760110148862877297603332790
const log10_e = 1.0 / ln10
const two_thirds = 0.66666666666666666666666666666666666666666666666666666666666667
const max_f32 = 3.40282346638528859811704183484516925440e+38 // 2**127 * (2**24 - 1) / 2**23

Floating-point limit values max is the largest finite value representable by the type. smallest_non_zero is the smallest positive, non-zero value representable by the type.

const smallest_non_zero_f32 = 1.401298464324817070923729583289916131280e-45 // 1 / 2**(127 - 1 + 23)
const max_f64 = 1.797693134862315708145274237317043567981e+308 // 2**1023 * (2**53 - 1) / 2**52
const smallest_non_zero_f64 = 4.940656458412465441765687928682213723651e-324

fn abs #

@[inline]
fn abs[T](a T) T

abs returns the absolute value of a. Floating point zero always becomes positive zero.

fn acos #

@[inline]
fn acos(x f64) f64

acos returns the arccosine, in radians, of x.

special case is: acos(x) = nan if x < -1 or x > 1

fn acosh #

fn acosh(x f64) f64

acosh returns the non-negative area hyperbolic cosine of x

fn alike #

fn alike(a f64, b f64) bool

alike checks if a and b are equal

fn angle_diff #

@[inline]
fn angle_diff(radian_a f64, radian_b f64) f64

angle_diff calculates the difference between angles in radians

fn aprox_cos #

fn aprox_cos(a f64) f64

aprox_cos returns an approximation of cos(a) made using lolremez

fn aprox_sin #

fn aprox_sin(a f64) f64

aprox_sin returns an approximation of sin(a) made using lolremez

fn asin #

fn asin(x_ f64) f64

asin returns the arcsine, in radians, of x.

special cases are: asin(±0) = ±0 asin(x) = nan if x < -1 or x > 1

fn asinh #

fn asinh(x f64) f64

asinh returns the area hyperbolic sine of x

fn atan #

fn atan(x f64) f64

atan returns the arctangent, in radians, of x.

special cases are: atan(±0) = ±0 atan(±inf) = ±pi/2.0

fn atan2 #

fn atan2(y f64, x f64) f64

atan2 returns the arc tangent of y/x, using the signs of the two to determine the quadrant of the return value.

special cases are (in order): atan2(y, nan) = nan atan2(nan, x) = nan atan2(+0, x>=0) = +0 atan2(-0, x>=0) = -0 atan2(+0, x<=-0) = +pi atan2(-0, x<=-0) = -pi atan2(y>0, 0) = +pi/2.0 atan2(y<0, 0) = -pi/2.0 atan2(+inf, +inf) = +pi/4 atan2(-inf, +inf) = -pi/4 atan2(+inf, -inf) = 3pi/4 atan2(-inf, -inf) = -3pi/4 atan2(y, +inf) = 0 atan2(y>0, -inf) = +pi atan2(y<0, -inf) = -pi atan2(+inf, x) = +pi/2.0 atan2(-inf, x) = -pi/2.0

fn atanh #

fn atanh(x f64) f64

atanh returns the area hyperbolic tangent of x

fn cbrt #

fn cbrt(a f64) f64

cbrt returns the cube root of a.

special cases are: cbrt(±0) = ±0 cbrt(±inf) = ±inf cbrt(nan) = nan

fn ceil #

fn ceil(x f64) f64

ceil returns the least integer value greater than or equal to x.

special cases are: ceil(±0) = ±0 ceil(±inf) = ±inf ceil(nan) = nan

fn clamp #

@[inline]
fn clamp(x f64, a f64, b f64) f64

clamp returns x constrained between a and b

fn classify #

fn classify(x f64) FloatClass

classify reports the class of x.

fn clip #

@[inline]
fn clip[T](x T, min_value T, max_value T) T

clip constrain the given value x, to lie between two further values min_value and max_value. See: https://registry.khronos.org/OpenGL-Refpages/gl4/html/clamp.xhtml Also: https://en.wikipedia.org/wiki/Clamp_(function)

fn close #

fn close(actual f64, expected f64) bool

close checks if actual and expected are within 1e-14 of each other

Note: the actual and expected parameters are NOT symmetrical. If you compare against a known expected number, put it in the second argument, especially if it is 0.

fn copysign #

@[inline]
fn copysign(x f64, y f64) f64

copysign returns a value with the magnitude of x and the sign of y

fn cos #

@[inline]
fn cos(a f64) f64

cos calculates cosine in radians (float64)

fn cosf #

@[inline]
fn cosf(a f32) f32

cosf calculates cosine in radians (float32)

fn cosh #

fn cosh(x f64) f64

cosh returns the hyperbolic cosine of x in radians

special cases are: cosh(±0) = 1 cosh(±inf) = +inf cosh(nan) = nan

fn cot #

fn cot(a f64) f64

cot calculates cotangent of a number

fn count_digits #

fn count_digits(number i64) int

count_digits return the number of digits in the number passed. Number argument accepts any integer type (i8|i16|int|isize|i64) and will be cast to i64

fn cube #

@[inline]
fn cube[T](x T) T

cube returns the cube of the argument x, i.e. x * x * x

fn cubic_bezier #

@[direct_array_access; inline]
fn cubic_bezier(t f64, p []BezierPoint) BezierPoint

cubic_bezier returns a linear interpolation between the control points, specified by their X and Y coordinates in a single array of points p, and given the parameter t, varying between 0.0 and 1.0 . When t == 0.0, the output is P[0] . When t == 1.0, the output is P[3] . The points x[1],y[1] and x[2],y[2], serve as attractors.

fn cubic_bezier_a #

@[direct_array_access; inline]
fn cubic_bezier_a(t f64, x []f64, y []f64) BezierPoint

cubic_bezier_a returns a linear interpolation between the control points, specified by their X and Y coordinates in 2 arrays, and given the parameter t, varying between 0.0 and 1.0 . When t == 0.0, the output is x[0],y[0] . When t == 1.0, the output is x[3],y[3] . The points x[1],y[1] and x[2],y[2], serve as attractors.

fn cubic_bezier_coords #

@[inline]
fn cubic_bezier_coords(t f64, x0 f64, x1 f64, x2 f64, x3 f64, y0 f64, y1 f64, y2 f64, y3 f64) BezierPoint

cubic_bezier_coords returns a linear interpolation between the control points, specified by their X and Y coordinates, and given the parameter t, varying between 0.0 and 1.0 . When t == 0.0, the output is x0,y0 . When t == 1.0, the output is x3,y3 . The points x1,y1 and x2,y2, serve as attractors.

fn cubic_bezier_fa #

@[direct_array_access; inline]
fn cubic_bezier_fa(t f64, x [4]f64, y [4]f64) BezierPoint

cubic_bezier_fa returns a linear interpolation between the control points, specified by their X and Y coordinates in 2 fixed arrays, and given the parameter t, varying between 0.0 and 1.0 . When t == 0.0, the output is x[0],y[0] . When t == 1.0, the output is x[3],y[3] . The points x[1],y[1] and x[2],y[2], serve as attractors.

fn degrees #

@[inline]
fn degrees(radians f64) f64

degrees converts an angle in radians to a corresponding angle in degrees.

fn digits #

fn digits(num i64, params DigitParams) []int

You can also use it, with an explicit reverse: true parameter, (it will do a reverse of the result array internally => slower):

Examples

assert math.digits(12345, base: 10) == [5,4,3,2,1]
assert math.digits(12345, reverse: true) == [1,2,3,4,5]

fn divide_euclid #

@[inline]
fn divide_euclid[T](numer T, denom T) DivResult[T]

divide_euclid returns the Euclidean version of the result of dividing numer to denom

fn divide_floored #

@[inline]
fn divide_floored[T](numer T, denom T) DivResult[T]

divide_floored returns the floored version of the result of dividing numer to denom

fn divide_truncated #

@[inline]
fn divide_truncated[T](numer T, denom T) DivResult[T]

divide_truncated returns the truncated version of the result of dividing numer to denom

fn egcd #

fn egcd(a i64, b i64) (i64, i64, i64)

egcd returns (gcd(a, b), x, y) such that |ax + by| = gcd(a, b)

fn erf #

fn erf(a f64) f64

erf returns the error function of x.

special cases are: erf(+inf) = 1 erf(-inf) = -1 erf(nan) = nan

fn erfc #

fn erfc(a f64) f64

erfc returns the complementary error function of x.

special cases are: erfc(+inf) = 0 erfc(-inf) = 2 erfc(nan) = nan

fn exp #

@[inline]
fn exp(x f64) f64

exp returns e**x, the base-e exponential of x.

fn exp2 #

@[inline]
fn exp2(x f64) f64

exp2 returns 2**x, the base-2 exponential of x.

fn exp_decay #

@[inline]
fn exp_decay[T](a T, b T, decay f64, delta_time_seconds f64) T

exp_decay returns a frame independent exponential decay value between a and b using delta_time_seconds. decay is supposed to be useful in the range 1.0 to 25.0. From slow to fast. The function is a frame rate independent (approximation) of the well-known lerp or mix (linear interpolation) function. It is ported to V from the pseudo code shown towards the end of the video https://youtu.be/LSNQuFEDOyQ?t=2977

Note: Thanks to Freya Holmér for the function and the work done in this field.

fn expm1 #

fn expm1(x f64) f64

expm1 calculates e**x - 1 special cases are: expm1(+inf) = +inf expm1(-inf) = -1 expm1(nan) = nan expm1(+/-0) = +/-0

fn f32_bits #

@[inline]
fn f32_bits(f f32) u32

f32_bits returns the IEEE 754 binary representation of f, with the sign bit of f and the result in the same bit position. f32_bits(f32_from_bits(x)) == x.

fn f32_from_bits #

@[inline]
fn f32_from_bits(b u32) f32

f32_from_bits returns the floating-point number corresponding to the IEEE 754 binary representation b, with the sign bit of b and the result in the same bit position. f32_from_bits(f32_bits(x)) == x.

fn f64_bits #

@[inline]
fn f64_bits(f f64) u64

f64_bits returns the IEEE 754 binary representation of f, with the sign bit of f and the result in the same bit position, and f64_bits(f64_from_bits(x)) == x.

fn f64_from_bits #

@[inline]
fn f64_from_bits(b u64) f64

f64_from_bits returns the floating-point number corresponding to the IEEE 754 binary representation b, with the sign bit of b and the result in the same bit position. f64_from_bits(f64_bits(x)) == x.

fn factorial #

fn factorial(n f64) f64

factorial calculates the factorial of the provided value. Non-integer values use gamma(n + 1); overflow returns positive infinity.

fn factoriali #

fn factoriali(n int) i64

factoriali returns 1 for n <= 0 and -1 if the result is too large for a 64 bit integer

fn floor #

fn floor(x f64) f64

floor returns the greatest integer value less than or equal to x.

special cases are: floor(±0) = ±0 floor(±inf) = ±inf floor(nan) = nan

fn floor_mod #

fn floor_mod(x f64, y f64) f64

floor_mod returns x mod y floored toward negative infinity, so the result has the sign of y, including an exact zero (Python's floating-point %).

fn floorf #

fn floorf(x f32) f32

floorf returns the greatest integer value less than or equal to x.

special cases are: floor(±0) = ±0 floor(±inf) = ±inf floor(nan) = nan

fn fma #

fn fma(x f64, y f64, z f64) f64

fma returns x*y + z with a single rounding, using the C math library.

fn fmod #

fn fmod(x f64, y f64) f64

fmod returns the floating-point remainder of number / denom (rounded towards zero)

fn frexp #

fn frexp(x f64) (f64, int)

frexp breaks f into a normalized fraction and an integral power of two. It returns frac and exp satisfying f == frac × 2**exp, with the absolute value of frac in the interval [½, 1).

special cases are: frexp(±0) = ±0, 0 frexp(±inf) = ±inf, 0 frexp(nan) = nan, 0 pub fn frexp(f f64) (f64, int) { mut y := f64_bits(x) ee := int((y >> 52) & 0x7ff) // special cases if ee == 0 { if x != 0.0 { x1p64 := f64_from_bits(0x43f0000000000000) z,e_ := frexp(x * x1p64) return z,e_ - 64 } return x,0 } else if ee == 0x7ff { return x,0 } e_ := ee - 0x3fe y &= 0x800fffffffffffff y |= 0x3fe0000000000000 return f64_from_bits(y),e_

fn gamma #

fn gamma(a f64) f64

gamma returns the gamma function of x.

special ifs are: gamma(+inf) = +inf gamma(+0) = +inf gamma(-0) = -inf gamma(x) = nan for integer x < 0 gamma(-inf) = nan gamma(nan) = nan

fn gcd #

fn gcd[T](a T, b T) T

gcd calculates greatest common (positive) divisor (or zero if a and b are both zero). It works on magnitudes in the u64 domain, so unlike plain negation it is safe for minimum values; unrepresentable results saturate to the type maximum. Supports int and signed/unsigned integer widths up to 64 bits.

fn gcd_many #

fn gcd_many[T](values []T) T

gcd_many returns the gcd of all values (0 for an empty input).

fn get_high_word #

@[inline]
fn get_high_word(f f64) u32

get_high_word returns high part of the word of f.

fn hypot #

fn hypot(x f64, y f64) f64

hypot returns the hypotenuse of the triangle give two sides.

special cases are: hypot(±inf, y) = +inf hypot(x, ±inf) = +inf hypot(nan, y) = nan hypot(x, nan) = nan

fn ilog_b #

fn ilog_b(x f64) int

ilog_b returns the binary exponent of x as an integer.

special cases are: ilog_b(±inf) = max_i32 ilog_b(0) = min_i32 ilog_b(nan) = max_i32

fn inf #

fn inf(sign int) f64

inf returns positive infinity if sign >= 0, negative infinity if sign < 0.

fn is_finite #

fn is_finite(f f64) bool

is_finite returns true if f is finite

fn is_inf #

fn is_inf(f f64, sign int) bool

is_inf reports whether f is an infinity, according to sign. If sign > 0, is_inf reports whether f is positive infinity. If sign < 0, is_inf reports whether f is negative infinity. If sign == 0, is_inf reports whether f is either infinity.

fn is_nan #

fn is_nan(f f64) bool

is_nan reports whether f is an IEEE 754 ``not-a-number'' value.

fn is_power_of_two #

fn is_power_of_two(n u64) bool

is_power_of_two reports whether n is a positive power of two (0 is not).

fn lcm #

fn lcm[T](a T, b T) T

lcm calculates least common (non-negative) multiple. Results too large to represent saturate to the type maximum instead of overflowing. Supports int and signed/unsigned integer widths up to 64 bits.

fn lcm_many #

fn lcm_many[T](values []T) T

lcm_many returns the lcm of all values (1 for an empty input).

fn ldexp #

@[inline]
fn ldexp(frac f64, exp int) f64

ldexp calculates frac*(2**exp).

fn lgamma #

fn lgamma(x f64) f64

lgamma returns the natural logarithm of the absolute value of Gamma(x). It is an alias of log_gamma, including its NaN, infinity, and pole behavior.

fn log #

@[inline]
fn log(x f64) f64

log returns the natural logarithm of x (float64)

fn log10 #

@[inline]
fn log10(x f64) f64

log10 returns the decimal logarithm of x (float64). The special cases are the same as for log.

fn log1p #

@[inline]
fn log1p(x f64) f64

log1p returns log(1+x).

fn log2 #

@[inline]
fn log2(x f64) f64

log2 returns the binary logarithm of x (float64). The special cases are the same as for log. Exact powers of two return their exponent.

fn log_b #

fn log_b(x f64) f64

log_b returns the binary exponent of x.

fn log_factorial #

fn log_factorial(n f64) f64

log_factorial calculates the log-factorial of the provided value.

fn log_gamma #

fn log_gamma(x f64) f64

log_gamma returns the natural logarithm of the absolute value of Gamma(x).

special ifs are: log_gamma(+inf) = +inf log_gamma(0) = +inf log_gamma(-integer) = +inf log_gamma(-inf) = +inf log_gamma(nan) = nan

fn log_gamma_sign #

fn log_gamma_sign(a f64) (f64, int)

log_gamma_sign returns the natural logarithm and sign (-1 or +1) of Gamma(x)

fn log_n #

fn log_n(x f64, b f64) f64

log_n returns log base b of x

fn logf #

@[inline]
fn logf(x f32) f32

logf returns the natural logarithm of x (float32).

fn max #

@[inline]
fn max[T](a T, b T) T

max returns the maximum of a and b. For floating point types, if either argument is NaN the result is NaN.

fn maxof #

@[inline]
fn maxof[T]() T

maxof returns the maximum value of the type T

fn min #

@[inline]
fn min[T](a T, b T) T

min returns the minimum of a and b. For floating point types, if either argument is NaN the result is NaN.

fn minmax #

fn minmax(a f64, b f64) (f64, f64)

minmax returns the minimum and maximum value of the two provided.

fn minof #

@[inline]
fn minof[T]() T

minof returns the minimum value of the type T

fn mix #

@[inline]
fn mix[T](start T, end T, t T) T

mix performs a linear interpolation (LERP) mix between start and end, using t to weight between them. t should be in the closed interval [0, 1]. For t == 0, the output is x.

Note: mix is calculated in such a way, that the output will be y, when t == 1.0 . See: https://registry.khronos.org/OpenGL-Refpages/gl4/html/mix.xhtml Also: https://en.wikipedia.org/wiki/Linear_interpolation .

fn mod #

fn mod(x f64, y f64) f64

Floating-point mod function. mod returns the floating-point remainder of x/y. The magnitude of the result is less than y and its sign agrees with that of x.

special cases are: mod(±inf, y) = nan mod(nan, y) = nan mod(x, 0) = nan mod(x, ±inf) = x mod(x, nan) = nan

fn modf #

fn modf(f f64) (f64, f64)

modf returns integer and fractional floating-point numbers that sum to f. Both values have the same sign as f.

special cases are: modf(±inf) = ±inf, nan modf(nan) = nan, nan

fn modulo_euclid #

@[inline]
fn modulo_euclid[T](numer T, denom T) T

modulo_euclid returns the Euclidean remainder of dividing numer to denom

fn modulo_floored #

@[inline]
fn modulo_floored[T](numer T, denom T) T

modulo_floored returns the floored remainder of dividing numer to denom

fn modulo_truncated #

@[inline]
fn modulo_truncated[T](numer T, denom T) T

modulo_truncated returns the truncated remainder of dividing numer to denom

fn nan #

fn nan() f64

nan returns an IEEE 754 ``not-a-number'' value.

fn next_power_of_two #

fn next_power_of_two(n u64) u64

next_power_of_two returns the smallest power of two >= n (1 for n == 0). Results above 2^63 saturate to 0, since 2^64 would not fit in u64.

fn nextafter #

fn nextafter(x f64, y f64) f64

nextafter returns the next representable f64 value after x towards y.

special cases are: nextafter(x, x) = x nextafter(nan, y) = nan nextafter(x, nan) = nan

fn nextafter32 #

fn nextafter32(x f32, y f32) f32

nextafter32 returns the next representable f32 value after x towards y.

special cases are: nextafter32(x, x) = x nextafter32(nan, y) = nan nextafter32(x, nan) = nan

fn normalize #

fn normalize(x f64) (f64, int)

normalize returns a normal number y and exponent exp satisfying x == y × 2**exp. It assumes x is finite and non-zero.

fn pow #

@[inline]
fn pow(x f64, y f64) f64

pow returns the base x, raised to the provided power y. (float64)

fn pow10 #

fn pow10(n int) f64

pow10 returns 10**n, the base-10 exponential of n.

special cases are: pow10(n) = 0 for n < -323 pow10(n) = +inf for n > 308

fn powf #

@[inline]
fn powf(a f32, b f32) f32

powf returns the base a, raised to the provided power b. (float32)

fn powi #

@[ignore_overflow]
fn powi(a i64, b i64) i64

powi returns base raised to power (a**b) as an integer (i64)

special case: powi(a, b) = -1 for a = 0 and b < 0

fn q_rsqrt #

@[inline]
fn q_rsqrt(x f64) f64

q_sqrt computes an approximation of the inverse square root (1 / √x) using a fast inverse square root algorithm. This method is often used in applications where performance is crucial, such as in computer graphics or physics simulations. (This algorithm is inspired by the famous "fast inverse square root" code used in the Quake III Arena game engine.)

fn radians #

@[inline]
fn radians(degrees f64) f64

radians converts an angle in degrees to a corresponding angle in radians.

fn remainder #

fn remainder(x f64, y f64) f64

remainder returns the IEEE 754 remainder x - n*y, with n the nearest integer to x/y, ties to even. Finite x and infinite y return x; zero y, infinite x, or NaN operands return NaN. Zero results keep the sign of x.

fn remap #

@[inline]
fn remap[T](x T, a T, b T, c T, d T) T

Note: a should be != b.

Example

assert math.remap(20, 1, 100, 50, 5000) == 1000

fn round #

fn round(x f64) f64

round returns the nearest integer, rounding half away from zero.

special cases are: round(±0) = ±0 round(±inf) = ±inf round(nan) = nan

fn round_sig #

fn round_sig(x f64, sig_digits int) f64

Returns the rounded float, with sig_digits of precision. i.e assert round_sig(4.3239437319748394,6) == 4.323944

fn round_to_even #

fn round_to_even(x f64) f64

round_to_even returns the nearest integer, rounding ties to even.

special cases are: round_to_even(±0) = ±0 round_to_even(±inf) = ±inf round_to_even(nan) = nan

fn saturating #

fn saturating[T](value T) Saturating[T]

saturating creates a Saturating[T] holding value.

fn scalbn #

fn scalbn(x f64, n_ int) f64

scalbn scales x by FLT_RADIX raised to the power of n, returning the same as: scalbn(x,n) = x * FLT_RADIX ** n

fn sign #

@[inline]
fn sign(n f64) f64

sign returns the corresponding sign -1.0, 1.0 of the provided number. if n is not a number, its sign is nan too.

fn signbit #

@[inline]
fn signbit(x f64) bool

signbit returns a value with the boolean representation of the sign for x

fn signi #

@[inline]
fn signi(n f64) int

signi returns the corresponding sign -1, 1 of the provided number.

fn sin #

@[inline]
fn sin(a f64) f64

sin calculates sine in radians (float64)

fn sincos #

fn sincos(x f64) (f64, f64)

sincos calculates the sine and cosine of the angle in radians

fn sinf #

@[inline]
fn sinf(a f32) f32

sinf calculates sine in radians (float32)

fn sinh #

fn sinh(x_ f64) f64

sinh calculates hyperbolic sine.

fn smootherstep #

@[inline]
fn smootherstep[T](edge0 T, edge1 T, x T) T

smootherstep smoothly maps a value between edge0 and edge1. smootherstep is a 2nd order smoothing function, using a 5th order polynomial. The 1st and 2nd order derivatives of the smootherstep function are 0 at both edges. See also smoothstep, which is faster, but has just its gradient being 0 at both edges. See also https://en.wikipedia.org/wiki/Smoothstep

fn smoothstep #

@[inline]
fn smoothstep[T](edge0 T, edge1 T, x T) T

smoothstep smoothly maps a value between edge0 and edge1. It returns: 0 if x is less than or equal to the left edge0, 1 if x is greater than or equal to the right edge, and smoothly interpolates, using a Hermite polynomial, between 0 and 1 otherwise. The gradient of the smoothstep function is zero at both edges. This is convenient for creating a sequence of transitions using smoothstep to interpolate each segment as an alternative to using more sophisticated or expensive interpolation techniques. smoothstep is a 1st order smoothing function, using a 3rd order polynomial. See also smootherstep, which is slower, but nicer looking. See also https://en.wikipedia.org/wiki/Smoothstep

fn sqrt #

@[inline]
fn sqrt(a f64) f64

sqrt calculates square-root of the provided value. (float64)

fn sqrtf #

@[inline]
fn sqrtf(a f32) f32

sqrtf calculates square-root of the provided value. (float32)

fn sqrti #

fn sqrti(a i64) i64

sqrti calculates the integer square-root of the provided value. (i64)

fn square #

@[inline]
fn square[T](x T) T

square returns the square of the argument x, i.e. x * x

fn tan #

fn tan(a f64) f64

tan calculates tangent of a number

fn tanf #

@[inline]
fn tanf(a f32) f32

tanf calculates tangent. (float32)

fn tanh #

fn tanh(x f64) f64

tanh returns the hyperbolic tangent of x.

special cases are: tanh(±0) = ±0 tanh(±inf) = ±1 tanh(nan) = nan

fn to_degrees #

fn to_degrees(radians f64) f64

to_degrees converts radians to degrees: x * 180 / pi.

fn to_radians #

fn to_radians(degrees f64) f64

to_radians converts degrees to radians: x * pi / 180.

fn tolerance #

fn tolerance(actual f64, expected f64, tol f64) bool

tolerance checks if the difference between actual and expected numbers, is less than or equal to the tolerance value tol.

Note: the actual and expected parameters are NOT symmetrical. If you compare against a known expected number, put it in the second argument, especially if it is 0.

fn trunc #

fn trunc(x f64) f64

trunc returns the integer value of x.

special cases are: trunc(±0) = ±0 trunc(±inf) = ±inf trunc(nan) = nan

fn veryclose #

fn veryclose(actual f64, expected f64) bool

veryclose checks if a and b are within 4e-16 of each other

Note: the actual and expected parameters are NOT symmetrical. If you compare against a known expected number, put it in the second argument, especially if it is 0.

fn with_set_high_word #

@[inline]
fn with_set_high_word(f f64, hi u32) f64

with_set_high_word sets high word of f to lo

fn with_set_low_word #

@[inline]
fn with_set_low_word(f f64, lo u32) f64

with_set_low_word sets low word of f to lo

fn wrapping #

fn wrapping[T](value T) Wrapping[T]

wrapping creates a Wrapping[T] holding value.

fn FloatClass.from #

fn FloatClass.from[W](input W) !FloatClass

from returns the FloatClass value of an integer or of a field name, or an error if there is none.

fn (Saturating[T]) str #

fn (s Saturating[T]) str() string

str returns the decimal representation of the saturated value.

fn (Saturating[T]) + #

fn (a Saturating[T]) + (b Saturating[T]) Saturating[T]
  • returns the sum clamped to the type's range.

fn (Saturating[T]) - #

fn (a Saturating[T]) - (b Saturating[T]) Saturating[T]
  • returns the difference clamped to the type's range.

fn (Saturating[T]) * #

fn (a Saturating[T]) * (b Saturating[T]) Saturating[T]
  • returns the product clamped to the type's range.

fn (Saturating[T]) / #

fn (a Saturating[T]) / (b Saturating[T]) Saturating[T]

/ returns the quotient, mapping min/-1 to max. Division by zero still panics, like plain division.

fn (Saturating[T]) % #

fn (a Saturating[T]) % (b Saturating[T]) Saturating[T]

% returns the remainder. Like plain remainder it panics on a zero divisor.

fn (Saturating[T]) neg #

fn (s Saturating[T]) neg() Saturating[T]

neg returns the negation clamped to the type's range (negating min yields max; negating an unsigned zero stays zero).

fn (Wrapping[T]) str #

fn (w Wrapping[T]) str() string

str returns the decimal representation of the wrapped value.

fn (Wrapping[T]) + #

fn (a Wrapping[T]) + (b Wrapping[T]) Wrapping[T]
  • returns the wrapped sum.

fn (Wrapping[T]) - #

fn (a Wrapping[T]) - (b Wrapping[T]) Wrapping[T]
  • returns the wrapped difference.

fn (Wrapping[T]) * #

fn (a Wrapping[T]) * (b Wrapping[T]) Wrapping[T]
  • returns the wrapped product.

fn (Wrapping[T]) / #

fn (a Wrapping[T]) / (b Wrapping[T]) Wrapping[T]

/ returns the wrapped quotient. Like plain division it panics on a zero divisor; min/-1 yields min (which would otherwise overflow).

fn (Wrapping[T]) % #

fn (a Wrapping[T]) % (b Wrapping[T]) Wrapping[T]

% returns the remainder. Like plain remainder it panics on a zero divisor.

fn (Wrapping[T]) neg #

fn (w Wrapping[T]) neg() Wrapping[T]

neg returns the wrapped negation.

enum FloatClass #

enum FloatClass {
	nan
	infinite
	normal
	subnormal
	zero
}

FloatClass classifies a floating-point value, as Go's math.Float64bits inspection or Rust's classify do.

struct BezierPoint #

struct BezierPoint {
pub mut:
	x f64
	y f64
}

BezierPoint represents point coordinates as floating point numbers. This type is used as the output of the cubic_bezier family of functions.

struct DigitParams #

@[params]
struct DigitParams {
pub:
	base    int = 10
	reverse bool
}

struct DivResult #

struct DivResult[T] {
pub mut:
	quot T
	rem  T
}

DivResult[T] represents the result of an integer division (both quotient and remainder) See also https://en.wikipedia.org/wiki/Modulo

struct Saturating #

struct Saturating[T] {
pub:
	value T
}

Saturating[T] is an integer wrapper with saturating arithmetic: results that would overflow clamp to the type's minimum or maximum instead of wrapping or panicking, in every build mode. It is the V-idiomatic equivalent of Rust's std::num::Saturating. Supported for the 8 fixed-width integer types (u8..u64, i8..i64).

struct Wrapping #

struct Wrapping[T] {
pub:
	value T
}

Wrapping[T] is an integer wrapper with wraparound (modular) arithmetic: results that would overflow wrap around instead of panicking or depending on backend-specific behavior, in every build mode. It is the V-idiomatic equivalent of Rust's std::num::Wrapping. Supported for the 8 fixed-width integer types (u8..u64, i8..i64).