module Math

Extended Modules

Defined in:

big/big_float.cr
big/big_int.cr
big/big_rational.cr
complex.cr
math/math.cr

Constant Summary

E = LibM.exp_f64(1.0)

Euler's number (e).

LOG10 = LibM.log_f64(10.0)
LOG2 = LibM.log_f64(2.0)
PI = 3.14159265358979323846

Archimedes' constant (π).

TAU = 6.283185307179586476925

The full circle constant (τ), equal to 2π.

Instance Method Summary

Instance Method Detail

def acos(value : Float32) : Float32Source

Calculates the arc cosine of value.

def acos(value : Float64) : Float64Source

Calculates the arc cosine of value.

def acos(value)Source

Calculates the arc cosine of value.

def acosh(value : Float32) : Float32Source

Calculates the inverse hyperbolic cosine of value.

def acosh(value : Float64) : Float64Source

Calculates the inverse hyperbolic cosine of value.

def acosh(value)Source

Calculates the inverse hyperbolic cosine of value.

def asin(value : Float32) : Float32Source

Calculates the arc sine of value.

def asin(value : Float64) : Float64Source

Calculates the arc sine of value.

def asin(value)Source

Calculates the arc sine of value.

def asinh(value : Float32) : Float32Source

Calculates the inverse hyperbolic sine of value.

def asinh(value : Float64) : Float64Source

Calculates the inverse hyperbolic sine of value.

def asinh(value)Source

Calculates the inverse hyperbolic sine of value.

def atan(value : Float32) : Float32Source

Calculates the arc tangent of value.

def atan(value : Float64) : Float64Source

Calculates the arc tangent of value.

def atan(value)Source

Calculates the arc tangent of value.

def atan2(y : Float32, x : Float32) : Float32Source

Calculates the two-argument arc tangent of the ray from (0, 0) to (x, y).

def atan2(y : Float64, x : Float64) : Float64Source

Calculates the two-argument arc tangent of the ray from (0, 0) to (x, y).

def atan2(y, x) : Float64Source

Calculates the two-argument arc tangent of the ray from (0, 0) to (x, y).

def atanh(value : Float32) : Float32Source

Calculates the inverse hyperbolic tangent of value.

def atanh(value : Float64) : Float64Source

Calculates the inverse hyperbolic tangent of value.

def atanh(value)Source

Calculates the inverse hyperbolic tangent of value.

def besselj(order : Int32, value : Float32)Source

Calculates the cylindrical Bessel function of the first kind of value for the given order.

def besselj(order : Int32, value : Float64) : Float64Source

Calculates the cylindrical Bessel function of the first kind of value for the given order.

def besselj(order, value)Source

Calculates the cylindrical Bessel function of the first kind of value for the given order.

def besselj0(value : Float32)Source

Calculates the cylindrical Bessel function of the first kind of value for order 0.

def besselj0(value : Float64) : Float64Source

Calculates the cylindrical Bessel function of the first kind of value for order 0.

def besselj0(value)Source

Calculates the cylindrical Bessel function of the first kind of value for order 0.

def besselj1(value : Float32)Source

Calculates the cylindrical Bessel function of the first kind of value for order 1.

def besselj1(value : Float64) : Float64Source

Calculates the cylindrical Bessel function of the first kind of value for order 1.

def besselj1(value)Source

Calculates the cylindrical Bessel function of the first kind of value for order 1.

def bessely(order : Int32, value : Float32)Source

Calculates the cylindrical Bessel function of the second kind of value for the given order.

def bessely(order : Int32, value : Float64) : Float64Source

Calculates the cylindrical Bessel function of the second kind of value for the given order.

def bessely(order, value)Source

Calculates the cylindrical Bessel function of the second kind of value for the given order.

def bessely0(value : Float32)Source

Calculates the cylindrical Bessel function of the second kind of value for order 0.

def bessely0(value : Float64) : Float64Source

Calculates the cylindrical Bessel function of the second kind of value for order 0.

def bessely0(value)Source

Calculates the cylindrical Bessel function of the second kind of value for order 0.

