Module: Math
- Defined in:
- math.c
Defined Under Namespace
Classes: DomainError
Constant Summary collapse
- PI =
Definition of the mathematical constant PI as a Float number.
DBL2NUM(M_PI)
- E =
Definition of the mathematical constant E for Euler’s number (e) as a Float number.
DBL2NUM(exp(1.0))
Class Method Summary collapse
-
.acos(x) ⇒ Float
Returns the arc cosine of
x
. -
.acosh(x) ⇒ Float
Returns the inverse hyperbolic cosine of
x
. -
.asin(x) ⇒ Float
Returns the arc sine of
x
. -
.asinh(x) ⇒ Float
Returns the inverse hyperbolic sine of
x
. -
.atan(x) ⇒ Float
Returns the arc tangent of
x
. -
.atan2(y, x) ⇒ Float
Returns the arc tangent of
y
andx
in radians. -
.atanh(x) ⇒ Float
Returns the inverse hyperbolic tangent of
x
. -
.cbrt(x) ⇒ Float
Returns the cube root of
x
. - .cos(x) ⇒ Float
-
.cosh(x) ⇒ Float
Returns the hyperbolic cosine of
x
in radians. -
.erf(x) ⇒ Float
Returns the value of the Gauss error function for
x
. -
.erfc(x) ⇒ Float
Returns the value of the complementary error function for
x
. -
.exp(x) ⇒ Float
Returns
e
raised to thex
power. -
.frexp(x) ⇒ Array
Returns a 2-element array containing the normalized signed float
fraction
and integerexponent
ofx
such that:. -
.gamma(x) ⇒ Float
Returns the value of the gamma function for
x
. -
.hypot(a, b) ⇒ Float
Returns
sqrt(a**2 + b**2)
, which is the length of the longest sidec
(the hypotenuse) of the right triangle whose other sides have lengthsa
andb
. -
.ldexp(fraction, exponent) ⇒ Float
Returns the value of
fraction * 2**exponent
. -
.lgamma(x) ⇒ Array, ...
Returns a 2-element array equivalent to:.
-
.log(x, base = Math::E) ⇒ Float
Returns the base
base
logarithm ofx
. -
.log10(x) ⇒ Float
Returns the base 10 logarithm of
x
. -
.log2(x) ⇒ Float
Returns the base 2 logarithm of
x
. - .sin(x) ⇒ Float
-
.sinh(x) ⇒ Float
Returns the hyperbolic sine of
x
in radians. -
.sqrt(x) ⇒ Float
Returns the principal (non-negative) square root of
x
. - .tan(x) ⇒ Float
-
.tanh(x) ⇒ Float
Returns the hyperbolic tangent of
x
in radians.
Instance Method Summary collapse
-
#acos(x) ⇒ Float
private
Returns the arc cosine of
x
. -
#acosh(x) ⇒ Float
private
Returns the inverse hyperbolic cosine of
x
. -
#asin(x) ⇒ Float
private
Returns the arc sine of
x
. -
#asinh(x) ⇒ Float
private
Returns the inverse hyperbolic sine of
x
. -
#atan(x) ⇒ Float
private
Returns the arc tangent of
x
. -
#atan2(y, x) ⇒ Float
private
Returns the arc tangent of
y
andx
in radians. -
#atanh(x) ⇒ Float
private
Returns the inverse hyperbolic tangent of
x
. -
#cbrt(x) ⇒ Float
private
Returns the cube root of
x
. - #cos(x) ⇒ Float private
-
#cosh(x) ⇒ Float
private
Returns the hyperbolic cosine of
x
in radians. -
#erf(x) ⇒ Float
private
Returns the value of the Gauss error function for
x
. -
#erfc(x) ⇒ Float
private
Returns the value of the complementary error function for
x
. -
#exp(x) ⇒ Float
private
Returns
e
raised to thex
power. -
#frexp(x) ⇒ Array
private
Returns a 2-element array containing the normalized signed float
fraction
and integerexponent
ofx
such that:. -
#gamma(x) ⇒ Float
private
Returns the value of the gamma function for
x
. -
#hypot(a, b) ⇒ Float
private
Returns
sqrt(a**2 + b**2)
, which is the length of the longest sidec
(the hypotenuse) of the right triangle whose other sides have lengthsa
andb
. -
#ldexp(fraction, exponent) ⇒ Float
private
Returns the value of
fraction * 2**exponent
. -
#lgamma(x) ⇒ Array, ...
private
Returns a 2-element array equivalent to:.
-
#log(x, base = Math::E) ⇒ Float
private
Returns the base
base
logarithm ofx
. -
#log10(x) ⇒ Float
private
Returns the base 10 logarithm of
x
. -
#log2(x) ⇒ Float
private
Returns the base 2 logarithm of
x
. - #sin(x) ⇒ Float private
-
#sinh(x) ⇒ Float
private
Returns the hyperbolic sine of
x
in radians. -
#sqrt(x) ⇒ Float
private
Returns the principal (non-negative) square root of
x
. - #tan(x) ⇒ Float private
-
#tanh(x) ⇒ Float
private
Returns the hyperbolic tangent of
x
in radians.
Class Method Details
.acos(x) ⇒ Float
Returns the arc cosine of x
.
-
Domain:
[-1, 1]
. -
Range:
[0, PI]
.
Examples:
acos(-1.0) # => 3.141592653589793 # PI
acos(0.0) # => 1.5707963267948966 # PI/2
acos(1.0) # => 0.0
196 197 198 199 200 |
# File 'math.c', line 196
static VALUE
math_acos(VALUE unused_obj, VALUE x)
{
math_arc(x, acos)
}
|
.acosh(x) ⇒ Float
Returns the inverse hyperbolic cosine of x
.
-
Domain:
[1, INFINITY]
. -
Range:
[0, INFINITY]
.
Examples:
acosh(1.0) # => 0.0
acosh(INFINITY) # => Infinity
371 372 373 374 375 376 377 378 379 |
# File 'math.c', line 371
static VALUE
math_acosh(VALUE unused_obj, VALUE x)
{
double d;
d = Get_Double(x);
domain_check_min(d, 1.0, "acosh");
return DBL2NUM(acosh(d));
}
|
.asin(x) ⇒ Float
Returns the arc sine of x
.
-
Domain:
[-1, -1]
. -
Range:
[-PI/2, PI/2]
.
Examples:
asin(-1.0) # => -1.5707963267948966 # -PI/2
asin(0.0) # => 0.0
asin(1.0) # => 1.5707963267948966 # PI/2
219 220 221 222 223 |
# File 'math.c', line 219
static VALUE
math_asin(VALUE unused_obj, VALUE x)
{
math_arc(x, asin)
}
|
.asinh(x) ⇒ Float
Returns the inverse hyperbolic sine of x
.
-
Domain:
[-INFINITY, INFINITY]
. -
Range:
[-INFINITY, INFINITY]
.
Examples:
asinh(-INFINITY) # => -Infinity
asinh(0.0) # => 0.0
asinh(INFINITY) # => Infinity
398 399 400 401 402 |
# File 'math.c', line 398
static VALUE
math_asinh(VALUE unused_obj, VALUE x)
{
return DBL2NUM(asinh(Get_Double(x)));
}
|
.atan(x) ⇒ Float
Returns the arc tangent of x
.
-
Domain:
[-INFINITY, INFINITY]
. -
Range:
[-PI/2, PI/2]
.
Examples:
atan(-INFINITY) # => -1.5707963267948966 # -PI2
atan(-PI) # => -1.2626272556789115
atan(-PI/2) # => -1.0038848218538872
atan(0.0) # => 0.0
atan(PI/2) # => 1.0038848218538872
atan(PI) # => 1.2626272556789115
atan(INFINITY) # => 1.5707963267948966 # PI/2
246 247 248 249 250 |
# File 'math.c', line 246
static VALUE
math_atan(VALUE unused_obj, VALUE x)
{
return DBL2NUM(atan(Get_Double(x)));
}
|
.atan2(y, x) ⇒ Float
Returns the arc tangent of y
and x
in radians.
-
Domain of
y
:[-INFINITY, INFINITY]
. -
Domain of
x
:[-INFINITY, INFINITY]
. -
Range:
[-PI, PI]
.
Examples:
atan2(-1.0, -1.0) # => -2.356194490192345 # -3*PI/4
atan2(-1.0, 0.0) # => -1.5707963267948966 # -PI/2
atan2(-1.0, 1.0) # => -0.7853981633974483 # -PI/4
atan2(0.0, -1.0) # => 3.141592653589793 # PI
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'math.c', line 61
static VALUE
math_atan2(VALUE unused_obj, VALUE y, VALUE x)
{
double dx, dy;
dx = Get_Double(x);
dy = Get_Double(y);
if (dx == 0.0 && dy == 0.0) {
if (!signbit(dx))
return DBL2NUM(dy);
if (!signbit(dy))
return DBL2NUM(M_PI);
return DBL2NUM(-M_PI);
}
#ifndef ATAN2_INF_C99
if (isinf(dx) && isinf(dy)) {
/* optimization for FLONUM */
if (dx < 0.0) {
const double dz = (3.0 * M_PI / 4.0);
return (dy < 0.0) ? DBL2NUM(-dz) : DBL2NUM(dz);
}
else {
const double dz = (M_PI / 4.0);
return (dy < 0.0) ? DBL2NUM(-dz) : DBL2NUM(dz);
}
}
#endif
return DBL2NUM(atan2(dy, dx));
}
|
.atanh(x) ⇒ Float
Returns the inverse hyperbolic tangent of x
.
-
Domain:
[-1, 1]
. -
Range:
[-INFINITY, INFINITY]
.
Examples:
atanh(-1.0) # => -Infinity
atanh(0.0) # => 0.0
atanh(1.0) # => Infinity
421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'math.c', line 421
static VALUE
math_atanh(VALUE unused_obj, VALUE x)
{
double d;
d = Get_Double(x);
domain_check_range(d, -1.0, +1.0, "atanh");
/* check for pole error */
if (d == -1.0) return DBL2NUM(-HUGE_VAL);
if (d == +1.0) return DBL2NUM(+HUGE_VAL);
return DBL2NUM(atanh(d));
}
|
.cbrt(x) ⇒ Float
Returns the cube root of x
.
-
Domain:
[-INFINITY, INFINITY]
. -
Range:
[-INFINITY, INFINITY]
.
Examples:
cbrt(-INFINITY) # => -Infinity
cbrt(-27.0) # => -3.0
cbrt(-8.0) # => -2.0
cbrt(-2.0) # => -1.2599210498948732
cbrt(1.0) # => 1.0
cbrt(0.0) # => 0.0
cbrt(1.0) # => 1.0
cbrt(2.0) # => 1.2599210498948732
cbrt(8.0) # => 2.0
cbrt(27.0) # => 3.0
cbrt(INFINITY) # => Infinity
741 742 743 744 745 746 747 748 749 750 751 752 |
# File 'math.c', line 741
static VALUE
math_cbrt(VALUE unused_obj, VALUE x)
{
double f = Get_Double(x);
double r = cbrt(f);
#if defined __GLIBC__
if (isfinite(r) && !(f == 0.0 && r == 0.0)) {
r = (2.0 * r + (f / r / r)) / 3.0;
}
#endif
return DBL2NUM(r);
}
|
.cos(x) ⇒ Float
112 113 114 115 116 |
# File 'math.c', line 112
static VALUE
math_cos(VALUE unused_obj, VALUE x)
{
return DBL2NUM(cos(Get_Double(x)));
}
|
.cosh(x) ⇒ Float
Returns the hyperbolic cosine of x
in radians.
-
Domain:
[-INFINITY, INFINITY]
. -
Range:
[1, INFINITY]
.
Examples:
cosh(-INFINITY) # => Infinity
cosh(0.0) # => 1.0
cosh(INFINITY) # => Infinity
278 279 280 281 282 |
# File 'math.c', line 278
static VALUE
math_cosh(VALUE unused_obj, VALUE x)
{
return DBL2NUM(cosh(Get_Double(x)));
}
|
.erf(x) ⇒ Float
Returns the value of the Gauss error function for x
.
- Domain: <tt>[-INFINITY, INFINITY]</tt>.
- Range: <tt>[-1, 1]</tt>.
Examples:
erf(-INFINITY) # => -1.0
erf(0.0) # => 0.0
erf(INFINITY) # => 1.0
Related: Math.erfc.
874 875 876 877 878 |
# File 'math.c', line 874
static VALUE
math_erf(VALUE unused_obj, VALUE x)
{
return DBL2NUM(erf(Get_Double(x)));
}
|
.erfc(x) ⇒ Float
Returns the value of the complementary error function for x
.
- Domain: <tt>[-INFINITY, INFINITY]</tt>.
- Range: <tt>[0, 2]</tt>.
Examples:
erfc(-INFINITY) # => 2.0
erfc(0.0) # => 1.0
erfc(INFINITY) # => 0.0
Related: Math.erf.
899 900 901 902 903 |
# File 'math.c', line 899
static VALUE
math_erfc(VALUE unused_obj, VALUE x)
{
return DBL2NUM(erfc(Get_Double(x)));
}
|
.exp(x) ⇒ Float
Returns e
raised to the x
power.
-
Domain:
[-INFINITY, INFINITY]
. -
Range:
[0, INFINITY]
.
Examples:
exp(-INFINITY) # => 0.0
exp(-1.0) # => 0.36787944117144233 # 1.0/E
exp(0.0) # => 1.0
exp(0.5) # => 1.6487212707001282 # sqrt(E)
exp(1.0) # => 2.718281828459045 # E
exp(2.0) # => 7.38905609893065 # E**2
exp(INFINITY) # => Infinity
455 456 457 458 459 |
# File 'math.c', line 455
static VALUE
math_exp(VALUE unused_obj, VALUE x)
{
return DBL2NUM(exp(Get_Double(x)));
}
|
.frexp(x) ⇒ Array
Returns a 2-element array containing the normalized signed float fraction
and integer exponent
of x
such that:
x = fraction * 2**exponent
See IEEE 754 double-precision binary floating-point format: binary64.
-
Domain:
[-INFINITY, INFINITY]
. -
Range
[-INFINITY, INFINITY]
.
Examples:
frexp(-INFINITY) # => [-Infinity, -1]
frexp(-2.0) # => [-0.5, 2]
frexp(-1.0) # => [-0.5, 1]
frexp(0.0) # => [0.0, 0]
frexp(1.0) # => [0.5, 1]
frexp(2.0) # => [0.5, 2]
frexp(INFINITY) # => [Infinity, -1]
Related: Math.ldexp (inverse of Math.frexp).
782 783 784 785 786 787 788 789 790 |
# File 'math.c', line 782
static VALUE
math_frexp(VALUE unused_obj, VALUE x)
{
double d;
int exp;
d = frexp(Get_Double(x), &exp);
return rb_assoc_new(DBL2NUM(d), INT2NUM(exp));
}
|
.gamma(x) ⇒ Float
Returns the value of the gamma function for x
.
- Domain: <tt>(-INFINITY, INFINITY]</tt> excluding negative integers.
- Range: <tt>[-INFINITY, INFINITY]</tt>.
Examples:
gamma(-2.5) # => -0.9453087204829431
gamma(-1.5) # => 2.3632718012073513
gamma(-0.5) # => -3.5449077018110375
gamma(0.0) # => Infinity
gamma(1.0) # => 1.0
gamma(2.0) # => 1.0
gamma(3.0) # => 2.0
gamma(4.0) # => 6.0
gamma(5.0) # => 24.0
Related: Math.lgamma.
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 |
# File 'math.c', line 930
static VALUE
math_gamma(VALUE unused_obj, VALUE x)
{
static const double fact_table[] = {
/* fact(0) */ 1.0,
/* fact(1) */ 1.0,
/* fact(2) */ 2.0,
/* fact(3) */ 6.0,
/* fact(4) */ 24.0,
/* fact(5) */ 120.0,
/* fact(6) */ 720.0,
/* fact(7) */ 5040.0,
/* fact(8) */ 40320.0,
/* fact(9) */ 362880.0,
/* fact(10) */ 3628800.0,
/* fact(11) */ 39916800.0,
/* fact(12) */ 479001600.0,
/* fact(13) */ 6227020800.0,
/* fact(14) */ 87178291200.0,
/* fact(15) */ 1307674368000.0,
/* fact(16) */ 20922789888000.0,
/* fact(17) */ 355687428096000.0,
/* fact(18) */ 6402373705728000.0,
/* fact(19) */ 121645100408832000.0,
/* fact(20) */ 2432902008176640000.0,
/* fact(21) */ 51090942171709440000.0,
/* fact(22) */ 1124000727777607680000.0,
/* fact(23)=25852016738884976640000 needs 56bit mantissa which is
* impossible to represent exactly in IEEE 754 double which have
* 53bit mantissa. */
};
enum {NFACT_TABLE = numberof(fact_table)};
double d;
d = Get_Double(x);
/* check for domain error */
if (isinf(d)) {
if (signbit(d)) domain_error("gamma");
return DBL2NUM(HUGE_VAL);
}
if (d == 0.0) {
return signbit(d) ? DBL2NUM(-HUGE_VAL) : DBL2NUM(HUGE_VAL);
}
if (d == floor(d)) {
domain_check_min(d, 0.0, "gamma");
if (1.0 <= d && d <= (double)NFACT_TABLE) {
return DBL2NUM(fact_table[(int)d - 1]);
}
}
return DBL2NUM(tgamma(d));
}
|
.hypot(a, b) ⇒ Float
Returns sqrt(a**2 + b**2)
, which is the length of the longest side c
(the hypotenuse) of the right triangle whose other sides have lengths a
and b
.
-
Domain of
a
:[-INFINITY, INFINITY]
. -
Domain of +ab:
[-INFINITY, INFINITY]
. -
Range:
[0, INFINITY]
.
Examples:
hypot(0.0, 1.0) # => 1.0
hypot(1.0, 1.0) # => 1.4142135623730951 # sqrt(2.0)
hypot(3.0, 4.0) # => 5.0
hypot(5.0, 12.0) # => 13.0
hypot(1.0, sqrt(3.0)) # => 1.9999999999999998 # Near 2.0
Note that if either argument is INFINITY
or -INFINITY
, the result is Infinity
.
849 850 851 852 853 |
# File 'math.c', line 849
static VALUE
math_hypot(VALUE unused_obj, VALUE x, VALUE y)
{
return DBL2NUM(hypot(Get_Double(x), Get_Double(y)));
}
|
.ldexp(fraction, exponent) ⇒ Float
Returns the value of fraction * 2**exponent
.
-
Domain of
fraction
:[0.0, 1.0)
. -
Domain of
exponent
:[0, 1024]
(larger values are equivalent to 1024).
See IEEE 754 double-precision binary floating-point format: binary64.
Examples:
ldexp(-INFINITY, -1) # => -Infinity
ldexp(-0.5, 2) # => -2.0
ldexp(-0.5, 1) # => -1.0
ldexp(0.0, 0) # => 0.0
ldexp(-0.5, 1) # => 1.0
ldexp(-0.5, 2) # => 2.0
ldexp(INFINITY, -1) # => Infinity
Related: Math.frexp (inverse of Math.ldexp).
818 819 820 821 822 |
# File 'math.c', line 818
static VALUE
math_ldexp(VALUE unused_obj, VALUE x, VALUE n)
{
return DBL2NUM(ldexp(Get_Double(x), NUM2INT(n)));
}
|
.lgamma(x) ⇒ Array, ...
Returns a 2-element array equivalent to:
[Math.log(Math.gamma(x).abs), Math.gamma(x) < 0 ? -1 : 1]
See {logarithmic gamma function}[https://en.wikipedia.org/wiki/Gamma_function#The_log-gamma_function].
- Domain: <tt>(-INFINITY, INFINITY]</tt>.
- Range of first element: <tt>(-INFINITY, INFINITY]</tt>.
- Second element is -1 or 1.
Examples:
lgamma(-4.0) # => [Infinity, -1]
lgamma(-3.0) # => [Infinity, -1]
lgamma(-2.0) # => [Infinity, -1]
lgamma(-1.0) # => [Infinity, -1]
lgamma(0.0) # => [Infinity, 1]
lgamma(1.0) # => [0.0, 1]
lgamma(2.0) # => [0.0, 1]
lgamma(3.0) # => [0.6931471805599436, 1]
lgamma(4.0) # => [1.7917594692280545, 1]
lgamma(-2.5) # => [-0.05624371649767279, -1]
lgamma(-1.5) # => [0.8600470153764797, 1]
lgamma(-0.5) # => [1.265512123484647, -1]
lgamma(0.5) # => [0.5723649429247004, 1]
lgamma(1.5) # => [-0.12078223763524676, 1]
lgamma(2.5) # => [0.2846828704729205, 1]
Related: Math.gamma.
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 |
# File 'math.c', line 1019
static VALUE
math_lgamma(VALUE unused_obj, VALUE x)
{
double d;
int sign=1;
VALUE v;
d = Get_Double(x);
/* check for domain error */
if (isinf(d)) {
if (signbit(d)) domain_error("lgamma");
return rb_assoc_new(DBL2NUM(HUGE_VAL), INT2FIX(1));
}
if (d == 0.0) {
VALUE vsign = signbit(d) ? INT2FIX(-1) : INT2FIX(+1);
return rb_assoc_new(DBL2NUM(HUGE_VAL), vsign);
}
v = DBL2NUM(lgamma_r(d, &sign));
return rb_assoc_new(v, INT2FIX(sign));
}
|
.log(x, base = Math::E) ⇒ Float
Returns the base base
logarithm of x
.
-
Domain:
[0, INFINITY]
. -
Range:
[-INFINITY, INFINITY)]
.
Examples:
log(0.0) # => -Infinity
log(1.0) # => 0.0
log(E) # => 1.0
log(INFINITY) # => Infinity
log(0.0, 2.0) # => -Infinity
log(1.0, 2.0) # => 0.0
log(2.0, 2.0) # => 1.0
log(0.0, 10.0) # => -Infinity
log(1.0, 10.0) # => 0.0
log(10.0, 10.0) # => 1.0
505 506 507 508 509 |
# File 'math.c', line 505
static VALUE
math_log(int argc, const VALUE *argv, VALUE unused_obj)
{
return rb_math_log(argc, argv);
}
|
.log10(x) ⇒ Float
Returns the base 10 logarithm of x
.
-
Domain:
[0, INFINITY]
. -
Range:
[-INFINITY, INFINITY]
.
Examples:
log10(0.0) # => -Infinity
log10(1.0) # => 0.0
log10(10.0) # => 1.0
log10(INFINITY) # => Infinity
637 638 639 640 641 642 643 644 645 646 647 648 |
# File 'math.c', line 637
static VALUE
math_log10(VALUE unused_obj, VALUE x)
{
size_t numbits;
double d = get_double_rshift(x, &numbits);
domain_check_min(d, 0.0, "log10");
/* check for pole error */
if (d == 0.0) return DBL2NUM(-HUGE_VAL);
return DBL2NUM(log10(d) + numbits * log10(2)); /* log10(d * 2 ** numbits) */
}
|
.log2(x) ⇒ Float
Returns the base 2 logarithm of x
.
-
Domain:
[0, INFINITY]
. -
Range:
[-INFINITY, INFINITY]
.
Examples:
log2(0.0) # => -Infinity
log2(1.0) # => 0.0
log2(2.0) # => 1.0
log2(INFINITY) # => Infinity
606 607 608 609 610 611 612 613 614 615 616 617 |
# File 'math.c', line 606
static VALUE
math_log2(VALUE unused_obj, VALUE x)
{
size_t numbits;
double d = get_double_rshift(x, &numbits);
domain_check_min(d, 0.0, "log2");
/* check for pole error */
if (d == 0.0) return DBL2NUM(-HUGE_VAL);
return DBL2NUM(log2(d) + numbits); /* log2(d * 2 ** numbits) */
}
|
.sin(x) ⇒ Float
139 140 141 142 143 |
# File 'math.c', line 139
static VALUE
math_sin(VALUE unused_obj, VALUE x)
{
return DBL2NUM(sin(Get_Double(x)));
}
|
.sinh(x) ⇒ Float
Returns the hyperbolic sine of x
in radians.
-
Domain:
[-INFINITY, INFINITY]
. -
Range:
[-INFINITY, INFINITY]
.
Examples:
sinh(-INFINITY) # => -Infinity
sinh(0.0) # => 0.0
sinh(INFINITY) # => Infinity
310 311 312 313 314 |
# File 'math.c', line 310
static VALUE
math_sinh(VALUE unused_obj, VALUE x)
{
return DBL2NUM(sinh(Get_Double(x)));
}
|
.sqrt(x) ⇒ Float
Returns the principal (non-negative) square root of x
.
-
Domain:
[0, INFINITY]
. -
Range:
[0, INFINITY]
.
Examples:
sqrt(0.0) # => 0.0
sqrt(0.5) # => 0.7071067811865476
sqrt(1.0) # => 1.0
sqrt(2.0) # => 1.4142135623730951
sqrt(4.0) # => 2.0
sqrt(9.0) # => 3.0
sqrt(INFINITY) # => Infinity
673 674 675 676 677 |
# File 'math.c', line 673
static VALUE
math_sqrt(VALUE unused_obj, VALUE x)
{
return rb_math_sqrt(x);
}
|
.tan(x) ⇒ Float
Returns the tangent of x
in radians.
-
Domain:
(-INFINITY, INFINITY)
. -
Range:
(-INFINITY, INFINITY)
.
Examples:
tan(-PI) # => 1.2246467991473532e-16 # -0.0000000000000001
tan(-PI/2) # => -1.633123935319537e+16 # -16331239353195370.0
tan(0.0) # => 0.0
tan(PI/2) # => 1.633123935319537e+16 # 16331239353195370.0
tan(PI) # => -1.2246467991473532e-16 # -0.0000000000000001
167 168 169 170 171 |
# File 'math.c', line 167
static VALUE
math_tan(VALUE unused_obj, VALUE x)
{
return DBL2NUM(tan(Get_Double(x)));
}
|
.tanh(x) ⇒ Float
Returns the hyperbolic tangent of x
in radians.
-
Domain:
[-INFINITY, INFINITY]
. -
Range:
[-1, 1]
.
Examples:
tanh(-INFINITY) # => -1.0
tanh(0.0) # => 0.0
tanh(INFINITY) # => 1.0
349 350 351 352 353 |
# File 'math.c', line 349
static VALUE
math_tanh(VALUE unused_obj, VALUE x)
{
return DBL2NUM(tanh(Get_Double(x)));
}
|
Instance Method Details
#acos(x) ⇒ Float (private)
Returns the arc cosine of x
.
-
Domain:
[-1, 1]
. -
Range:
[0, PI]
.
Examples:
acos(-1.0) # => 3.141592653589793 # PI
acos(0.0) # => 1.5707963267948966 # PI/2
acos(1.0) # => 0.0
196 197 198 199 200 |
# File 'math.c', line 196
static VALUE
math_acos(VALUE unused_obj, VALUE x)
{
math_arc(x, acos)
}
|
#acosh(x) ⇒ Float (private)
Returns the inverse hyperbolic cosine of x
.
-
Domain:
[1, INFINITY]
. -
Range:
[0, INFINITY]
.
Examples:
acosh(1.0) # => 0.0
acosh(INFINITY) # => Infinity
371 372 373 374 375 376 377 378 379 |
# File 'math.c', line 371
static VALUE
math_acosh(VALUE unused_obj, VALUE x)
{
double d;
d = Get_Double(x);
domain_check_min(d, 1.0, "acosh");
return DBL2NUM(acosh(d));
}
|
#asin(x) ⇒ Float (private)
Returns the arc sine of x
.
-
Domain:
[-1, -1]
. -
Range:
[-PI/2, PI/2]
.
Examples:
asin(-1.0) # => -1.5707963267948966 # -PI/2
asin(0.0) # => 0.0
asin(1.0) # => 1.5707963267948966 # PI/2
219 220 221 222 223 |
# File 'math.c', line 219
static VALUE
math_asin(VALUE unused_obj, VALUE x)
{
math_arc(x, asin)
}
|
#asinh(x) ⇒ Float (private)
Returns the inverse hyperbolic sine of x
.
-
Domain:
[-INFINITY, INFINITY]
. -
Range:
[-INFINITY, INFINITY]
.
Examples:
asinh(-INFINITY) # => -Infinity
asinh(0.0) # => 0.0
asinh(INFINITY) # => Infinity
398 399 400 401 402 |
# File 'math.c', line 398
static VALUE
math_asinh(VALUE unused_obj, VALUE x)
{
return DBL2NUM(asinh(Get_Double(x)));
}
|
#atan(x) ⇒ Float (private)
Returns the arc tangent of x
.
-
Domain:
[-INFINITY, INFINITY]
. -
Range:
[-PI/2, PI/2]
.
Examples:
atan(-INFINITY) # => -1.5707963267948966 # -PI2
atan(-PI) # => -1.2626272556789115
atan(-PI/2) # => -1.0038848218538872
atan(0.0) # => 0.0
atan(PI/2) # => 1.0038848218538872
atan(PI) # => 1.2626272556789115
atan(INFINITY) # => 1.5707963267948966 # PI/2
246 247 248 249 250 |
# File 'math.c', line 246
static VALUE
math_atan(VALUE unused_obj, VALUE x)
{
return DBL2NUM(atan(Get_Double(x)));
}
|
#atan2(y, x) ⇒ Float (private)
Returns the arc tangent of y
and x
in radians.
-
Domain of
y
:[-INFINITY, INFINITY]
. -
Domain of
x
:[-INFINITY, INFINITY]
. -
Range:
[-PI, PI]
.
Examples:
atan2(-1.0, -1.0) # => -2.356194490192345 # -3*PI/4
atan2(-1.0, 0.0) # => -1.5707963267948966 # -PI/2
atan2(-1.0, 1.0) # => -0.7853981633974483 # -PI/4
atan2(0.0, -1.0) # => 3.141592653589793 # PI
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'math.c', line 61
static VALUE
math_atan2(VALUE unused_obj, VALUE y, VALUE x)
{
double dx, dy;
dx = Get_Double(x);
dy = Get_Double(y);
if (dx == 0.0 && dy == 0.0) {
if (!signbit(dx))
return DBL2NUM(dy);
if (!signbit(dy))
return DBL2NUM(M_PI);
return DBL2NUM(-M_PI);
}
#ifndef ATAN2_INF_C99
if (isinf(dx) && isinf(dy)) {
/* optimization for FLONUM */
if (dx < 0.0) {
const double dz = (3.0 * M_PI / 4.0);
return (dy < 0.0) ? DBL2NUM(-dz) : DBL2NUM(dz);
}
else {
const double dz = (M_PI / 4.0);
return (dy < 0.0) ? DBL2NUM(-dz) : DBL2NUM(dz);
}
}
#endif
return DBL2NUM(atan2(dy, dx));
}
|
#atanh(x) ⇒ Float (private)
Returns the inverse hyperbolic tangent of x
.
-
Domain:
[-1, 1]
. -
Range:
[-INFINITY, INFINITY]
.
Examples:
atanh(-1.0) # => -Infinity
atanh(0.0) # => 0.0
atanh(1.0) # => Infinity
421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'math.c', line 421
static VALUE
math_atanh(VALUE unused_obj, VALUE x)
{
double d;
d = Get_Double(x);
domain_check_range(d, -1.0, +1.0, "atanh");
/* check for pole error */
if (d == -1.0) return DBL2NUM(-HUGE_VAL);
if (d == +1.0) return DBL2NUM(+HUGE_VAL);
return DBL2NUM(atanh(d));
}
|
#cbrt(x) ⇒ Float (private)
Returns the cube root of x
.
-
Domain:
[-INFINITY, INFINITY]
. -
Range:
[-INFINITY, INFINITY]
.
Examples:
cbrt(-INFINITY) # => -Infinity
cbrt(-27.0) # => -3.0
cbrt(-8.0) # => -2.0
cbrt(-2.0) # => -1.2599210498948732
cbrt(1.0) # => 1.0
cbrt(0.0) # => 0.0
cbrt(1.0) # => 1.0
cbrt(2.0) # => 1.2599210498948732
cbrt(8.0) # => 2.0
cbrt(27.0) # => 3.0
cbrt(INFINITY) # => Infinity
741 742 743 744 745 746 747 748 749 750 751 752 |
# File 'math.c', line 741
static VALUE
math_cbrt(VALUE unused_obj, VALUE x)
{
double f = Get_Double(x);
double r = cbrt(f);
#if defined __GLIBC__
if (isfinite(r) && !(f == 0.0 && r == 0.0)) {
r = (2.0 * r + (f / r / r)) / 3.0;
}
#endif
return DBL2NUM(r);
}
|
#cos(x) ⇒ Float (private)
112 113 114 115 116 |
# File 'math.c', line 112
static VALUE
math_cos(VALUE unused_obj, VALUE x)
{
return DBL2NUM(cos(Get_Double(x)));
}
|
#cosh(x) ⇒ Float (private)
Returns the hyperbolic cosine of x
in radians.
-
Domain:
[-INFINITY, INFINITY]
. -
Range:
[1, INFINITY]
.
Examples:
cosh(-INFINITY) # => Infinity
cosh(0.0) # => 1.0
cosh(INFINITY) # => Infinity
278 279 280 281 282 |
# File 'math.c', line 278
static VALUE
math_cosh(VALUE unused_obj, VALUE x)
{
return DBL2NUM(cosh(Get_Double(x)));
}
|
#erf(x) ⇒ Float (private)
Returns the value of the Gauss error function for x
.
- Domain: <tt>[-INFINITY, INFINITY]</tt>.
- Range: <tt>[-1, 1]</tt>.
Examples:
erf(-INFINITY) # => -1.0
erf(0.0) # => 0.0
erf(INFINITY) # => 1.0
Related: Math.erfc.
874 875 876 877 878 |
# File 'math.c', line 874
static VALUE
math_erf(VALUE unused_obj, VALUE x)
{
return DBL2NUM(erf(Get_Double(x)));
}
|
#erfc(x) ⇒ Float (private)
Returns the value of the complementary error function for x
.
- Domain: <tt>[-INFINITY, INFINITY]</tt>.
- Range: <tt>[0, 2]</tt>.
Examples:
erfc(-INFINITY) # => 2.0
erfc(0.0) # => 1.0
erfc(INFINITY) # => 0.0
Related: Math.erf.
899 900 901 902 903 |
# File 'math.c', line 899
static VALUE
math_erfc(VALUE unused_obj, VALUE x)
{
return DBL2NUM(erfc(Get_Double(x)));
}
|
#exp(x) ⇒ Float (private)
Returns e
raised to the x
power.
-
Domain:
[-INFINITY, INFINITY]
. -
Range:
[0, INFINITY]
.
Examples:
exp(-INFINITY) # => 0.0
exp(-1.0) # => 0.36787944117144233 # 1.0/E
exp(0.0) # => 1.0
exp(0.5) # => 1.6487212707001282 # sqrt(E)
exp(1.0) # => 2.718281828459045 # E
exp(2.0) # => 7.38905609893065 # E**2
exp(INFINITY) # => Infinity
455 456 457 458 459 |
# File 'math.c', line 455
static VALUE
math_exp(VALUE unused_obj, VALUE x)
{
return DBL2NUM(exp(Get_Double(x)));
}
|
#frexp(x) ⇒ Array (private)
Returns a 2-element array containing the normalized signed float fraction
and integer exponent
of x
such that:
x = fraction * 2**exponent
See IEEE 754 double-precision binary floating-point format: binary64.
-
Domain:
[-INFINITY, INFINITY]
. -
Range
[-INFINITY, INFINITY]
.
Examples:
frexp(-INFINITY) # => [-Infinity, -1]
frexp(-2.0) # => [-0.5, 2]
frexp(-1.0) # => [-0.5, 1]
frexp(0.0) # => [0.0, 0]
frexp(1.0) # => [0.5, 1]
frexp(2.0) # => [0.5, 2]
frexp(INFINITY) # => [Infinity, -1]
Related: Math.ldexp (inverse of Math.frexp).
782 783 784 785 786 787 788 789 790 |
# File 'math.c', line 782
static VALUE
math_frexp(VALUE unused_obj, VALUE x)
{
double d;
int exp;
d = frexp(Get_Double(x), &exp);
return rb_assoc_new(DBL2NUM(d), INT2NUM(exp));
}
|
#gamma(x) ⇒ Float (private)
Returns the value of the gamma function for x
.
- Domain: <tt>(-INFINITY, INFINITY]</tt> excluding negative integers.
- Range: <tt>[-INFINITY, INFINITY]</tt>.
Examples:
gamma(-2.5) # => -0.9453087204829431
gamma(-1.5) # => 2.3632718012073513
gamma(-0.5) # => -3.5449077018110375
gamma(0.0) # => Infinity
gamma(1.0) # => 1.0
gamma(2.0) # => 1.0
gamma(3.0) # => 2.0
gamma(4.0) # => 6.0
gamma(5.0) # => 24.0
Related: Math.lgamma.
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 |
# File 'math.c', line 930
static VALUE
math_gamma(VALUE unused_obj, VALUE x)
{
static const double fact_table[] = {
/* fact(0) */ 1.0,
/* fact(1) */ 1.0,
/* fact(2) */ 2.0,
/* fact(3) */ 6.0,
/* fact(4) */ 24.0,
/* fact(5) */ 120.0,
/* fact(6) */ 720.0,
/* fact(7) */ 5040.0,
/* fact(8) */ 40320.0,
/* fact(9) */ 362880.0,
/* fact(10) */ 3628800.0,
/* fact(11) */ 39916800.0,
/* fact(12) */ 479001600.0,
/* fact(13) */ 6227020800.0,
/* fact(14) */ 87178291200.0,
/* fact(15) */ 1307674368000.0,
/* fact(16) */ 20922789888000.0,
/* fact(17) */ 355687428096000.0,
/* fact(18) */ 6402373705728000.0,
/* fact(19) */ 121645100408832000.0,
/* fact(20) */ 2432902008176640000.0,
/* fact(21) */ 51090942171709440000.0,
/* fact(22) */ 1124000727777607680000.0,
/* fact(23)=25852016738884976640000 needs 56bit mantissa which is
* impossible to represent exactly in IEEE 754 double which have
* 53bit mantissa. */
};
enum {NFACT_TABLE = numberof(fact_table)};
double d;
d = Get_Double(x);
/* check for domain error */
if (isinf(d)) {
if (signbit(d)) domain_error("gamma");
return DBL2NUM(HUGE_VAL);
}
if (d == 0.0) {
return signbit(d) ? DBL2NUM(-HUGE_VAL) : DBL2NUM(HUGE_VAL);
}
if (d == floor(d)) {
domain_check_min(d, 0.0, "gamma");
if (1.0 <= d && d <= (double)NFACT_TABLE) {
return DBL2NUM(fact_table[(int)d - 1]);
}
}
return DBL2NUM(tgamma(d));
}
|
#hypot(a, b) ⇒ Float (private)
Returns sqrt(a**2 + b**2)
, which is the length of the longest side c
(the hypotenuse) of the right triangle whose other sides have lengths a
and b
.
-
Domain of
a
:[-INFINITY, INFINITY]
. -
Domain of +ab:
[-INFINITY, INFINITY]
. -
Range:
[0, INFINITY]
.
Examples:
hypot(0.0, 1.0) # => 1.0
hypot(1.0, 1.0) # => 1.4142135623730951 # sqrt(2.0)
hypot(3.0, 4.0) # => 5.0
hypot(5.0, 12.0) # => 13.0
hypot(1.0, sqrt(3.0)) # => 1.9999999999999998 # Near 2.0
Note that if either argument is INFINITY
or -INFINITY
, the result is Infinity
.
849 850 851 852 853 |
# File 'math.c', line 849
static VALUE
math_hypot(VALUE unused_obj, VALUE x, VALUE y)
{
return DBL2NUM(hypot(Get_Double(x), Get_Double(y)));
}
|
#ldexp(fraction, exponent) ⇒ Float (private)
Returns the value of fraction * 2**exponent
.
-
Domain of
fraction
:[0.0, 1.0)
. -
Domain of
exponent
:[0, 1024]
(larger values are equivalent to 1024).
See IEEE 754 double-precision binary floating-point format: binary64.
Examples:
ldexp(-INFINITY, -1) # => -Infinity
ldexp(-0.5, 2) # => -2.0
ldexp(-0.5, 1) # => -1.0
ldexp(0.0, 0) # => 0.0
ldexp(-0.5, 1) # => 1.0
ldexp(-0.5, 2) # => 2.0
ldexp(INFINITY, -1) # => Infinity
Related: Math.frexp (inverse of Math.ldexp).
818 819 820 821 822 |
# File 'math.c', line 818
static VALUE
math_ldexp(VALUE unused_obj, VALUE x, VALUE n)
{
return DBL2NUM(ldexp(Get_Double(x), NUM2INT(n)));
}
|
#lgamma(x) ⇒ Array, ... (private)
Returns a 2-element array equivalent to:
[Math.log(Math.gamma(x).abs), Math.gamma(x) < 0 ? -1 : 1]
See {logarithmic gamma function}[https://en.wikipedia.org/wiki/Gamma_function#The_log-gamma_function].
- Domain: <tt>(-INFINITY, INFINITY]</tt>.
- Range of first element: <tt>(-INFINITY, INFINITY]</tt>.
- Second element is -1 or 1.
Examples:
lgamma(-4.0) # => [Infinity, -1]
lgamma(-3.0) # => [Infinity, -1]
lgamma(-2.0) # => [Infinity, -1]
lgamma(-1.0) # => [Infinity, -1]
lgamma(0.0) # => [Infinity, 1]
lgamma(1.0) # => [0.0, 1]
lgamma(2.0) # => [0.0, 1]
lgamma(3.0) # => [0.6931471805599436, 1]
lgamma(4.0) # => [1.7917594692280545, 1]
lgamma(-2.5) # => [-0.05624371649767279, -1]
lgamma(-1.5) # => [0.8600470153764797, 1]
lgamma(-0.5) # => [1.265512123484647, -1]
lgamma(0.5) # => [0.5723649429247004, 1]
lgamma(1.5) # => [-0.12078223763524676, 1]
lgamma(2.5) # => [0.2846828704729205, 1]
Related: Math.gamma.
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 |
# File 'math.c', line 1019
static VALUE
math_lgamma(VALUE unused_obj, VALUE x)
{
double d;
int sign=1;
VALUE v;
d = Get_Double(x);
/* check for domain error */
if (isinf(d)) {
if (signbit(d)) domain_error("lgamma");
return rb_assoc_new(DBL2NUM(HUGE_VAL), INT2FIX(1));
}
if (d == 0.0) {
VALUE vsign = signbit(d) ? INT2FIX(-1) : INT2FIX(+1);
return rb_assoc_new(DBL2NUM(HUGE_VAL), vsign);
}
v = DBL2NUM(lgamma_r(d, &sign));
return rb_assoc_new(v, INT2FIX(sign));
}
|
#log(x, base = Math::E) ⇒ Float (private)
Returns the base base
logarithm of x
.
-
Domain:
[0, INFINITY]
. -
Range:
[-INFINITY, INFINITY)]
.
Examples:
log(0.0) # => -Infinity
log(1.0) # => 0.0
log(E) # => 1.0
log(INFINITY) # => Infinity
log(0.0, 2.0) # => -Infinity
log(1.0, 2.0) # => 0.0
log(2.0, 2.0) # => 1.0
log(0.0, 10.0) # => -Infinity
log(1.0, 10.0) # => 0.0
log(10.0, 10.0) # => 1.0
505 506 507 508 509 |
# File 'math.c', line 505
static VALUE
math_log(int argc, const VALUE *argv, VALUE unused_obj)
{
return rb_math_log(argc, argv);
}
|
#log10(x) ⇒ Float (private)
Returns the base 10 logarithm of x
.
-
Domain:
[0, INFINITY]
. -
Range:
[-INFINITY, INFINITY]
.
Examples:
log10(0.0) # => -Infinity
log10(1.0) # => 0.0
log10(10.0) # => 1.0
log10(INFINITY) # => Infinity
637 638 639 640 641 642 643 644 645 646 647 648 |
# File 'math.c', line 637
static VALUE
math_log10(VALUE unused_obj, VALUE x)
{
size_t numbits;
double d = get_double_rshift(x, &numbits);
domain_check_min(d, 0.0, "log10");
/* check for pole error */
if (d == 0.0) return DBL2NUM(-HUGE_VAL);
return DBL2NUM(log10(d) + numbits * log10(2)); /* log10(d * 2 ** numbits) */
}
|
#log2(x) ⇒ Float (private)
Returns the base 2 logarithm of x
.
-
Domain:
[0, INFINITY]
. -
Range:
[-INFINITY, INFINITY]
.
Examples:
log2(0.0) # => -Infinity
log2(1.0) # => 0.0
log2(2.0) # => 1.0
log2(INFINITY) # => Infinity
606 607 608 609 610 611 612 613 614 615 616 617 |
# File 'math.c', line 606
static VALUE
math_log2(VALUE unused_obj, VALUE x)
{
size_t numbits;
double d = get_double_rshift(x, &numbits);
domain_check_min(d, 0.0, "log2");
/* check for pole error */
if (d == 0.0) return DBL2NUM(-HUGE_VAL);
return DBL2NUM(log2(d) + numbits); /* log2(d * 2 ** numbits) */
}
|
#sin(x) ⇒ Float (private)
139 140 141 142 143 |
# File 'math.c', line 139
static VALUE
math_sin(VALUE unused_obj, VALUE x)
{
return DBL2NUM(sin(Get_Double(x)));
}
|
#sinh(x) ⇒ Float (private)
Returns the hyperbolic sine of x
in radians.
-
Domain:
[-INFINITY, INFINITY]
. -
Range:
[-INFINITY, INFINITY]
.
Examples:
sinh(-INFINITY) # => -Infinity
sinh(0.0) # => 0.0
sinh(INFINITY) # => Infinity
310 311 312 313 314 |
# File 'math.c', line 310
static VALUE
math_sinh(VALUE unused_obj, VALUE x)
{
return DBL2NUM(sinh(Get_Double(x)));
}
|
#sqrt(x) ⇒ Float (private)
Returns the principal (non-negative) square root of x
.
-
Domain:
[0, INFINITY]
. -
Range:
[0, INFINITY]
.
Examples:
sqrt(0.0) # => 0.0
sqrt(0.5) # => 0.7071067811865476
sqrt(1.0) # => 1.0
sqrt(2.0) # => 1.4142135623730951
sqrt(4.0) # => 2.0
sqrt(9.0) # => 3.0
sqrt(INFINITY) # => Infinity
673 674 675 676 677 |
# File 'math.c', line 673
static VALUE
math_sqrt(VALUE unused_obj, VALUE x)
{
return rb_math_sqrt(x);
}
|
#tan(x) ⇒ Float (private)
Returns the tangent of x
in radians.
-
Domain:
(-INFINITY, INFINITY)
. -
Range:
(-INFINITY, INFINITY)
.
Examples:
tan(-PI) # => 1.2246467991473532e-16 # -0.0000000000000001
tan(-PI/2) # => -1.633123935319537e+16 # -16331239353195370.0
tan(0.0) # => 0.0
tan(PI/2) # => 1.633123935319537e+16 # 16331239353195370.0
tan(PI) # => -1.2246467991473532e-16 # -0.0000000000000001
167 168 169 170 171 |
# File 'math.c', line 167
static VALUE
math_tan(VALUE unused_obj, VALUE x)
{
return DBL2NUM(tan(Get_Double(x)));
}
|
#tanh(x) ⇒ Float (private)
Returns the hyperbolic tangent of x
in radians.
-
Domain:
[-INFINITY, INFINITY]
. -
Range:
[-1, 1]
.
Examples:
tanh(-INFINITY) # => -1.0
tanh(0.0) # => 0.0
tanh(INFINITY) # => 1.0
349 350 351 352 353 |
# File 'math.c', line 349
static VALUE
math_tanh(VALUE unused_obj, VALUE x)
{
return DBL2NUM(tanh(Get_Double(x)));
}
|