Learn and Learn

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

Ruby – Find Cosine – cos() Method – Examples & Explanation

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:

 

Parameter of cos() Method in Ruby

ParameterDescription
xAny 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


Output

21.050556181654844+71.1552598809822i

 

Ruby – Various Ways of Calculation in cos() Method


Output

-1.490839765215787+0.03976617365742172i

 

Ruby – Various Ways of Calculation in cos() Method


Output

-0.8390715290764524+0.0i

 

Ruby – Non-Complex Nubmer in cos() Method Produces Error


Output
Error

 

Ruby – Get the cos() of Negative Number


Output

-0.8390715290764524

 

LearnAndLearn.com © 2018 - All rights reserved