Home »
Golang »
Golang Reference
Golang imag() Function with Examples
Golang | imag() Function: Here, we are going to learn about the built-in imag() function with its usages, syntax, and examples.
Submitted by IncludeHelp, on October 17, 2021
imag() Function
In the Go programming language, the imag() is a built-in function that is used to get the imaginary part of the given complex number.
It accepts one parameter (c ComplexType) and returns the imaginary part of c.
Syntax:
func imag(c ComplexType) FloatType
Parameter(s):
- c : The complex value of a ComplexType.
Return Value:
The return type of the imag() function is a FloatType, it returns the imaginary part of the given complex value and the return value will be floating point type corresponding to the type of the given complex value.
Example 1:
// Golang program to demonstrate the
// example of imag() function
package main
import (
"fmt"
)
func main() {
// Declare and assign complex numbers
var x complex128 = complex(1, 2)
var y complex128 = complex(3, 4)
// Printing the types and values
fmt.Printf("x: %T, %v\n", x, x)
fmt.Printf("y: %T, %v\n", y, y)
// Extracting the imaginary part and
// printing its type and value
imag_x := imag(x)
imag_y := imag(y)
fmt.Printf("imag_x: %T, %v\n", imag_x, imag_x)
fmt.Printf("imag_y: %T, %v\n", imag_y, imag_y)
}
Output:
x: complex128, (1+2i)
y: complex128, (3+4i)
imag_x: float64, 2
imag_y: float64, 4
Example 2:
// Golang program to demonstrate the
// example of imag() function
package main
import (
"fmt"
)
func main() {
complex1 := complex(2, 10)
complex2 := 2 + 6i
fmt.Println("complex1", complex1)
fmt.Println("complex2:", complex2)
fmt.Println("complex1*complex2", complex1*complex2)
fmt.Println("Imaginary Number of complex1:", imag(complex1))
fmt.Println("Imaginary Number of complex2:", imag(complex2))
fmt.Println("Imaginary Number of complex1*complex2:", imag(complex1*complex2))
}
Output:
complex1 (2+10i)
complex2: (2+6i)
complex1*complex2 (-56+32i)
Imaginary Number of complex1: 10
Imaginary Number of complex2: 6
Imaginary Number of complex1*complex2: 32
Golang builtin Package »