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