def bessely1(value : Float32)Source

Calculates the cylindrical Bessel function of the second kind of value for order 1.

def bessely1(value : Float64) : Float64Source

Calculates the cylindrical Bessel function of the second kind of value for order 1.

def bessely1(value)Source

Calculates the cylindrical Bessel function of the second kind of value for order 1.

def cbrt(value : Float32) : Float32Source

Calculates the cubic root of value.

def cbrt(value : Float64) : Float64Source

Calculates the cubic root of value.

def cbrt(value)Source

Calculates the cubic root of value.

def copysign(value1 : Float32, value2 : Float32)Source

Returns the floating-point value with the magnitude of value1 and the sign of value2.

def copysign(value1 : Float64, value2 : Float64) : Float64Source

Returns the floating-point value with the magnitude of value1 and the sign of value2.

def copysign(value1, value2)Source

Returns the floating-point value with the magnitude of value1 and the sign of value2.

def cos(value : Float32) : Float32Source

Calculates the cosine of value, measured in radians.

def cos(value : Float64) : Float64Source

Calculates the cosine of value, measured in radians.

def cos(value)Source

Calculates the cosine of value, measured in radians.

def cosh(value : Float32) : Float32Source

Calculates the hyperbolic cosine of value.

def cosh(value : Float64) : Float64Source

Calculates the hyperbolic cosine of value.

def cosh(value)Source

Calculates the hyperbolic cosine of value.

def erf(value : Float32) : Float32Source

Calculates the error function of value.

def erf(value : Float64) : Float64Source

Calculates the error function of value.

def erf(value)Source

Calculates the error function of value.

def erfc(value : Float32) : Float32Source

Calculates 1 minus the error function of value.

def erfc(value : Float64) : Float64Source

Calculates 1 minus the error function of value.

def erfc(value)Source

Calculates 1 minus the error function of value.

def exp(value : Float32) : Float32Source

Calculates the exponential of value.

def exp(value : Float64) : Float64Source

Calculates the exponential of value.

def exp(value : Complex) : ComplexSource

Calculates the exponential of value.

require "complex"

Math.exp(4 + 2.i) # => -22.720847417619233 + 49.645957334580565i

def exp(value)Source

Calculates the exponential of value.

def exp2(value : Float32) : Float32Source

Calculates 2 raised to the power value.

def exp2(value : Float64) : Float64Source

Calculates 2 raised to the power value.

def exp2(value)Source

Calculates 2 raised to the power value.

def expm1(value : Float32) : Float32Source

Calculates the exponential of value, minus 1.

def expm1(value : Float64) : Float64Source

Calculates the exponential of value, minus 1.

def expm1(value)Source

Calculates the exponential of value, minus 1.

def frexp(value : Float32) : Tuple(Float32, Int32)Source

Decomposes the given floating-point value into a normalized fraction and an integral power of two.

def frexp(value : Float64) : Tuple(Float64, Int32)Source

Decomposes the given floating-point value into a normalized fraction and an integral power of two.

def frexp(value : BigFloat) : Tuple(BigFloat, Int64)Source

Decomposes the given floating-point value into a normalized fraction and an integral power of two.

def frexp(value)Source

Decomposes the given floating-point value into a normalized fraction and an integral power of two.

def gamma(value : Float32) : Float32Source

Calculates the gamma function of value.

Note that #gamma(n) is same as fact(n - 1) for integer n > 0. However #gamma(n) returns float and can be an approximation.

def gamma(value : Float64) : Float64Source

Calculates the gamma function of value.

Note that #gamma(n) is same as fact(n - 1) for integer n > 0. However #gamma(n) returns float and can be an approximation.

def gamma(value) : Float64Source

Calculates the gamma function of value.

Note that #gamma(n) is same as fact(n - 1) for integer n > 0. However #gamma(n) returns float and can be an approximation.

def hypot(value1 : Float32, value2 : Float32) : Float32Source

Calculates the length of the hypotenuse from (0, 0) to (value1, value2).

Equivalent to

Math.sqrt(value1 ** 2 + value2 ** 2)

