Ruby Tutorial Ruby References

Ruby - Math Module



The Ruby has a Math module which contains number of methods and constants that are available for mathematical operation. The methods and constants of Math module are listed below:

Public Class Methods

Trigonometric Methods

MethodsDescription
sin() Returns the trigonometric sine of an angle in radians.
cos() Returns the trigonometric cosine of an angle in radians.
tan() Returns the trigonometric tangent of an angle in radians.
asin() Returns the arc sine of a value.
acos() Returns the arc cosine of a value.
atan() Returns the arc tangent of a value.
atan2() Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta).

Hyperbolic Methods

MethodsDescription
sinh() Returns the hyperbolic sine of a value.
cosh() Returns the hyperbolic cosine of a value.
tanh() Returns the hyperbolic tangent of a value.
asinh() Returns the inverse hyperbolic sine of a value
acosh() Returns the inverse hyperbolic cosine of a value
atanh() Returns the inverse hyperbolic tangent of a value

Exponential & Logarithmic Methods

MethodsDescription
exp() Returns the exponent of e.
log() Returns the natural logarithm of a given number.
log10() Returns the base-10 logarithm of a given number.
log2() Returns the base-2 (binary) logarithm of a given number.

Other Methods

MethodsDescription
sqrt() Returns the square root of the given number.
cbrt() Returns the cube root of the given number.
hypot() Returns square root of sum of squares of two arguments, i.e., sqrt(x2 +y2).
frexp() Get significand and exponent.
ldexp() Generate value from significand and exponent.

Error & gamma Methods

FunctionsDescription
erf() Returns the error function of the argument.
erfc() Returns the complementary error function of the argument.
gamma() Returns gamma function of the argument.
lgamma() Returns log-gamma function of the argument.

Constants

PropertiesDescription
Math::E Euler's constant and the base of natural logarithms. It is approximately 2.718.
Math::PI Ratio of the a circle's circumference to its diameter. It is approximately 3.14159.

5