Changes

Jump to: navigation, search

Ruby Math Functions and Methods

2,163 bytes added, 15:44, 30 November 2007
no edit summary
The Ruby ''Math'' module provides the Ruby programmer with an extensive range of methods to 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:
 
<pre>
Math.constants
=> ["E", "PI"]
</pre>
 
As we can see, as of tyhe current version of Ruby, only two constants are defined. We can access these using :: notation:
 
<pre>
Math::PI
=> 3.14159265358979
 
Math::E
=> 2.71828182845905
</pre>
 
== Ruby Math Methods ==
 
As mentioned previously, Ruby provides an extensive range of meth related methods. These are liosted and described in the following table.
 
<table border="1" cellspacing="0">
<tr style="background:#efefef;">
<tr><th>Method(s)<td> Description</th></tr>
<tr><td>Math.acos, Math.acos!<td> Arc cosine</td></tr>
<tr><td>Math.acosh, Math.acosh! <td>Hyperbolic arc cosine</td></tr>
<tr><td>Math.asin, Math.asin! <td>Arc sine</td></tr>
<tr><td>Math.asinh, Math.asinh <td>Hyperbolic arc sine</td></tr>
<tr><td>Math.atan, Math.atan!, Math.atan2, Math.atan2! <td>Arc tangent; atan takes an x argument; atan2 takes an x and a y argument</td></tr>
<tr><td>Math.atanh, Math.atanh! <td>Hyperbolic arc tangent</td></tr>
<tr><td>Math.cos, Math.cos! <td>Cosine</td></tr>
<tr><td>Math.cosh, Math.cosh <td>Hyperbolic cosine</td></tr>
<tr><td>Math.sin, Math.sin! <td>Sine</td></tr>
<tr><td>Math.erf <td>Error function</td></tr>
<tr><td>Match.erfc <td>Complementary error function</td></tr>
<tr><td>Math.exp, Math.exp! <td>Base x of Euler</td></tr>
<tr><td>Math.frexp <td>Normalized fraction and exponent</td></tr>
<tr><td>Math.hypot <td>Hypotenuse</td></tr>
<tr><td>Math.ldexp <td>Floating-point value corresponding to a given mantissa and exponent</td></tr>
<tr><td>Math.sinh, Math.sinh! <td>Hyperbolic sine</td></tr>
<tr><td>Math.sqrt, Math.sqrt! <td>Square root</td></tr>
<tr><td>Math.tan, Math.tan! <td>Tangent</td></tr>
<tr><td>Math.tanh, Math.tanh! <td>Hyperbolic tangent</td></tr>
</table>

Navigation menu