math #
Description
math
provides commonly used mathematical functions for trigonometry, logarithms, etc.
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
const min_i8 = i8(-128)
const max_i8 = i8(127)
const min_i16 = i16(-32768)
const max_i16 = i16(32767)
const min_i32 = i32(-2147483648)
const max_i32 = i32(2147483647)
const min_i64 = i64(-9223372036854775807 - 1)
-9223372036854775808 is wrong, because C compilers parse literal values without sign first, and 9223372036854775808 overflows i64, hence the consecutive subtraction by 1
const max_i64 = i64(9223372036854775807)
const min_u8 = u8(0)
const max_u8 = u8(255)
const min_u16 = u16(0)
const max_u16 = u16(65535)
const min_u32 = u32(0)
const max_u32 = u32(4294967295)
const min_u64 = u64(0)
const max_u64 = u64(18446744073709551615)
fn abs #
fn abs[T](a T) T
abs returns the absolute value of a
fn acos #
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 #
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 #
fn clamp(x f64, a f64, b f64) f64
clamp returns x constrained between a and b
fn close #
fn close(a f64, b f64) bool
close checks if a and b are within 1e-14 of each other
fn copysign #
fn copysign(x f64, y f64) f64
copysign returns a value with the magnitude of x and the sign of y
fn cos #
fn cos(a f64) f64
cos calculates cosine in radians (float64)
fn cosf #
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
tan 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 #
fn cube[T](x T) T
cube returns the cube of the argument x, i.e. x * x * x
fn degrees #
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 #
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 #
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 #
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 #
fn exp(x f64) f64
exp returns e**x, the base-e exponential of x.
fn exp2 #
fn exp2(x f64) f64
exp2 returns 2**x, the base-2 exponential of x.
fn expm1 #
fn expm1(x f64) f64
expm1 calculates e**x - 1 special cases are: expm1(+inf) = +inf expm1(-inf) = -1 expm1(nan) = nan
fn f32_bits #
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 #
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 #
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 #
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.
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 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 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(a_ i64, b_ i64) i64
gcd calculates greatest common (positive) divisor (or zero if a and b are both zero).
fn get_high_word #
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
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 lcm #
fn lcm(a i64, b i64) i64
lcm calculates least common (non-negative) multiple.
fn ldexp #
fn ldexp(frac f64, exp int) f64
ldexp calculates frac*(2**exp).
fn log #
fn log(a f64) f64
log returns the natural logarithm of x (float64)
fn log10 #
fn log10(x f64) f64
log10 returns the decimal logarithm of x. The special cases are the same as for log.
fn log1p #
fn log1p(x f64) f64
log1p returns log(1+x)
fn log2 #
fn log2(x f64) f64
log2 returns the binary logarithm of x. The special cases are the same as for log.
fn log_b #
fn log_b(x f64) f64
log_b returns the binary exponent of x.
special cases are: log_b(±inf) = +inf log_b(0) = -inf log_b(nan) = nan
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 and sign (-1 or +1) 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 #
fn logf(a f32) f32
log returns the natural logarithm of x (float32)
fn max #
fn max[T](a T, b T) T
max returns the maximum of a
and b
fn maxof #
fn maxof[T]() T
maxof returns the maximum value of the type T
fn min #
fn min[T](a T, b T) T
min returns the minimum of a
and b
fn minmax #
fn minmax(a f64, b f64) (f64, f64)
minmax returns the minimum and maximum value of the two provided.
fn minof #
fn minof[T]() T
minof returns the minimum value of the type T
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 #
fn modulo_euclid[T](numer T, denom T) T
modulo_euclid returns the Euclidean remainder of dividing numer to denom
fn modulo_floored #
fn modulo_floored[T](numer T, denom T) T
modulo_floored returns the floored remainder of dividing numer to denom
fn modulo_truncated #
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 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 #
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 #
fn powf(a f32, b f32) f32
powf returns the base a, raised to the provided power b. (float32)
fn powi #
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 #
fn q_rsqrt(x f64) f64
fn radians #
fn radians(degrees f64) f64
radians converts an angle in degrees to a corresponding angle in radians.
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 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 #
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 #
fn signbit(x f64) bool
signbit returns a value with the boolean representation of the sign for x
fn signi #
fn signi(n f64) int
signi returns the corresponding sign -1, 1 of the provided number.
fn sin #
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 #
fn sinf(a f32) f32
sinf calculates sine in radians (float32)
fn sinh #
fn sinh(x_ f64) f64
sinh calculates hyperbolic sine.
fn sqrt #
fn sqrt(a f64) f64
sqrt calculates square-root of the provided value. (float64)
fn sqrtf #
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 #
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 #
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 tolerance #
fn tolerance(a f64, b f64, tol f64) bool
tolerance checks if a and b difference are less than or equal to the tolerance value
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(a f64, b f64) bool
veryclose checks if a and b are within 4e-16 of each other
fn with_set_high_word #
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 #
fn with_set_low_word(f f64, lo u32) f64
with_set_low_word sets low word of f
to lo
struct DigitParams #
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
- README
- Constants
- fn abs
- fn acos
- fn acosh
- fn alike
- fn angle_diff
- fn aprox_cos
- fn aprox_sin
- fn asin
- fn asinh
- fn atan
- fn atan2
- fn atanh
- fn cbrt
- fn ceil
- fn clamp
- fn close
- fn copysign
- fn cos
- fn cosf
- fn cosh
- fn cot
- fn count_digits
- fn cube
- fn degrees
- fn digits
- fn divide_euclid
- fn divide_floored
- fn divide_truncated
- fn egcd
- fn erf
- fn erfc
- fn exp
- fn exp2
- fn expm1
- fn f32_bits
- fn f32_from_bits
- fn f64_bits
- fn f64_from_bits
- fn factorial
- fn factoriali
- fn floor
- fn floorf
- fn fmod
- fn frexp
- fn gamma
- fn gcd
- fn get_high_word
- fn hypot
- fn ilog_b
- fn inf
- fn is_finite
- fn is_inf
- fn is_nan
- fn lcm
- fn ldexp
- fn log
- fn log10
- fn log1p
- fn log2
- fn log_b
- fn log_factorial
- fn log_gamma
- fn log_gamma_sign
- fn log_n
- fn logf
- fn max
- fn maxof
- fn min
- fn minmax
- fn minof
- fn mod
- fn modf
- fn modulo_euclid
- fn modulo_floored
- fn modulo_truncated
- fn nan
- fn nextafter
- fn nextafter32
- fn normalize
- fn pow
- fn pow10
- fn powf
- fn powi
- fn q_rsqrt
- fn radians
- fn round
- fn round_sig
- fn round_to_even
- fn scalbn
- fn sign
- fn signbit
- fn signi
- fn sin
- fn sincos
- fn sinf
- fn sinh
- fn sqrt
- fn sqrtf
- fn sqrti
- fn square
- fn tan
- fn tanf
- fn tanh
- fn tolerance
- fn trunc
- fn veryclose
- fn with_set_high_word
- fn with_set_low_word
- struct DigitParams
- struct DivResult