module Math
Extended Modules
Defined in:
big/big_float.crbig/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
-
#acos(value : Float32) : Float32
Calculates the arc cosine of value.
-
#acos(value : Float64) : Float64
Calculates the arc cosine of value.
-
#acos(value)
Calculates the arc cosine of value.
-
#acosh(value : Float32) : Float32
Calculates the inverse hyperbolic cosine of value.
-
#acosh(value : Float64) : Float64
Calculates the inverse hyperbolic cosine of value.
-
#acosh(value)
Calculates the inverse hyperbolic cosine of value.
-
#asin(value : Float32) : Float32
Calculates the arc sine of value.
-
#asin(value : Float64) : Float64
Calculates the arc sine of value.
-
#asin(value)
Calculates the arc sine of value.
-
#asinh(value : Float32) : Float32
Calculates the inverse hyperbolic sine of value.
-
#asinh(value : Float64) : Float64
Calculates the inverse hyperbolic sine of value.
-
#asinh(value)
Calculates the inverse hyperbolic sine of value.
-
#atan(value : Float32) : Float32
Calculates the arc tangent of value.
-
#atan(value : Float64) : Float64
Calculates the arc tangent of value.
-
#atan(value)
Calculates the arc tangent of value.
-
#atan2(y : Float32, x : Float32) : Float32
Calculates the two-argument arc tangent of the ray from (0, 0) to (x, y).
-
#atan2(y : Float64, x : Float64) : Float64
Calculates the two-argument arc tangent of the ray from (0, 0) to (x, y).
-
#atan2(y, x) : Float64
Calculates the two-argument arc tangent of the ray from (0, 0) to (x, y).
-
#atanh(value : Float32) : Float32
Calculates the inverse hyperbolic tangent of value.
-
#atanh(value : Float64) : Float64
Calculates the inverse hyperbolic tangent of value.
-
#atanh(value)
Calculates the inverse hyperbolic tangent of value.
-
#besselj(order : Int32, value : Float32)
Calculates the cylindrical Bessel function of the first kind of value for the given order.
-
#besselj(order : Int32, value : Float64) : Float64
Calculates the cylindrical Bessel function of the first kind of value for the given order.
-
#besselj(order, value)
Calculates the cylindrical Bessel function of the first kind of value for the given order.
-
#besselj0(value : Float32)
Calculates the cylindrical Bessel function of the first kind of value for order 0.
-
#besselj0(value : Float64) : Float64
Calculates the cylindrical Bessel function of the first kind of value for order 0.
-
#besselj0(value)
Calculates the cylindrical Bessel function of the first kind of value for order 0.
-
#besselj1(value : Float32)
Calculates the cylindrical Bessel function of the first kind of value for order 1.
-
#besselj1(value : Float64) : Float64
Calculates the cylindrical Bessel function of the first kind of value for order 1.
-
#besselj1(value)
Calculates the cylindrical Bessel function of the first kind of value for order 1.
-
#bessely(order : Int32, value : Float32)
Calculates the cylindrical Bessel function of the second kind of value for the given order.
-
#bessely(order : Int32, value : Float64) : Float64
Calculates the cylindrical Bessel function of the second kind of value for the given order.
-
#bessely(order, value)
Calculates the cylindrical Bessel function of the second kind of value for the given order.
-
#bessely0(value : Float32)
Calculates the cylindrical Bessel function of the second kind of value for order 0.
-
#bessely0(value : Float64) : Float64
Calculates the cylindrical Bessel function of the second kind of value for order 0.
-
#bessely0(value)
Calculates the cylindrical Bessel function of the second kind of value for order 0.
-
#bessely1(value : Float32)
Calculates the cylindrical Bessel function of the second kind of value for order 1.
-
#bessely1(value : Float64) : Float64
Calculates the cylindrical Bessel function of the second kind of value for order 1.
-
#bessely1(value)
Calculates the cylindrical Bessel function of the second kind of value for order 1.
-
#cbrt(value : Float32) : Float32
Calculates the cubic root of value.
-
#cbrt(value : Float64) : Float64
Calculates the cubic root of value.
-
#cbrt(value)
Calculates the cubic root of value.
-
#copysign(value1 : Float32, value2 : Float32)
Returns the floating-point value with the magnitude of value1 and the sign of value2.
-
#copysign(value1 : Float64, value2 : Float64) : Float64
Returns the floating-point value with the magnitude of value1 and the sign of value2.
-
#copysign(value1 : BigFloat, value2 : BigFloat) : BigFloat
Returns the floating-point value with the magnitude of value1 and the sign of value2.
-
#copysign(value1, value2)
Returns the floating-point value with the magnitude of value1 and the sign of value2.
-
#cos(value : Float32) : Float32
Calculates the cosine of value, measured in radians.
-
#cos(value : Float64) : Float64
Calculates the cosine of value, measured in radians.
-
#cos(value)
Calculates the cosine of value, measured in radians.
-
#cosh(value : Float32) : Float32
Calculates the hyperbolic cosine of value.
-
#cosh(value : Float64) : Float64
Calculates the hyperbolic cosine of value.
-
#cosh(value)
Calculates the hyperbolic cosine of value.
-
#erf(value : Float32) : Float32
Calculates the error function of value.
-
#erf(value : Float64) : Float64
Calculates the error function of value.
-
#erf(value)
Calculates the error function of value.
-
#erfc(value : Float32) : Float32
Calculates 1 minus the error function of value.
-
#erfc(value : Float64) : Float64
Calculates 1 minus the error function of value.
-
#erfc(value)
Calculates 1 minus the error function of value.
-
#exp(value : Float32) : Float32
Calculates the exponential of value.
-
#exp(value : Float64) : Float64
Calculates the exponential of value.
-
#exp(value : Complex) : Complex
Calculates the exponential of value.
-
#exp(value)
Calculates the exponential of value.
-
#exp2(value : Float32) : Float32
Calculates 2 raised to the power value.
-
#exp2(value : Float64) : Float64
Calculates 2 raised to the power value.
-
#exp2(value)
Calculates 2 raised to the power value.
-
#expm1(value : Float32) : Float32
Calculates the exponential of value, minus 1.
-
#expm1(value : Float64) : Float64
Calculates the exponential of value, minus 1.
-
#expm1(value)
Calculates the exponential of value, minus 1.
-
#fma(value1 : Float32, value2 : Float32, value3 : Float32) : Float32
Fused multiply-add; returns
value1 * value2 + value3
, performing a single rounding instead of two. -
#fma(value1 : Float64, value2 : Float64, value3 : Float64) : Float64
Fused multiply-add; returns
value1 * value2 + value3
, performing a single rounding instead of two. -
#fma(value1, value2, value3)
Fused multiply-add; returns
value1 * value2 + value3
, performing a single rounding instead of two. -
#frexp(value : Float32) : Tuple(Float32, Int32)
Decomposes the given floating-point value into a normalized fraction and an integral power of two.
-
#frexp(value : Float64) : Tuple(Float64, Int32)
Decomposes the given floating-point value into a normalized fraction and an integral power of two.
-
#frexp(value : BigFloat) : Tuple(BigFloat, Int64)
Decomposes the given floating-point value into a normalized fraction and an integral power of two.
-
#frexp(value)
Decomposes the given floating-point value into a normalized fraction and an integral power of two.
-
#gamma(value : Float32) : Float32
Calculates the gamma function of value.
-
#gamma(value : Float64) : Float64
Calculates the gamma function of value.
-
#gamma(value) : Float64
Calculates the gamma function of value.
-
#hypot(value1 : Float32, value2 : Float32) : Float32
Calculates the length of the hypotenuse from (0, 0) to (value1, value2).
-
#hypot(value1 : Float64, value2 : Float64) : Float64
Calculates the length of the hypotenuse from (0, 0) to (value1, value2).
-
#hypot(value1, value2)
Calculates the length of the hypotenuse from (0, 0) to (value1, value2).
-
#ilogb(value : Float32) : Int32
Returns the unbiased base 2 exponent of the given floating-point value.
-
#ilogb(value : Float64) : Int32
Returns the unbiased base 2 exponent of the given floating-point value.
-
#ilogb(value : BigFloat) : Int64
Returns the unbiased base 2 exponent of the given floating-point value.
-
#ilogb(value)
Returns the unbiased base 2 exponent of the given floating-point value.
-
#isqrt(value : Int::Primitive)
Calculates the integer square root of value.
-
#isqrt(value : BigInt)
Calculates the integer square root of value.
-
#ldexp(value : Float32, exp : Int32) : Float32
Multiplies the given floating-point value by 2 raised to the power exp.
-
#ldexp(value : Float64, exp : Int32) : Float64
Multiplies the given floating-point value by 2 raised to the power exp.
-
#ldexp(value : BigFloat, exp : Int) : BigFloat
Multiplies the given floating-point value by 2 raised to the power exp.
-
#ldexp(value, exp)
Multiplies the given floating-point value by 2 raised to the power exp.
-
#lgamma(value : Float32)
Calculates the logarithmic gamma of value.
-
#lgamma(value : Float64) : Float64
Calculates the logarithmic gamma of value.
-
#lgamma(value) : Float64
Calculates the logarithmic gamma of value.
-
#log(value, base)
Calculates the logarithm of value to the given base.
-
#log(value : Float32) : Float32
Calculates the natural logarithm of value.
-
#log(value : Float64) : Float64
Calculates the natural logarithm of value.
-
#log(value : Complex) : Complex
Calculates the natural logarithm of value.
-
#log(value) : Float64
Calculates the natural logarithm of value.
-
#log10(value : Float32) : Float32
Calculates the logarithm of value to base 10.
-
#log10(value : Float64) : Float64
Calculates the logarithm of value to base 10.
-
#log10(value : Complex) : Complex
Calculates the logarithm of value to base 10.
-
#log10(value)
Calculates the logarithm of value to base 10.
-
#log1p(value : Float32) : Float32
Calculates the natural logarithm of 1 plus value.
-
#log1p(value : Float64) : Float64
Calculates the natural logarithm of 1 plus value.
-
#log1p(value)
Calculates the natural logarithm of 1 plus value.
-
#log2(value : Float32) : Float32
Calculates the logarithm of value to base 2.
-
#log2(value : Float64) : Float64
Calculates the logarithm of value to base 2.
-
#log2(value : Complex) : Complex
Calculates the logarithm of value to base 2.
-
#log2(value) : Float64
Calculates the logarithm of value to base 2.
-
#logb(value : Float32) : Float32
Returns the unbiased radix-independent exponent of the given floating-point value.
-
#logb(value : Float64) : Float64
Returns the unbiased radix-independent exponent of the given floating-point value.
-
#logb(value : BigFloat) : BigFloat
Returns the unbiased radix-independent exponent of the given floating-point value.
-
#logb(value)
Returns the unbiased radix-independent exponent of the given floating-point value.
-
#max(value1 : Float32, value2 : Float32)
Returns the greater of value1 and value2.
-
#max(value1 : Float64, value2 : Float64) : Float64
Returns the greater of value1 and value2.
-
#max(value1, value2)
Returns the greater of value1 and value2.
-
#min(value1 : Float32, value2 : Float32)
Returns the smaller of value1 and value2.
-
#min(value1 : Float64, value2 : Float64) : Float64
Returns the smaller of value1 and value2.
-
#min(value1, value2)
Returns the smaller of value1 and value2.
-
#pw2ceil(v : Int::Primitive)
Computes the smallest nonnegative power of 2 that is greater than or equal to v.
-
#pw2ceil(v : BigInt) : BigInt
Computes the smallest nonnegative power of 2 that is greater than or equal to v.
-
#scalbln(value : Float32, exp : Int64)
Returns the floating-point value with its exponent raised by exp.
-
#scalbln(value : Float64, exp : Int64) : Float64
Returns the floating-point value with its exponent raised by exp.
-
#scalbln(value : BigFloat, exp : Int) : BigFloat
Returns the floating-point value with its exponent raised by exp.
-
#scalbln(value, exp) : Float64
Returns the floating-point value with its exponent raised by exp.
-
#scalbn(value : Float32, exp : Int32) : Float32
Returns the floating-point value with its exponent raised by exp.
-
#scalbn(value : Float64, exp : Int32) : Float64
Returns the floating-point value with its exponent raised by exp.
-
#scalbn(value : BigFloat, exp : Int) : BigFloat
Returns the floating-point value with its exponent raised by exp.
-
#scalbn(value, exp)
Returns the floating-point value with its exponent raised by exp.
-
#sin(value : Float32) : Float32
Calculates the sine of value, measured in radians.
-
#sin(value : Float64) : Float64
Calculates the sine of value, measured in radians.
-
#sin(value)
Calculates the sine of value, measured in radians.
-
#sinh(value : Float32) : Float32
Calculates the hyperbolic sine of value.
-
#sinh(value : Float64) : Float64
Calculates the hyperbolic sine of value.
-
#sinh(value)
Calculates the hyperbolic sine of value.
-
#sqrt(value : Float32) : Float32
Calculates the square root of value.
-
#sqrt(value : Float64) : Float64
Calculates the square root of value.
-
#sqrt(value : BigInt) : BigFloat
Calculates the square root of value.
-
#sqrt(value : BigFloat) : BigFloat
Calculates the square root of value.
-
#sqrt(value : BigRational) : BigFloat
Calculates the square root of value.
-
#sqrt(value : Complex) : Complex
Calculates the square root of value.
-
#sqrt(value) : Float64
Calculates the square root of value.
-
#tan(value : Float32) : Float32
Calculates the tangent of value, measured in radians.
-
#tan(value : Float64) : Float64
Calculates the tangent of value, measured in radians.
-
#tan(value)
Calculates the tangent of value, measured in radians.
-
#tanh(value : Float32) : Float32
Calculates the hyperbolic tangent of value.
-
#tanh(value : Float64) : Float64
Calculates the hyperbolic tangent of value.
-
#tanh(value)
Calculates the hyperbolic tangent of value.
Instance Method Detail
Calculates the inverse hyperbolic cosine of value.
Calculates the inverse hyperbolic cosine of value.
Calculates the inverse hyperbolic sine of value.
Calculates the inverse hyperbolic sine of value.
Calculates the two-argument arc tangent of the ray from (0, 0) to (x, y).
Calculates the two-argument arc tangent of the ray from (0, 0) to (x, y).
Calculates the two-argument arc tangent of the ray from (0, 0) to (x, y).
Calculates the inverse hyperbolic tangent of value.
Calculates the inverse hyperbolic tangent of value.
Calculates the cylindrical Bessel function of the first kind of value for the given order.
Calculates the cylindrical Bessel function of the first kind of value for the given order.
Calculates the cylindrical Bessel function of the first kind of value for the given order.
Calculates the cylindrical Bessel function of the first kind of value for order 0.
Calculates the cylindrical Bessel function of the first kind of value for order 0.
Calculates the cylindrical Bessel function of the first kind of value for order 0.
Calculates the cylindrical Bessel function of the first kind of value for order 1.
Calculates the cylindrical Bessel function of the first kind of value for order 1.
Calculates the cylindrical Bessel function of the first kind of value for order 1.
Calculates the cylindrical Bessel function of the second kind of value for the given order.
Calculates the cylindrical Bessel function of the second kind of value for the given order.
Calculates the cylindrical Bessel function of the second kind of value for the given order.
Calculates the cylindrical Bessel function of the second kind of value for order 0.
Calculates the cylindrical Bessel function of the second kind of value for order 0.
Calculates the cylindrical Bessel function of the second kind of value for order 0.
Calculates the cylindrical Bessel function of the second kind of value for order 1.
Calculates the cylindrical Bessel function of the second kind of value for order 1.
Calculates the cylindrical Bessel function of the second kind of value for order 1.
Returns the floating-point value with the magnitude of value1 and the sign of value2.
Returns the floating-point value with the magnitude of value1 and the sign of value2.
Returns the floating-point value with the magnitude of value1 and the sign of value2.
BigFloat
does not support signed zeros; if value2 == 0
, the returned value is non-negative.
Returns the floating-point value with the magnitude of value1 and the sign of value2.
Calculates the cosine of value, measured in radians.
Calculates the cosine of value, measured in radians.
Calculates the exponential of value.
require "complex"
Math.exp(4 + 2.i) # => -22.720847417619233 + 49.645957334580565.i
Fused multiply-add; returns value1 * value2 + value3
, performing a single
rounding instead of two.
Math.fma(0.1, 10.0, -1.0) # => 5.551115123125783e-17
1.0 * 10.0 - 1.0 # => 0.0
Fused multiply-add; returns value1 * value2 + value3
, performing a single
rounding instead of two.
Math.fma(0.1, 10.0, -1.0) # => 5.551115123125783e-17
1.0 * 10.0 - 1.0 # => 0.0
Fused multiply-add; returns value1 * value2 + value3
, performing a single
rounding instead of two.
Math.fma(0.1, 10.0, -1.0) # => 5.551115123125783e-17
1.0 * 10.0 - 1.0 # => 0.0
Decomposes the given floating-point value into a normalized fraction and an integral power of two.
Decomposes the given floating-point value into a normalized fraction and an integral power of two.
Decomposes the given floating-point value into a normalized fraction and an integral power of two.
Decomposes the given floating-point value into a normalized fraction and an integral power of two.
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.
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.
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.
Calculates the length of the hypotenuse from (0, 0) to (value1, value2).
Equivalent to
Math.sqrt(value1 ** 2 + value2 ** 2)
Calculates the length of the hypotenuse from (0, 0) to (value1, value2).
Equivalent to
Math.sqrt(value1 ** 2 + value2 ** 2)
Calculates the length of the hypotenuse from (0, 0) to (value1, value2).
Equivalent to
Math.sqrt(value1 ** 2 + value2 ** 2)
Returns the unbiased base 2 exponent of the given floating-point value.
Returns the unbiased base 2 exponent of the given floating-point value.
Returns the unbiased base 2 exponent of the given floating-point value.
Raises ArgumentError
if value is zero.
Multiplies the given floating-point value by 2 raised to the power exp.
Multiplies the given floating-point value by 2 raised to the power exp.
Multiplies the given floating-point value by 2 raised to the power exp.
Calculates the logarithmic gamma of value.
Math.lgamma(2.96)
is equivalent to
Math.log(Math.gamma(2.96).abs)
Calculates the logarithmic gamma of value.
Math.lgamma(2.96)
is equivalent to
Math.log(Math.gamma(2.96).abs)
Calculates the logarithmic gamma of value.
Math.lgamma(2.96)
is equivalent to
Math.log(Math.gamma(2.96).abs)
Calculates the natural logarithm of value.
require "complex"
Math.log(4 + 2.i) # => 1.4978661367769956 + 0.4636476090008061.i
Calculates the logarithm of value to base 10.
require "complex"
Math.log10(4 + 2.i) # => 0.6505149978319906 + 0.20135959813668655.i
Calculates the natural logarithm of 1 plus value.
Calculates the natural logarithm of 1 plus value.
Calculates the logarithm of value to base 2.
require "complex"
Math.log2(4 + 2.i) # => 2.1609640474436813 + 0.6689021062254881.i
Returns the unbiased radix-independent exponent of the given floating-point value.
Returns the unbiased radix-independent exponent of the given floating-point value.
Returns the unbiased radix-independent exponent of the given floating-point value.
For BigFloat
this is equivalent to #ilogb
.
Raises ArgumentError
is value is zero.
Returns the unbiased radix-independent exponent of the given floating-point value.
Returns the greater of value1 and value2.
Returns the greater of value1 and value2.
Returns the smaller of value1 and value2.
Returns the smaller of value1 and value2.
Computes the smallest nonnegative power of 2 that is greater than or equal to v.
The returned value has the same type as the argument. Raises OverflowError
if the result does not fit into the argument's type.
Math.pw2ceil(33) # => 64
Math.pw2ceil(64) # => 64
Math.pw2ceil(-5) # => 1
Computes the smallest nonnegative power of 2 that is greater than or equal to v.
The returned value has the same type as the argument.
Math.pw2ceil(33) # => 64
Math.pw2ceil(64) # => 64
Math.pw2ceil(-5) # => 1
Returns the floating-point value with its exponent raised by exp.
Returns the floating-point value with its exponent raised by exp.
Returns the floating-point value with its exponent raised by exp.
Returns the floating-point value with its exponent raised by exp.
Returns the floating-point value with its exponent raised by exp.
Returns the floating-point value with its exponent raised by exp.
Returns the floating-point value with its exponent raised by exp.
Calculates the sine of value, measured in radians.
Calculates the sine of value, measured in radians.
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
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
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
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.48586827175664565.i
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.0.i
Calculates the tangent of value, measured in radians.
Calculates the tangent of value, measured in radians.