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
# import math library require "bigdecimal/math" include BigMath a = BigDecimal((PI(100)/5).to_s) puts a
Output
0.62831853071795864.....e0
Similarly, if you need to take the value of cos from the above code you can do it like this:
# import math library require "bigdecimal/math" include BigMath a = BigDecimal((PI(100)/5).to_s) puts cos(a,100)
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
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
# import math library require "bigdecimal/math" include BigMath x = BigMath. E(5).to_s puts x
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.
# import math library require "bigdecimal/math" include BigMath x = BigMath.E(5).to_s y = BigMath.E(4).to_s z = x + y puts z
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
# import math library require "bigdecimal/math" include BigMath x = BigMath.PI(5).to_s puts x
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
# import math library require "bigdecimal/math" include BigMath x = BigMath.atan(BigDecimal.new('5'), 10).to_s puts x
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
# import math library require "bigdecimal/math" include BigMath x = BigMath.cos(BigMath.PI(5), 10).to_s puts x
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
# import math library require "bigdecimal/math" include BigMath x = BigMath.sin(BigMath.PI(5), 10).to_s puts x
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
# import math library require "bigdecimal/math" include BigMath x = BigMath.sqrt(BigDecimal.new('5'), 10).to_s puts x
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
#import begidecimal/util library require 'bigdecimal' require 'bigdecimal/util' x = 0.5.to_d # => 0.5e0 x = 1.234.to_d(2) # => 0.12e1 x = 2.432.to_d (4) #=> #> 0.2432e1
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
#import begidecimal/util library require 'bigdecimal' require 'bigdecimal/util' x = 192.to_d # => “0.192e3” x = -902.to_d # => "-0.902e3"
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.0
f.one
It returns value 1.0
f.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 required precision parameter is used to control the number of important digits for the result.
For example
#import begidecimal/util library require 'bigdecimal' require 'bigdecimal/util' x = Rational(151, 3).to_d(3) puts x
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
#import begidecimal/util library require 'bigdecimal' require 'bigdecimal/util' x = "1.7".to_d # => 0.17e1 x = "455.12e3".to_d # => 0.45512e6