Difference between revisions of "Ruby Math Functions and Methods"

From Techotopia
Jump to: navigation, search
(Removing all content from page)
m (Text replacement - "<table border="0" cellspacing="0"> " to "<table border="0" cellspacing="0" width="100%">")
 
(14 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
<table border="0" cellspacing="0" width="100%"><tr>
 +
<td width="20%">[[Ruby Operator Precedence|Previous]]<td align="center">[[Ruby Essentials|Table of Contents]]<td width="20%" align="right">[[Understanding Ruby Logical Operators|Next]]</td>
 +
<tr>
 +
<td width="20%">Ruby Operator Precedence<td align="center"><td width="20%" align="right">Understanding Ruby Logical Operators</td>
 +
</table>
 +
<hr>
  
 +
 +
<htmlet>ruby</htmlet>
 +
 +
 +
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:
 +
 +
<pre>
 +
Math.constants
 +
=> ["E", "PI"]
 +
</pre>
 +
 +
As we can see, as of the 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 math related methods. These are listed and described in the following table.
 +
 +
<htmlet>adsdaqbox_flow</htmlet>
 +
<table border="1" cellspacing="0">
 +
<tr style="background:#efefef;">
 +
<th>Method name<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 x and y arguments</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 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>
 +
 +
== 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:
 +
 +
<pre>
 +
Math.sqrt(9)
 +
=> 3.0
 +
</pre>
 +
 +
Or a Euler calculation:
 +
 +
<pre>
 +
Math.exp(2)
 +
=> 7.38905609893065
 +
</pre>
 +
 +
== Summary ==
 +
 +
This chapter has covered the concepts behind Ruby methods and functions. The next chapter will focus on Ruby logical operators.
 +
 +
 +
<htmlet>ruby</htmlet>
 +
 +
 +
<hr>
 +
<table border="0" cellspacing="0" width="100%"><tr>
 +
<td width="20%">[[Ruby Operator Precedence|Previous]]<td align="center">[[Ruby Essentials|Table of Contents]]<td width="20%" align="right">[[Understanding Ruby Logical Operators|Next]]</td>
 +
<tr>
 +
<td width="20%">Ruby Operator Precedence<td align="center"><td width="20%" align="right">Understanding Ruby Logical Operators</td>
 +
</table>

Latest revision as of 20:17, 27 October 2016

PreviousTable of ContentsNext
Ruby Operator PrecedenceUnderstanding Ruby Logical Operators


Purchase and download the full PDF and ePub editions of this Ruby eBook for only $8.99


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.


Contents


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

Summary

This chapter has covered the concepts behind Ruby methods and functions. The next chapter will focus on Ruby logical operators.


Purchase and download the full PDF and ePub editions of this Ruby eBook for only $8.99



PreviousTable of ContentsNext
Ruby Operator PrecedenceUnderstanding Ruby Logical Operators