Difference between revisions of "Ruby Math Functions and Methods"
From Techotopia
Line 8: | Line 8: | ||
− | The Ruby ''Math'' module provides the Ruby programmer with an extensive range of methods | + | The Ruby ''Math'' module provides the Ruby programmer with an extensive range of methods for performing mathematical tasks. In addition, the ''Math'' module includes two commonly used mathematical constants. |
== Ruby Math Constants == | == Ruby Math Constants == | ||
Line 19: | Line 19: | ||
</pre> | </pre> | ||
− | As we can see, as of | + | As we can see, as of the current version of Ruby, only two constants are defined. We can access these using :: notation: |
<pre> | <pre> | ||
Line 31: | Line 31: | ||
== Ruby Math Methods == | == Ruby Math Methods == | ||
− | As mentioned previously, Ruby provides an extensive range of | + | As mentioned previously, Ruby provides an extensive range of math related methods. These are listed and described in the following table. |
<table border="1" cellspacing="0"> | <table border="1" cellspacing="0"> |
Revision as of 12:46, 1 December 2007
Previous | Table of Contents | Next |
Ruby Operator Precedence | Understanding Ruby Logical Operators |
The Ruby Math module provides the Ruby programmer with an extensive range of methods for performing mathematical tasks. In addition, the Math module includes two commonly used mathematical constants.
Ruby Math Constants
The Ruby Math module includes common math constants. A list of constants may be accessed using the constants method:
Math.constants => ["E", "PI"]
As we can see, as of the current version of Ruby, only two constants are defined. We can access these using :: notation:
Math::PI => 3.14159265358979 Math::E => 2.71828182845905
Ruby Math Methods
As mentioned previously, Ruby provides an extensive range of math related methods. These are listed and described in the following table.
Method name | Description |
---|---|
Math.acos, Math.acos! | Arc cosine |
Math.acosh, Math.acosh! | Hyperbolic arc cosine |
Math.asin, Math.asin! | Arc sine |
Math.asinh, Math.asinh | Hyperbolic arc sine |
Math.atan, Math.atan!, Math.atan2, Math.atan2! | Arc tangent. atan takes an x argument. atan2 takes x and y arguments |
Math.atanh, Math.atanh! | Hyperbolic arc tangent |
Math.cos, Math.cos! | Cosine |
Math.cosh, Math.cosh | Hyperbolic cosine |
Math.sin, Math.sin! | Sine |
Math.erf | Error function |
Match.erfc | Complementary error function |
Math.exp, Math.exp! | Base x of Euler |
Math.frexp | Normalized fraction and exponent |
Math.hypot | Hypotenuse |
Math.ldexp | Floating-point value corresponding to mantissa and exponent |
Math.sinh, Math.sinh! | Hyperbolic sine |
Math.sqrt, Math.sqrt! | Square root |
Math.tan, Math.tan! | Tangent |
Math.tanh, Math.tanh! | Hyperbolic tangent |
Some Examples
Now that we have a list of the math methods available to us, we can start to use them:
To perform a square root:
Math.sqrt(9) => 3.0
Or a Euler calculation:
Math.exp(2) => 7.38905609893065
Previous | Table of Contents | Next |
Ruby Operator Precedence | Understanding Ruby Logical Operators |