# Finding the Natural Logarithm of Complex Number in Golang

In the field of mathematics and computer programming, natural logarithm is an important function that is used in many calculations. The natural logarithm of a number is the logarithm of that number to the base of e (Euler's number), where e is approximately equal to 2.71828. In Golang, the math/cmplx package provides various functions to perform complex number operations including the natural logarithm of a complex number.

In this article, we will discuss how to find the natural logarithm of a complex number in Golang with the help of examples.

## Syntax

The syntax for finding the natural logarithm of a complex number in Golang is −

func Log(z complex128) complex128


## Parameters

The Log() function of the math/cmplx package takes one argument which is a complex number of type complex128.

## Returns

The Log() function of the math/cmplx package takes one argument which is a complex number of type complex128.

## Example 1: Finding the Natural Logarithm of a Complex Number

Let's take a complex number z = 3 + 4i and find its natural logarithm.

package main

import (
"fmt"
"math/cmplx"
)

func main() {
// Creating a complex number
z := complex(3, 4)

// Finding the natural logarithm of the complex number
ln := cmplx.Log(z)

// Displaying the result
fmt.Println("Natural Logarithm of", z, "is", ln)
}


## Output

Natural Logarithm of (3+4i) is (1.6094379124341003+0.9272952180016122i)


## Example 2: Finding the Natural Logarithm of a Negative Complex Number

Let's take a negative complex number z = -5 + 12i and find its natural logarithm.

package main

import (
"fmt"
"math/cmplx"
)

func main() {
// Creating a complex number
z := complex(-5, 12)

// Finding the natural logarithm of the complex number
ln := cmplx.Log(z)

// Displaying the result
fmt.Println("Natural Logarithm of", z, "is", ln)
}


## Output

Natural Logarithm of (-5+12i) is (2.5649493574615367+1.965587446494658i)


## Example 3: Finding the Natural Logarithm of a Zero Complex Number

Let's take a zero complex number z = 0 + 0i and find its natural logarithm.

package main

import (
"fmt"
"math/cmplx"
)

func main() {
// Creating a complex number
z := complex(0, 0)

// Finding the natural logarithm of the complex number
ln := cmplx.Log(z)

// Displaying the result
fmt.Println("Natural Logarithm of", z, "is", ln)
}


## Output

Natural Logarithm of (0+0i) is (-Inf+0i)


## Example 4: Finding the Natural Logarithm of a Real Number

Let's take a real number z = 5 and find its natural logarithm.

package main

import (
"fmt"
"math"
)

func main() {
// Taking natural logarithm of a real number
x := 5.0
result := math.Log(x)

// Displaying the result
fmt.Printf("Natural logarithm of %v is %v", x, result)
}


## Output

Natural logarithm of 5 is 1.6094379124341003


## Conclusion

In this article, we learned how to find the natural logarithm of a complex number in Golang using the cmplx.Log function. We also looked at some examples that demonstrated the usage of this function.

The cmplx.Log function takes a complex number as an input and returns its natural logarithm. If the input is zero, the function returns -Inf. If the input is negative, the function returns NaN. The natural logarithm of a complex number is defined as the logarithm of its magnitude plus the imaginary unit times its argument.

We also looked at some examples that showed how to find the natural logarithm of complex numbers with different values. These examples included finding the natural logarithm of a purely imaginary number, a purely real number, a negative real number, and a complex number with both real and imaginary parts.

Updated on: 17-Apr-2023

105 Views