Module: Math

Defined in:
math.c

Overview

The Math module contains module functions for basic trigonometric and transcendental functions. See class Float for a list of constants that define Ruby’s floating point accuracy.

Constant Summary collapse

PI =
rb_float_new(atan(1.0)*4.0)
E =
rb_float_new(exp(1.0))

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.acos(x) ⇒ Float

Computes the arc cosine of x. Returns 0..PI.

Returns:



122
123
124
# File 'math.c', line 122

static VALUE
math_acos(obj, x)
VALUE obj, x;

.acosh(x) ⇒ Float

Computes the inverse hyperbolic cosine of x.

Returns:



252
253
254
# File 'math.c', line 252

static VALUE
math_acosh(obj, x)
VALUE obj, x;

.asin(x) ⇒ Float

Computes the arc sine of x. Returns 0..PI.

Returns:



142
143
144
# File 'math.c', line 142

static VALUE
math_asin(obj, x)
VALUE obj, x;

.asinh(x) ⇒ Float

Computes the inverse hyperbolic sine of x.

Returns:



272
273
274
# File 'math.c', line 272

static VALUE
math_asinh(obj, x)
VALUE obj, x;

.atan(x) ⇒ Float

Computes the arc tangent of x. Returns -PI/2 .. PI/2.

Returns:



162
163
164
# File 'math.c', line 162

static VALUE
math_atan(obj, x)
VALUE obj, x;

.atan2(y, x) ⇒ Float

Computes the arc tangent given y and x. Returns -PI..PI.

Returns:



56
57
58
# File 'math.c', line 56

static VALUE
math_atan2(obj, y, x)
VALUE obj, x, y;

.atanh(x) ⇒ Float

Computes the inverse hyperbolic tangent of x.

Returns:



287
288
289
# File 'math.c', line 287

static VALUE
math_atanh(obj, x)
VALUE obj, x;

.cos(x) ⇒ Float

Computes the cosine of x (expressed in radians). Returns -1..1.

Returns:



73
74
75
# File 'math.c', line 73

static VALUE
math_cos(obj, x)
VALUE obj, x;

.cosh(x) ⇒ Float

Computes the hyperbolic cosine of x (expressed in radians).

Returns:



186
187
188
# File 'math.c', line 186

static VALUE
math_cosh(obj, x)
VALUE obj, x;

.erf(x) ⇒ Float

Calculates the error function of x.

Returns:



452
453
454
# File 'math.c', line 452

static VALUE
math_erf(obj, x)
VALUE obj, x;

.erfc(x) ⇒ Float

Calculates the complementary error function of x.

Returns:



467
468
469
# File 'math.c', line 467

static VALUE
math_erfc(obj, x)
VALUE obj, x;

.exp(x) ⇒ Float

Returns e**x.

Returns:



307
308
309
# File 'math.c', line 307

static VALUE
math_exp(obj, x)
VALUE obj, x;

.frexp(numeric) ⇒ Array

Returns a two-element array containing the normalized fraction (a Float) and exponent (a Fixnum) of numeric.

fraction, exponent = Math.frexp(1234)   #=> [0.6025390625, 11]
fraction * 2**exponent                  #=> 1234.0

Returns:



396
397
398
# File 'math.c', line 396

static VALUE
math_frexp(obj, x)
VALUE obj, x;

.hypot(x, y) ⇒ Float

Returns sqrt(x**2 + y**2), the hypotenuse of a right-angled triangle with sides x and y.

Math.hypot(3, 4)   #=> 5.0

Returns:



437
438
439
# File 'math.c', line 437

static VALUE
math_hypot(obj, x, y)
VALUE obj, x, y;

.ldexp(flt, int) ⇒ Float

Returns the value of flt*(2**int).

fraction, exponent = Math.frexp(1234)
Math.ldexp(fraction, exponent)   #=> 1234.0

Returns:



419
420
421
# File 'math.c', line 419

static VALUE
math_ldexp(obj, x, n)
VALUE obj, x, n;

.log(numeric) ⇒ Float

Returns the natural logarithm of numeric.

Returns:



331
332
333
# File 'math.c', line 331

static VALUE
math_log(obj, x)
VALUE obj, x;

.log10(numeric) ⇒ Float

Returns the base 10 logarithm of numeric.

Returns:



351
352
353
# File 'math.c', line 351

static VALUE
math_log10(obj, x)
VALUE obj, x;

.sin(x) ⇒ Float

Computes the sine of x (expressed in radians). Returns -1..1.

Returns:



89
90
91
# File 'math.c', line 89

static VALUE
math_sin(obj, x)
VALUE obj, x;

.sinh(x) ⇒ Float

Computes the hyperbolic sine of x (expressed in radians).

Returns:



212
213
214
# File 'math.c', line 212

static VALUE
math_sinh(obj, x)
VALUE obj, x;

.sqrt(numeric) ⇒ Float

Returns the non-negative square root of numeric.

Returns:



371
372
373
# File 'math.c', line 371

static VALUE
math_sqrt(obj, x)
VALUE obj, x;

.tan(x) ⇒ Float

Returns the tangent of x (expressed in radians).

Returns:



106
107
108
# File 'math.c', line 106

static VALUE
math_tan(obj, x)
VALUE obj, x;

.tanhFloat

Computes the hyperbolic tangent of x (expressed in radians).

Returns:



237
238
239
# File 'math.c', line 237

