math #
Description:
math
provides commonly used mathematical functions for
trigonometry, logarithms, etc.
Constants #
const (
epsilon = 2.2204460492503130808472633361816E-16
e = 2.71828182845904523536028747135266249775724709369995957496696763
pi = 3.14159265358979323846264338327950288419716939937510582097494459
pi_2 = pi / 2.0
pi_4 = pi / 4.0
phi = 1.61803398874989484820458683436563811772030917980576286213544862
tau = 6.28318530717958647692528676655900576839433879875021164194988918
one_over_tau = 1.0 / tau
one_over_pi = 1.0 / pi
tau_over2 = tau / 2.0
tau_over4 = tau / 4.0
tau_over8 = tau / 8.0
sqrt2 = 1.41421356237309504880168872420969807856967187537694807317667974
sqrt_3 = 1.73205080756887729352744634150587236694280525381038062805580697
sqrt_5 = 2.23606797749978969640917366873127623544061835961152572427089724
sqrt_e = 1.64872127070012814684865078781416357165377610071014801157507931
sqrt_pi = 1.77245385090551602729816748334114518279754945612238712821380779
sqrt_tau = 2.50662827463100050241576528481104525300698674060993831662992357
sqrt_phi = 1.27201964951406896425242246173749149171560804184009624861664038
ln2 = 0.693147180559945309417232121458176568075500134360255254120680009
log2_e = 1.0 / ln2
ln10 = 2.30258509299404568401799145468436420760110148862877297603332790
log10_e = 1.0 / ln10
two_thirds = 0.66666666666666666666666666666666666666666666666666666666666667
)
const (
max_f32 = 3.40282346638528859811704183484516925440e+38 // 2**127 * (2**24 - 1) / 2**23
smallest_non_zero_f32 = 1.401298464324817070923729583289916131280e-45 // 1 / 2**(127 - 1 + 23)
max_f64 = 1.797693134862315708145274237317043567981e+308 // 2**1023 * (2**53 - 1) / 2**52
smallest_non_zero_f64 = 4.940656458412465441765687928682213723651e-324 // 1 / 2**(1023 - 1 + 52)
)
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 (
min_i8 = i8(-128)
max_i8 = i8(127)
min_i16 = i16(-32768)
max_i16 = i16(32767)
min_i32 = int(-2147483648)
max_i32 = int(2147483647)
// -9223372036854775808 is wrong, because C compilers parse literal values
// without sign first, and 9223372036854775808 overflows i64, hence the
// consecutive subtraction by 1
min_i64 = i64(-9223372036854775807 - 1)
max_i64 = i64(9223372036854775807)
min_u8 = u8(0)
max_u8 = u8(255)
min_u16 = u16(0)
max_u16 = u16(65535)
min_u32 = u32(0)
max_u32 = u32(4294967295)
min_u64 = u64(0)
max_u64 = u64(18446744073709551615)
)
Integer limit values
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
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 sin(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(x f64) f64
cos calculates the cosine of the angle in radians
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 degrees #
fn degrees(radians f64) f64
degrees converts from radians to 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 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.
special cases are: exp(+inf) = +inf exp(nan) = nan
Very large values overflow to 0 or +inf.
Very small values underflow to 1.
fn exp2 #
fn exp2(x f64) f64
exp2 returns 2**x, the base-2 exponential of x.
special cases are the same as exp.
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 fabs #
fn fabs(x f64) f64
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 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
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 logarithm of x
Method : 1. Argument Reduction: find k and f such that x = 2^k * (1+f), where sqrt(2)/2 < 1+f < sqrt(2) .
Approximation of log(1+f).
Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) = 2s + 2/3 s3 + 2/5 s5 + ....., = 2s + sR We use a special Remez algorithm on [0,0.1716] to generate a polynomial of degree 14 to approximate R The maximum error of this polynomial approximation is bounded by 2**-58.45. In other words, 2 4 6 8 10 12 14 R(z) ~ Lg1s +Lg2s +Lg3s +Lg4s +Lg5s +Lg6s +Lg7s (the values of Lg1 to Lg7 are listed in the program) and | 2 14 | -58.45 | Lg1s +...+Lg7s - R(z) | <= 2 | | Note that 2s = f - sf = f - hfsq + shfsq, where hfsq = ff/2.
In order to guarantee error in log below 1ulp, we compute log by log(1+f) = f - s(f - R) (if f is not too large) log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy)- Finally, log(x) = kln2 + log(1+f).
= kln2_hi+(f-(hfsq-(s*(hfsq+R)+kln2_lo))) Here ln2 is split into two floating point number: ln2_hi + ln2_lo, where nln2_hi is always exact for |n| < 2000.
- Finally, log(x) = kln2 + log(1+f).
Special cases: log(x) is NaN with signal if x < 0 (including -inf) ; log(+inf) is +inf; log(0) is -inf with signal; log(NaN) is that NaN with no signal.
Accuracy: according to an error analysis, the error is always less than 1 ulp (unit in the last place).
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)
fn log_n #
fn log_n(x f64, b f64) f64
log_n returns log base b of x
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 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 base raised to the provided power.
todo(playXE): make this function work on JS backend, probably problem of JS codegen that it does not work.
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 base raised to the provided power. (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 from degrees to 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.0, 1.0 of the provided number.
fn sin #
fn sin(x f64) f64
sin calculates the sine of the angle in radians
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
special cases are: sqrt(+inf) = +inf sqrt(±0) = ±0 sqrt(x < 0) = nan sqrt(nan) = nan
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 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 {
base int = 10
reverse bool
}
- 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 degrees
- fn digits
- 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 fabs
- fn factorial
- fn factoriali
- fn floor
- 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 max
- fn maxof
- fn min
- fn minmax
- fn minof
- fn mod
- fn modf
- 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 tan
- fn tanf
- fn tanh
- fn tolerance
- fn trunc
- fn veryclose
- fn with_set_high_word
- fn with_set_low_word
- struct DigitParams