Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang

What is type complex128 in Golang?

Talha Ashar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

The type complex128 in Golang is the set of all complex numbers with float64 real and imaginary parts.

Complex numbers consist of a real and an imaginary part, as illustrated below:

Real and Imaginary Parts of a Complex Number

To create a complex128 number in Golang, you can use the in-built complex function. The prototype of the complex function is shown below:

func complex(real, imaginary FloatType) ComplexType

If you provide the complex function with arguments of type float64, it returns a type complex128 value.

An alternative method of creating a complex number is as follows:

temp := 1 + 4i 

The declaration shown above creates a complex number, with 11 as the real part and 4i4i as the imaginary part.

Operations

The complex128 type supports the usual complex number operations, e.g., addition, subtraction, multiplication, division, etc.

Example

The code below shows how the complex128 type works in Golang:

package main
import (
"fmt"
)
func main() {
// initializing real and imaginary parts
var real float64 = 5
var img float64 = 10
// intiializing complex numbers
complex1 := complex(real, img)
complex2 := 2 + 4i
// performing arithmetic operations
fmt.Println("Addition: ", complex1, " + ", complex2, " = ", complex1 + complex2)
fmt.Println("Subtraction: ", complex1, " - ", complex2, " = ", complex1 - complex2)
fmt.Println("Multiplication: ", complex1, " * ", complex2, " = ", complex1 * complex2)
fmt.Println("Division: ", complex1, " / ", complex2, " = ", complex1 / complex2)
}

Explanation

First, the code initializes two complex128 values: complex1 and complex2. complex1 is initialized using the in-built complex function by providing it with two float64 values. complex2 is initialized using the Golang shorthand assignment operator.

Lines 182118-21 perform the arithmetic operations on the complex128 values and output the results accordingly. These arithmetic operations follow the mathematical rules defined for complex numbers.

RELATED TAGS

golang

CONTRIBUTOR

Talha Ashar
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring