# Finding Absolute Value For Specified 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 absolute value of the specified complex number with the help of Abs() 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 Abs() function.

Syntax:

func Abs(a complex128) float64

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

Example 1:

 // Golang program to illustrate// how to find absolute valuepackage main  import (    "fmt"    "math/cmplx")  // Main functionfunc main() {      // Finding absolute value of    // the specified complex number    // Using Abs() function    res_1 := cmplx.Abs(3 + 5i)    res_2 := cmplx.Abs(-4 + 8i)    res_3 := cmplx.Abs(-8 - 7i)      // Displaying the result    fmt.Println("Random Number 1:", res_1)    fmt.Println("Random Number 2: ", res_2)    fmt.Println("Random Number 3: ", res_3)}

Output:

Random Number 1: 5.8309518948453
Random Number 2:  8.94427190999916
Random Number 3:  10.63014581273465

Example 2:

 // Golang program to illustrate how// to find absolute valuepackage main  import (    "fmt"    "math/cmplx")  // Main functionfunc main() {      // Complex numbers    cnumber_1 := complex(5, 7)    cnumber_2 := complex(6, 9)      // Finding absolute values    absvalue_1 := cmplx.Abs(cnumber_1)    absvalue_2 := cmplx.Abs(cnumber_2)      // Sum of two absolute values    res := absvalue_1 + absvalue_2      // Displaying results    fmt.Println("Complex Number 1: ", cnumber_1)    fmt.Println("Complex Number 2: ", cnumber_2)    fmt.Println("Sum of the absolute values of "+                   "the given complex numbers: ")      fmt.Printf("%.1f + %.1f = %.1f", absvalue_1, absvalue_2, res)  }

Output:

Complex Number 1:  (5+7i)
Complex Number 2:  (6+9i)
Sum of the absolute values of the given complex numbers:
8.6 + 10.8 = 19.4

My Personal Notes arrow_drop_up