Finding the Decimal Logarithm of Complex Number in Golang


In mathematics, a complex number is a number that comprises a real and imaginary part. A decimal logarithm is a logarithmic function with base 10. In Golang, we can use the "math/cmplx" package to find the decimal logarithm of a complex number.

Syntax

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

func Log10(z complex128) complex128

Here, "z" is the complex number for which we want to find the decimal logarithm.

Example 1

Let's say we have a complex number (5+12i), and we want to find its decimal logarithm. We can do this using the following code −

package main

import (
   "fmt"
   "math/cmplx"
)

func main() {
   z := complex(5, 12)
   log := cmplx.Log10(z)
   fmt.Printf("log10(%v) = %v\n", z, log)
}

Output

log10((5+12i)) = (1.1139433523068367+0.5107325721309081i)

We first create a complex number z with the value (5+12i). We then use the Log10() function from the cmplx package to find its decimal logarithm. Finally, we print the result using fmt.Printf().

Example 2

Let's say we have a complex number (-2+4i), and we want to find its decimal logarithm. We can do this using the following code −

package main

import (
   "fmt"
   "math/cmplx"
)

func main() {
   z := complex(-2, 4)
   log := cmplx.Log10(z)
   fmt.Printf("log10(%v) = %v\n", z, log)
}

Output

log10((-2+4i)) = (0.6505149978319906+0.8835477750576072i)

We first create a complex number z with the value (-2+4i). We then use the Log10() function from the cmplx package to find its decimal logarithm. Finally, we print the result using fmt.Printf().

Example 3

Let's say we have a complex number (0+1i), and we want to find its decimal logarithm. We can do this using the following code −

package main

import (
   "fmt"
   "math/cmplx"
)

func main() {
   z := complex(0, 1)
   log := cmplx.Log10(z)
   fmt.Printf("log10(%v) = %v\n", z, log)
}

Output

log10((0+1i)) = (0+0.6821881769209206i)

We first create a complex number z with the value (0+1i). We then use the Log10() function from the cmplx package to find its decimal logarithm. Finally, we print the result using fmt.Printf().

Conclusion

In this article, we have learned how to find the decimal logarithm of a complex number in Golang using the "math/cmplx" package. We have seen three examples of how to use this function to find the decimal logarithm of different complex numbers. The Log10() function takes a complex number as input and returns a complex number as output.

Updated on: 12-Apr-2023

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements