The imag function in Golang is a built-in function that extracts the imaginary part of a given complex number.
The process is illustrated below:
The prototype of the imag function is shown below:
func imag(c ComplexType) FloatType
The imag function takes a single mandatory parameter in the form of a ComplexType object.
The imag function returns a FloatType object that represents the imaginary part of the complex number provided as a parameter.
If the given complex number is a complex64 object, then the imag function returns a float32 object. If the given complex number is a complex128 object, then the imag function returns a float64 object.
The code below shows how the imag function works in Golang:
package mainimport ("fmt")func main() {// initializing complex valuesa := complex(15, 2)b := complex (-5, 4)// extracting imaginary partsfmt.Println("The imaginary part of", a , "is", imag(a))fmt.Println("The imaginary part of", b , "is", imag(b))}
The complex function in lines and initializes a complex object based on the values provided for the real and imaginary parts.
The imag function in lines and proceeds to extract the imaginary part of each complex object, i.e., and , respectively. The extracted imaginary values are then output.
Free Resources