In Ruby, cos()
is a method in CMath
class which is used to find the cosine of any given complex number and this number must be in radians. To find out cosine of any given complex number CMath.cos()
method is used.
cos()
Method in Ruby
cos()
method exists in Standard CMath Library of Ruby. The purpose of this method is to provide the cosine of any given complex number and this number must be in radians.
Syntax of the cos()
Method in Ruby
Syntax of
cos()
method is:
cmath.cos(x)
Parameter of cos()
Method in Ruby
Parameter | Description |
x |
Any valid complex number. This parameter is required. |
Note: If the x
parameter is not a complex number, cos()
method will return an error
.
Return Value of cos()
cos()
method will return the cosine of any given complex number and this number must be in radians.
Ruby – Get the cos()
of the Given Numbers
# import CMath library require 'cmath' x = CMath.cos(5 + 5i) puts x
Output
21.050556181654844+71.1552598809822i
Ruby – Various Ways of Calculation in cos()
Method
# import CMath library require 'cmath' x = CMath.cos(-10 + 5i) puts x
Output
-1.490839765215787+0.03976617365742172i
Ruby – Various Ways of Calculation in cos()
Method
# import CMath library require 'cmath' x = CMath.cos(10 + 0i) puts x
Output
-0.8390715290764524+0.0i
Ruby – Non-Complex Nubmer in cos()
Method Produces Error
# import CMath library require 'cmath' x = CMath.cos(a10 - b5i) puts x
Output
Error
Ruby – Get the cos()
of Negative Number
# import CMath library require 'cmath' x = CMath.cos(-10) puts x
Output
-0.8390715290764524