Skip to content
Related Articles

Related Articles

Improve Article

Finding the Cosine of Complex Number in Golang

  • Last Updated : 27 Mar, 2020

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 cosine of the specified complex number with the help of the Cos() 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 Cos() function.

Syntax:

func Cos(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 cosine value of the given complex number
  
package main
  
import (
    "fmt"
    "math/cmplx"
)
  
// Main function
func main() {
  
    // Finding Cosine of the 
    // specified complex number
    // Using Cos() function
    res_1 := cmplx.Cos(2 + 5i)
    res_2 := cmplx.Cos(-1 + 8i)
    res_3 := cmplx.Cos(-1 - 7i)
  
    // Displaying the result
    fmt.Println("Result 1:", res_1)
    fmt.Println("Result 2:", res_2)
    fmt.Println("Result 3:", res_3)
}

Output:

Result 1: (-30.88223531891674-67.47278844058751i)
Result 2: (805.3093276729627+1254.19468537245i)
Result 3: (296.2569584411429-461.3921082367867i)

Example 2:




// Golang program to illustrate how to find
// the cosine value of the given complex number
  
package main
  
import (
    "fmt"
    "math/cmplx"
)
  
// Main function
func main() {
  
    cnumber_1 := complex(1, 2)
    cnumber_2 := complex(3, 6)
  
    // Finding cosine
    cvalue_1 := cmplx.Cos(cnumber_1)
    cvalue_2 := cmplx.Cos(cnumber_2)
  
    // Sum of two cosine values
    res := cvalue_1 + cvalue_2
  
    // Displaying results
    fmt.Println("Complex Number 1: ", cnumber_1)
    fmt.Println("Cosine 1: ", cvalue_1)
  
    fmt.Println("Complex Number 2: ", cnumber_2)
    fmt.Println("Cosine 2: ", cvalue_2)
    fmt.Println("Sum : ", res)
  
}

Output:

Complex Number 1:  (1+2i)
Cosine 1:  (2.0327230070196656-3.0518977991518i)
Complex Number 2:  (3+6i)
Cosine 2:  (-199.6969662082171-28.465762393875067i)
Sum :  (-197.66424320119745-31.517660193026867i)



My Personal Notes arrow_drop_up
Recommended Articles
Page :