Updated April 4, 2023

## Introduction of Ruby Math

Ruby provides various Modules which are defined as a set of methods, constants, and classes. Ruby Math is also one of the modules which are a collection of different methods that are used to calculate the Mathematical and trigonometric functions easily, to calculate this function ruby uses Math package. Math packages will include or load all the built-in functions that need to be loaded first to calculate or use these tricky trigonometric functions in the program. For example, log (calculate the logarithm value), sqrt (calculate the square root).

### Functions of Ruby Math with Examples

Functions of ruby math are given below:

#### 1. E

This function is constant where the value of E is the same. This function returns the base value of natural logarithm e.

**Code:**

```
puts "Value of Natural Logrithum"
puts Math::E
```

**Output:**

#### 2. PI

This function is constant, return value of π. As we have already learned this in our math concept the value of PI is 3.141

**Code:**

```
puts "Value of PI"
puts Math::PI
```

**Output:**

#### 3. acos

This function is used to calculate the arc cosine of the given value. The return type of arc cosine is always float. The range of this function is [0…PI].

**Code:**

```
puts "Value of arc cosine"
puts Math::acos(0)
```

**Output:**

#### 4. acosh

This function is used to calculate the inverse hyperbolic cosine of the given value. The return type of this function is float.

**Code:**

```
puts "Value of inverse hyperbolic cosine"
puts Math::acosh(2)
```

**Output:**

#### 5. asin

This function is used to calculate the arc sine of the given value. The return type of these functions is float and ranges from –PI/2 to PI/2.

**Code:**

```
puts "Value of arc sine"
puts Math::asin(1)
```

**Output:**

#### 6. asinh

This function is used to calculate the inverse hyperbolic sine of the given value. The return type of this function is float

**Code:**

```
puts "Value of inverse hyperbolic sine"
puts Math::asinh(1)
```

**Output:**

#### 7. atan

This function is used to calculate the arc tangent of the given value. The return types of this function are float and range from –PI to PI

**Code:**

```
puts "Value of arc tangent"
puts Math::atan(1)
```

**Output:**

#### 8. atanh

This function is used to calculate the inverse hyperbolic tangent of the given value. The return type of this function is float

**Code:**

```
puts "Value of inverse hyperbolic tangent"
puts Math::atanh(1)
```

**Output:**

#### 9. atan2

This function is used to calculate the arc tangent of the given value. It takes two parameters. The return type of this function is float and ranges from –PI to PI.

**Code:**

```
puts "Value of arc of tangent between two values"
puts Math::atan2(1,0)
```

**Output:**

#### 10. erf

This function is used to calculate the error function of the given value. The return type of this function is float.

**Code:**

```
puts "Value of error function"
puts Math::erf(3)
```

**Output:**

#### 11. erfc

This function is the opposite of erf function, as erf calculates the error function, and erfc functions are used to calculate the complementary error function of a given value. The return type of this function is float.

**Code:**

```
puts "Value of complementary error function"
puts Math::erfc(3)
```

**Output:**

#### 12. exp

This function is used to calculate the value of e_{a}, Return type of this function is float.

**Code:**

```
puts "Exponential value"
puts Math::exp(3)
```

**Output:**

#### 13. frexp

This function is used to calculate the array consist of two elements. The array contains the exponent and a normalized fraction of the given value

**Code:**

```
puts "Two Element Array"
puts Math::frexp(3)
```

**Output:**

#### 14. sqrt

This function is used to calculate the square root of the given value. The return type of the function is float. Note that this function will calculate the square root of only positive values if the negative value is given as an input, then it will throw an error

**Code:**

```
puts "Value of square root"
puts Math.sqrt(9)
```

**Output:**

#### 15. hypot

This function is used to calculate √a_{2}+b_{2} . i.e. hypotenuse of a right-angle triangle. It takes two parameters. The return type of this function is float.

**Code:**

```
puts "Value of hypotenuse of right angle triangle"
puts Math.hypot(5,5)
```

**Output:**

#### 16. log

This function is used to calculate the natural logarithm of a given value. The return type of this function is float.

**Code:**

```
puts "Value of natural logarithm"
puts Math.log(5)
```

**Output:**

#### 17. log10

This function is used to calculate the base 10 of the logarithm of a given value. The return type of this function is float.

**Code:**

```
puts "Value of natural logarithm base 10"
puts Math.log10(5)
```

**Output:**

#### 18. sin

This function is used to calculate the sine of the given value which is expressed in radian form. The return type of this function is float and ranges from -1.0 to +1.0

**Code:**

```
puts "Value of sine"
puts Math.sin(1)
```

**Output:**

#### 19. sinh

This function is used to calculate the hyperbolic sine of the given value which is expressed in radian form. Return type of this function is float.

**Code:**

```
puts "Value of hyperbolic sine"
puts Math.sinh(1)
```

**Output:**

#### 20. cos

This function is used to calculate the cosine of the given value which is expressed in radian form. Return type of this function is float and ranges from -1.0 to +1.0

**Code:**

```
puts "Value of cosine"
puts Math::cos(0)
```

**Output:**

#### 21. cosh

This function is used to calculate the hyperbolic cosine of the given value which is expressed in radian form. Return type of this function is float.

**Code:**

```
puts "Value of hyperbolic cosine"
puts Math::cosh(1)
```

**Output:**

#### 22. tan

This function is used to calculate the tangent of the given value which is expressed in radian form. Return type of this function is float.

**Code:**

```
puts "Value of tangent"
puts Math.tan(1)
```

**Output:**

#### 23. tanh

This function is used to calculate the hyperbolic tangent of the given value which is expressed in radian form. Return type of this function is float.

**Code:**

```
puts "Value of hyperbolic tangent"
puts Math.tanh(1)
```

**Output:**

### Recommended Articles

This is a guide to Ruby Math. Here we also discuss the Introduction and functions of ruby math along with different examples and its code implementation. You may also have a look at the following articles to learn more –