Monday, December 24, 2018

Golang Tutorials - Complex Types Numbers Guide With Examples

In this blog post, You will learn about Complex Numbers types in Go language with examples.

Golang Complex Type Numbers

Golang provides Inbuilt datatypes for numeric strings and other types Numeric strings are int, uint, float, and complex numbers. Complex Numbers contains two parts - real numbers and imaginary numbers, for example, a complex number is 12.8i
In Golang, Complex numbers are represented in two types
complex64 type- this type is used to represent float32 real and imaginary data.
complex128 type - this type is used to represent float64 real and imaginary data

How do we create a Complex number in Golang? 

There are 2 ways to create a Complex number of ways in Golang.
Using Inbuilt function -
    complex Golang Provides Inbuilt Complex function, provides to create real and imaginary data.
Here is a complex function syntax
func complex(real, imaginary FloatType) ComplexType  
This takes real and imaginary floating types - float 32 or float 64. Complex type returns Complex type complex64 and complex128 types These both parameters types should be always same. if real and imaginary numbers of float32 is supplied, retuned Complex32 type, if real and imaginary types are of type float64 is supplied, the function always returns Complex64 type
Using Shorthand assignment Syntax
This is another way to create Complex numbers shorthand assignment syntax
complexData := 1 + 4i  

Golang Complex Number Type example

In the below example, You will learn the following things.
  • Create a Complex number using Inbuilt Complex function
  • Declare and assign Complex Number Variable with shorthand syntax Printing Complex numbers using Println function 
  • Print Imaginary Data part using imag function 
  • Print real Data part using a real function

package main

import (
 "fmt"
)

func main() {
 complex1 := complex(10, 4)
 complex2 := 1 + 4i
 fmt.Println("complex Number 1", complex1)
 fmt.Println("complex Number 2:", complex2)
 fmt.Println("Real Number 1:", real(complex1))
 fmt.Println("Imaginary Number 1:", imag(complex1))
 fmt.Println("Real Number 1:", real(complex2))
 fmt.Println("Imaginary Number 2:", imag(complex2))
}
When above program compiles, Output is
complex Number 1 (10+4i)
complex Number 2: (1+4i)
Real Number 1: 10
Imaginary Number 1: 4
Real Number 1: 10
Imaginary Number 2: 4

Example program - Arithmetic Operations with Complex Numbers 

This is an example for Having arithmetic operations - add, subtract, divide, Multiplication on Complex Numbers When you are doing add operation with complex numbers, Real part adds separately, Imaginary part adds separately. The same it works for other operations Below is an example for add, subtract, multiply, divide operations on complex numbers.
package main
import (
 "fmt"
)

func main() {
 complex1 := complex(10, 4)
 complex2 := 1 + 4i
 fmt.Println("Addition ", complex1+complex2)
 fmt.Println("Substraction:", complex1-complex2)
 fmt.Println("Division ", complex1/complex2)
 fmt.Println("Multiplication:", complex1*complex2)
}
Output is
Addition  (11+8i)
Substraction: (9+0i)
Division  (1.5294117647058822-2.1176470588235294i)
Multiplication: (-6+44i)

Related article

This Is The Newest Post


EmoticonEmoticon