Menu Close

Go – How to get real part of the complex number in go

In this tutorial, We are going to learn about how to get real part of the complex number in go. We can do it by using real() built-in function in go.

real() built-in function returns the real part of the complex number.

Function prototype:

func real(c ComplexType) FloatType

Input parameter:

c: Complex number

Return type:

real() built-in function returns 
the real part of the complex number of 
type float32.

Example:

Input: 3 + 2i
output: 3

Input: 5 + 7i
output: 5

Example with code:

package main

import (
  "fmt"
)

func main() {

  complex_num := 3 + 2i

  y := real(complex_num)

  fmt.Println("Real num: ", y)


  var num complex64

  var x float32

  num = 5 + 7i

  x = real(num)

  fmt.Println("Real num: ", x)
}

Output:

Real num: 3

Real num: 5

To learn more about golang, You can refer given below link:

https://www.techieindoor.com/go-lang-tutorial/

References:

 https://golang.org/doc/
https://golang.org/pkg/
https://golang.org/pkg/fmt/
https://golang.org/pkg/fmt/#Println
Posted in builtin, golang

Leave a Reply

Your email address will not be published. Required fields are marked *