×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang real() Function with Examples

Golang | real() Function: Here, we are going to learn about the built-in real() function with its usages, syntax, and examples.
Submitted by IncludeHelp, on October 20, 2021 [Last updated : March 15, 2023]

real() Function

In the Go programming language, the real() is a built-in function that is used to get the real part of the given complex number. The return value will be floating point type corresponding to the type of the given complex number.

It accepts one parameter (c ComplexType) and returns the real part of the c.

Syntax

func real(c ComplexType) FloatType

Parameter(s)

  • c : The complex number of ComplexType.

Return Value

The return type of the real() function is FloatType, it returns the real part of the given complex number.

Example 1

// Golang program to demonstrate the
// example of real() 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 real part and
	// printing its type and value
	real_x := real(x)
	real_y := real(y)

	fmt.Printf("real_x: %T, %v\n", real_x, real_x)
	fmt.Printf("real_y: %T, %v\n", real_y, real_y)
}

Output

x: complex128, (1+2i)
y: complex128, (3+4i)
real_x: float64, 1
real_y: float64, 3

Example 2

// Golang program to demonstrate the
// example of real() 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("Real Number of complex1:", real(complex1))
	fmt.Println("Real Number of complex2:", real(complex2))
	fmt.Println("Real Number of complex1*complex2:", real(complex1*complex2))
}

Output

complex1 (2+10i)
complex2: (2+6i)
complex1*complex2 (-56+32i)
Real Number of complex1: 2
Real Number of complex2: 2
Real Number of complex1*complex2: -56

Golang builtin Package »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.