def hypot(value1 : Float64, value2 : Float64) : Float64Source

Calculates the length of the hypotenuse from (0, 0) to (value1, value2).

Equivalent to

Math.sqrt(value1 ** 2 + value2 ** 2)

def hypot(value1, value2)Source

Calculates the length of the hypotenuse from (0, 0) to (value1, value2).

Equivalent to

Math.sqrt(value1 ** 2 + value2 ** 2)

def ilogb(value : Float32) : Int32Source

Returns the unbiased base 2 exponent of the given floating-point value.

def ilogb(value : Float64) : Int32Source

Returns the unbiased base 2 exponent of the given floating-point value.

def ilogb(value)Source

Returns the unbiased base 2 exponent of the given floating-point value.

def isqrt(value : Int::Primitive)Source

Calculates the integer square root of value.

def isqrt(value : BigInt)Source

Calculates the integer square root of value.

def ldexp(value : Float32, exp : Int32) : Float32Source

Multiplies the given floating-point value by 2 raised to the power exp.

def ldexp(value : Float64, exp : Int32) : Float64Source

Multiplies the given floating-point value by 2 raised to the power exp.

def ldexp(value, exp)Source

Multiplies the given floating-point value by 2 raised to the power exp.

def lgamma(value : Float32)Source

Calculates the logarithmic gamma of value.

Math.lgamma(2.96)

is equivalent to

Math.log(Math.gamma(2.96).abs)

def lgamma(value : Float64) : Float64Source

Calculates the logarithmic gamma of value.

Math.lgamma(2.96)

is equivalent to

Math.log(Math.gamma(2.96).abs)

def lgamma(value) : Float64Source

Calculates the logarithmic gamma of value.

Math.lgamma(2.96)

is equivalent to

Math.log(Math.gamma(2.96).abs)

def log(value, base)Source

Calculates the logarithm of value to the given base.

def log(value : Float32) : Float32Source

Calculates the natural logarithm of value.

def log(value : Float64) : Float64Source

Calculates the natural logarithm of value.

def log(value : Complex) : ComplexSource

Calculates the natural logarithm of value.

require "complex"

Math.log(4 + 2.i) # => 1.4978661367769956 + 0.4636476090008061i

def log(value) : Float64Source

Calculates the natural logarithm of value.

def log10(value : Float32) : Float32Source

Calculates the logarithm of value to base 10.

def log10(value : Float64) : Float64Source

Calculates the logarithm of value to base 10.

def log10(value : Complex) : ComplexSource

Calculates the logarithm of value to base 10.

require "complex"

Math.log10(4 + 2.i) # => 0.6505149978319906 + 0.20135959813668655i

def log10(value)Source

Calculates the logarithm of value to base 10.

def log1p(value : Float32) : Float32Source

Calculates the natural logarithm of 1 plus value.

def log1p(value : Float64) : Float64Source

Calculates the natural logarithm of 1 plus value.

def log1p(value)Source

Calculates the natural logarithm of 1 plus value.

def log2(value : Float32) : Float32Source

Calculates the logarithm of value to base 2.

def log2(value : Float64) : Float64Source

Calculates the logarithm of value to base 2.

def log2(value : Complex) : ComplexSource

Calculates the logarithm of value to base 2.

require "complex"

Math.log2(4 + 2.i) # => 2.1609640474436813 + 0.6689021062254881i

def log2(value) : Float64Source

Calculates the logarithm of value to base 2.

def logb(value : Float32) : Float32Source

Returns the unbiased radix-independent exponent of the given floating-point value.

For Float32 and Float64 this is equivalent to #ilogb.

def logb(value : Float64) : Float64Source

Returns the unbiased radix-independent exponent of the given floating-point value.

For Float32 and Float64 this is equivalent to #ilogb.

def logb(value)Source

Returns the unbiased radix-independent exponent of the given floating-point value.

For Float32 and Float64 this is equivalent to #ilogb.

def max(value1 : Float32, value2 : Float32)Source

Returns the greater of value1 and value2.

def max(value1 : Float64, value2 : Float64) : Float64Source