static VALUE
math_tanh(obj, x)
VALUE obj, x;

Instance Method Details

#acos(x) ⇒ Float (private)

Computes the arc cosine of x. Returns 0..PI.

Returns:



122
123
124
# File 'math.c', line 122

static VALUE
math_acos(obj, x)
VALUE obj, x;

#acosh(x) ⇒ Float (private)

Computes the inverse hyperbolic cosine of x.

Returns:



252
253
254
# File 'math.c', line 252

static VALUE
math_acosh(obj, x)
VALUE obj, x;

#asin(x) ⇒ Float (private)

Computes the arc sine of x. Returns 0..PI.

Returns:



142
143
144
# File 'math.c', line 142

static VALUE
math_asin(obj, x)
VALUE obj, x;

#asinh(x) ⇒ Float (private)

Computes the inverse hyperbolic sine of x.

Returns:



272
273
274
# File 'math.c', line 272

static VALUE
math_asinh(obj, x)
VALUE obj, x;

#atan(x) ⇒ Float (private)

Computes the arc tangent of x. Returns -PI/2 .. PI/2.

Returns:



162
163
164
# File 'math.c', line 162

static VALUE
math_atan(obj, x)
VALUE obj, x;

#atan2(y, x) ⇒ Float (private)

Computes the arc tangent given y and x. Returns -PI..PI.

Returns:



56
57
58
# File 'math.c', line 56

static VALUE
math_atan2(obj, y, x)
VALUE obj, x, y;

#atanh(x) ⇒ Float (private)

Computes the inverse hyperbolic tangent of x.

Returns:



287
288
289
# File 'math.c', line 287

static VALUE
math_atanh(obj, x)
VALUE obj, x;

#cos(x) ⇒ Float (private)

Computes the cosine of x (expressed in radians). Returns -1..1.

Returns:



73
74
75
# File 'math.c', line 73

static VALUE
math_cos(obj, x)
VALUE obj, x;

#cosh(x) ⇒ Float (private)

Computes the hyperbolic cosine of x (expressed in radians).

Returns:



186
187
188
# File 'math.c', line 186

static VALUE
math_cosh(obj, x)
VALUE obj, x;

#erf(x) ⇒ Float (private)

Calculates the error function of x.

Returns:



452
453
454
# File 'math.c', line 452

static VALUE
math_erf(obj, x)
VALUE obj, x;

#erfc(x) ⇒ Float (private)

Calculates the complementary error function of x.

Returns:



467
468
469
# File 'math.c', line 467

static VALUE
math_erfc(obj, x)
VALUE obj, x;

#exp(x) ⇒ Float (private)

Returns e**x.

Returns:



307
308
309
# File 'math.c', line 307

static VALUE
math_exp(obj, x)
VALUE obj, x;

#frexp(numeric) ⇒ Array (private)

Returns a two-element array containing the normalized fraction (a Float) and exponent (a Fixnum) of numeric.

fraction, exponent = Math.frexp(1234)   #=> [0.6025390625, 11]
fraction * 2**exponent                  #=> 1234.0

Returns:



396
397
398
# File 'math.c', line 396

static VALUE
math_frexp(obj, x)
VALUE obj, x;

#hypot(x, y) ⇒ Float (private)

Returns sqrt(x**2 + y**2), the hypotenuse of a right-angled triangle with sides x and y.

Math.hypot(3, 4)   #=> 5.0

Returns:



437
438
439
# File 'math.c', line 437

static VALUE
math_hypot(obj, x, y)
VALUE obj, x, y;

#ldexp(flt, int) ⇒ Float (private)

Returns the value of flt*(2**int).

fraction, exponent = Math.frexp(1234)
Math.ldexp(fraction, exponent)   #=> 1234.0

Returns:



419
420
421
# File 'math.c', line 419

static VALUE
math_ldexp(obj, x, n)
VALUE obj, x, n;

#log(numeric) ⇒ Float (private)

Returns the natural logarithm of numeric.

Returns:



331
332
333
# File 'math.c', line 331

static VALUE
math_log(obj, x)
VALUE obj, x;

#log10(numeric) ⇒ Float (private)

Returns the base 10 logarithm of numeric.

Returns:



351
352
353
# File 'math.c', line 351

static VALUE
math_log10(obj, x)
VALUE obj, x;

#sin(x) ⇒ Float (private)

Computes the sine of x (expressed in radians). Returns -1..1.

Returns:



89
90
91
# File 'math.c', line 89

static VALUE
math_sin(obj, x)
VALUE obj, x;

#sinh(x) ⇒ Float (private)

Computes the hyperbolic sine of x (expressed in radians).

Returns:



212
213
214
# File 'math.c', line 212

static VALUE
math_sinh(obj, x)
VALUE obj, x;

#sqrt(numeric) ⇒ Float (private)

Returns the non-negative square root of numeric.

Returns:



371
372
373
# File 'math.c', line 371

static VALUE
math_sqrt(obj, x)
VALUE obj, x;

#tan(x) ⇒ Float (private)

Returns the tangent of x (expressed in radians).

Returns:



106
107
108
# File 'math.c', line 106

static VALUE
math_tan(obj, x)
VALUE obj, x;

#tanhFloat (private)

Computes the hyperbolic tangent of x (expressed in radians).

Returns:



237
238
239
# File 'math.c', line 237

static VALUE
math_tanh(obj, x)
VALUE obj, x;