Ruby – Find Natural Logarithm – log() Method – Examples & Explanation

log() is a method in CMath class which is used to find the natural logarithm of a given complex number in Ruby language and if a second argument is given in the code, it will be the base of the logarithm.

log() Method in Ruby

log() method exists in Standard CMath Library of Ruby. The purpose of this method is to provide natural logarithm of a given complex number.

 

Syntax of the log() Method in Ruby

Syntax of log() method is:

cmath.log(x)

 

Parameter of log() Method in Ruby

Parameter Description
x Any valid complex number. This parameter is required.

Note: If the x parameter is not a complex number, log() method will return an error.

 

Return Value of log()

log() method will return natural logarithm of a given complex number.

 

Ruby log() Example 1

 # import CMath library 
 require 'cmath' 
 x = CMath.log(1 + 3i) 
 puts x

Output

1.151292546497023+1.2490457723982544i

 

Ruby log() Example 2

 # import CMath library
 require 'cmath'
 x = CMath.log(10 + 5i, 5)
 puts x 

Output

1.5000000000000002+0.2880804567972364i

 

Ruby log() Example 3

 # import CMath library 
 require 'cmath' 
 x = CMath.log(-10 + 0i, 5) 
 puts x 

Output

1.4306765580733933+1.9519812658311715i

 

Ruby log() Example 4

 # import CMath library 
 require 'cmath' 
 x = CMath.cosh(a10 - b5i, 5) 
 puts x 

Output

Error