Skip to content
Related Articles

Related Articles

Finding the Natural Logarithm of Complex Number in Golang
  • Last Updated : 01 Apr, 2020
GeeksforGeeks - Summer Carnival Banner

Go language provides inbuilt support for basic constants and mathematical functions for complex numbers with the help of the cmplx package. You are allowed to find the natural logarithm of the specified complex number with the help of Log() function provided by the math/cmplx package. So, you need to add a math/cmplx package in your program with the help of the import keyword to access the Log() function.

Syntax:

func Log(y complex128) complex128

Let us discuss this concept with the help of the given examples:

Example 1:




// Golang program to illustrate how to find the
// natural logarithm of the given complex number
  
package main
  
import (
    "fmt"
    "math/cmplx"
)
  
// Main function
func main() {
  
    // Finding natural logarithm of
    // the specified complex number
    // Using Log() function
    res_1 := cmplx.Log(1i)
    res_2 := cmplx.Log(-4 + 12i)
    res_3 := cmplx.Log(-3 - 9i)
  
    // Displaying the result
    fmt.Printf("Result 1: %.1f", res_1)
    fmt.Printf("\nResult 2:  %.1f", res_2)
    fmt.Printf("\nResult 3:  %.1f", res_3)
}

Output:



Result 1: (0.0+1.6i)
Result 2:  (2.5+1.9i)
Result 3:  (2.2-1.9i)

Example 2:




// Golang program to illustrate how to find the
// natural logarithm of the given complex number
  
package main
  
import (
    "fmt"
    "math/cmplx"
)
  
// Main function
func main() {
  
    cnumber_1 := complex(0, 2)
    cnumber_2 := complex(4, 6)
  
    // Finding natural logarithm
    cvalue_1 := cmplx.Log(cnumber_1)
    cvalue_2 := cmplx.Log(cnumber_2)
  
    // Sum of the given natural logarithm
    res := cvalue_1 + cvalue_2
  
    // Displaying results
    fmt.Println("Complex Number 1: ", cnumber_1)
    fmt.Printf("Natural logarithm 1: %.1f", cvalue_1)
  
    fmt.Println("\nComplex Number 2: ", cnumber_2)
    fmt.Printf("Natural logarithm 2: %.1f ", cvalue_2)
    fmt.Printf("\nSum : %.1f", res)
  
}

Output:

Complex Number 1:  (0+2i)
Natural logarithm 1: (0.7+1.6i)
Complex Number 2:  (4+6i)
Natural logarithm 2: (2.0+1.0i) 
Sum : (2.7+2.6i)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :