Learn and Learn

Learn And Learn - great place for tutorials, references and how-to

How to use BigMath Class and its Methods In Ruby

In Ruby BigMath provides perfect mathematical methods. To use BigMath in Ruby you can use BigMath().

BigMath() method in Ruby

BigMath() method exists in Standard BigDecimal Library of Ruby. The purpose of this method is to provide perfect mathematical methods. Provided that, BigMath also helpful in getting the values of PI, sin, cos, sqrt and atan.

For example

Output

0.62831853071795864.....e0

Similarly, if you need to take the value of cos from the above code you can do it like this:


Output:

0.80901699437494742410....e0

 

Return Value of BigMath() in Ruby

BigMath() method will return perfect mathematics calculations for the given values.

 

BigMath – exp in BigDecimal

This method Computes/calculates the value of e (base of natural logarithms) that will raise to the power of a decimal, to the indicated number of digits of precision.Note: If a decimal is an infinity, then it will return Infinity. And If a decimal is NaN (not a number), then it will return NaN.

 

BigMath – log in BigDecimal

This method Computes/calculates the natural logarithm of a decimal to the indicated number of digits of precision, which will be numeric.Note: If a decimal is zero or negative, then it will produce Math::DomainError.
If a decimal is a positive infinity, then it will return Infinity. and
If a decimal is a NaN (not a number), then it will return NaN.

 

BigMath – E in BigDecimal

The E method computes/calculates E (the base of natural logarithms) to the indicated number of digits of precision, it must be a numeric.

For example

Output:
0.27183e1

NOTE: In the above code, indicated number 5 shows that there will be 5 digits right to the decimal point. See another example how to add two precision values.

 


Output:

0.27183e10.2718281828e1

 

BigMath – Pi in BigDecimal

Pi computes/calculates the value of pi to the indicated number of digits of precision and it must be a numeric.

For example

Output:

"0.31415926535897932384627534923029509162e1”

 

BigMath – atan in BigDecimal

atan computes/calculates arctangent of the decimal to the indicated number of digits of precision and it must be a numeric.

For example

Output:

0.1373400766945015860861271e1

In the above code, decimal value is 5 which is computing with the indicated number 10.

 

BigMath – cos in BigDecimal

cos computes/calculates cosine of the decimal to the indicated number of digits of precision and it must be a numeric.

For example

Output:

"-0.10000000000000000000000004820e1"

In the above code, decimal value is 5 which is computing with the indicated number 10.

 

BigMath – sin in BigDecimal

sin computes/calculates sine of the decimal to the indicated number of digits of precision and it must be a numeric.

For example

Output:

"-0.1101151826389346196115519273961392806590184e-21"

In the above code, decimal value is 5 which is computing with the indicated number 10.

 

BigMath – sqrt in BigDecimal

sqrt computes/calculates square root of the decimal to the indicated number of digits of precision and it must be a numeric.

For example

Output:

"0.2236067977499789696e1"

In the above code, decimal value is 5 which is computing with the indicated number 10.

 

Float – to_d in BigDecimal

The method of to_d returns the given value of float as a BigDecicmal. The precision parameter in the code is used to control the number of significant digits to the right of decimal point. (the default is Float::DIG).

For example

In the above code, the optional precision parameter like (2) and (4) means that in the result there will be 4 digits after the decimal point.

 

Integer – to_d in BigDecimal

The method of to_d in integer returns the given value of int as a BigDecicmal.

For example

 

Jacobian in BigDecimal

Jacobian method Provides help to compute/calculate the Jacobian matrix of a set of equations at a point x. In the methods below:
Suppose f is an Object, which is used to compute/calculate the Jacobian matrix of the equations. The following methods must be provided to it:

 

f.values(x)
It returns the numeric values of all methods at x.f.zero
It returns value 0.0f.one
It returns value 1.0f.two
It returns value 2.0

f.ten
It returns value 10.0

f.eps
This method shows the merging criterion (epsilon value) used to control whether two values are considered equal or not because If |b-c| < epsilon, then the two particular values are considered equal.

Note: x is the point at which you compute to the Jacobian.
fx is equal to f.values(x).

 

Rational – to_d in BigDecimal

The rational method will return the value as a BigDecimal
The required precision parameter is used to control the number of important digits for the result.

For example

Output:

0.503e2

The digit after to_d is determined the digit after the decimal point.

 

String – to_d in BigDecimal

The string to_d method returns the major character in the code in the form of a string as a BigDecimal.

For example

 

Loading...
LearnAndLearn.com © 2018 - All rights reserved