Returns the greater of value1 and value2.

def max(value1, value2)Source

Returns the greater of value1 and value2.

def min(value1 : Float32, value2 : Float32)Source

Returns the smaller of value1 and value2.

def min(value1 : Float64, value2 : Float64) : Float64Source

Returns the smaller of value1 and value2.

def min(value1, value2)Source

Returns the smaller of value1 and value2.

def pw2ceil(v : Int32)Source

Computes the next highest power of 2 of v.

Math.pw2ceil(33) # => 64

def pw2ceil(v : Int64)Source

def scalbln(value : Float32, exp : Int64)Source

Returns the floating-point value with its exponent raised by exp.

For Float32 and Float64 this is equivalent to #ldexp.

def scalbln(value : Float64, exp : Int64) : Float64Source

Returns the floating-point value with its exponent raised by exp.

For Float32 and Float64 this is equivalent to #ldexp.

def scalbln(value, exp) : Float64Source

Returns the floating-point value with its exponent raised by exp.

For Float32 and Float64 this is equivalent to #ldexp.

def scalbn(value : Float32, exp : Int32) : Float32Source

Returns the floating-point value with its exponent raised by exp.

For Float32 and Float64 this is equivalent to #ldexp.

def scalbn(value : Float64, exp : Int32) : Float64Source

Returns the floating-point value with its exponent raised by exp.

For Float32 and Float64 this is equivalent to #ldexp.

def scalbn(value, exp)Source

Returns the floating-point value with its exponent raised by exp.

For Float32 and Float64 this is equivalent to #ldexp.

def sin(value : Float32) : Float32Source

Calculates the sine of value, measured in radians.

def sin(value : Float64) : Float64Source

Calculates the sine of value, measured in radians.

def sin(value)Source

Calculates the sine of value, measured in radians.

def sinh(value : Float32) : Float32Source

Calculates the hyperbolic sine of value.

def sinh(value : Float64) : Float64Source

Calculates the hyperbolic sine of value.

def sinh(value)Source

Calculates the hyperbolic sine of value.

def sqrt(value : Float32) : Float32Source

Calculates the square root of value.

def sqrt(value : Float64) : Float64Source

Calculates the square root of value.

def sqrt(value : BigInt) : BigFloatSource

Calculates the square root of value.

require "big"

Math.sqrt(1_000_000_000_000.to_big_i * 1_000_000_000_000.to_big_i) # => 1000000000000.0

def sqrt(value : BigFloat) : BigFloatSource

Calculates the square root of value.

require "big"

Math.sqrt(1_000_000_000_000.to_big_f * 1_000_000_000_000.to_big_f) # => 1000000000000.0

def sqrt(value : BigRational) : BigFloatSource

Calculates the square root of value.

require "big"

Math.sqrt(1_000_000_000_000.to_big_r * 1_000_000_000_000.to_big_r) # => 1000000000000.0

def sqrt(value : Complex) : ComplexSource

Calculates the square root of value. Inspired by the following blog post of Pavel Panchekha on floating point precision.

require "complex"

Math.sqrt(4 + 2.i) # => 2.0581710272714924 + 0.48586827175664565i

Although the imaginary number is defined as i = sqrt(-1), calling Math.sqrt with a negative number will return -NaN. To obtain the result in the complex plane, Math.sqrt must be called with a complex number.

Math.sqrt(-1.0)         # => -NaN
Math.sqrt(-1.0 + 0.0.i) # => 0.0 + 1.0i

def sqrt(value) : Float64Source

Calculates the square root of value.

def tan(value : Float32) : Float32Source

Calculates the tangent of value, measured in radians.

def tan(value : Float64) : Float64Source

Calculates the tangent of value, measured in radians.

def tan(value)Source

Calculates the tangent of value, measured in radians.

def tanh(value : Float32) : Float32Source

Calculates the hyperbolic tangent of value.

def tanh(value : Float64) : Float64Source

Calculates the hyperbolic tangent of value.

def tanh(value)Source

Calculates the hyperbolic tangent of value.

© 2012–2021 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/1.2.1/Math.html