# Complex Number Data Types in GoLang

### Complex Number:

A complex number is a basic data type of GoLang. It is a combination of the real and imaginary part, where both parts of the complex number will be float type in the go program. Complex numbers are further categorized into two parts in go language. These two parts are:

1. Complex64
2. Complex128

Complex64:

Expression declared in complex64 data type will occupy 64 bits memory from the storage in go program. Both the real and imaginary parts of the expression will be float32 type.

Complex128:

Expression declared in complex128 data type will occupy 128 bits memory from the storage in go program. Both the real and imaginary parts of the expression will be a float64 type.

#### Note:

If the developer declared a complex variable without using data type in the go program's variable declaration expression, then by default, the type of complex variable will be complex128.

For example:

`            var exp = 12 + 23i`

OR

`            exp := 23 + 34i`

Here, in both cases of variable declaration, by default variable 'exp' will be assigned as complex128 number means a type of variable 'exp’ will be complex128.

#### Program:

Here is a go program in which the default type of complex variable is defined. The program is given below:

``` package main
// program to find default data type of complex numbers.
import "fmt"
func main() {
fmt.Println("default data type of complex numbers \n ")
/* default type of complex variables,
if they declared without assigning their data type. */
var exp1 = 10 + 11i
exp2 := 12 + 23i                    // variable declared.
fmt.Printf(" The assigned value is : %v \n ", exp1)
fmt.Printf("The entrusted value to the variable (exp2) is : %g \n ", exp2)
fmt.Printf("Type of the value that is determinated to the variable exp1 is (by default) : %T \n ", exp1)
fmt.Printf("The assigned value's type is (by default) : %T \n ", exp2)
} ```

#### Output:

When you run your go file on your command prompt; containing the same code as given in the above portion, then it will generate the output as below:

#### 'Complex' function:

There is a function available in the go language called complex function, which is used to create the complex numbers. It is a built-in function of the go language. If the developer creates a complex number in the go program with the help of variables instead of literals, he can create them easily by using the 'complex' function.

##### Rules for using the complex function:

There are some available rules also for using the 'complex' function in the go program. These rules are given below:

Rule no. 1:

The go program's declared variable must be the same type means both real and imaginary parts should not be of a different type. Both parts of the expression must be the same; if it is not, the program will give a compile-time error. Even float32 and float64 types are not allowed in the declaration. Both the real and imaginary parts should be either float32 or float 64 types.

Program:

``` package main
// Program for using complex function with different type variables.
import "fmt"
func main() {
/* Here we are going to declare two variables
having different type in float */
var count1 float32 = 12.12
var count2 float64 = 23.23              // variable declared.
/* when we make complex number by using these
two variables then it will give compile time error.*/
var exp = complex(count1, count2)
// above command will generate error, because numbers are different type.
fmt.Println(" The resultant complex number is :  ", exp)
// for successful execution, use same type float variables.
} ```

Output:

When you run your go file on your command prompt containing the same code as given in the above portion, then it will generate the output as below:

Rule no. 2:

If you are creating a complex number with the help of the 'complex' function, there is no need for the character 'i' in the declaration.

Program:

``` package main
// program for using complex function
import "fmt"
func main() {
var num1 float64 = 12
var num2 float64 = 23                     // variable declared.
// create a complex number with the help of num1 and num2 variable.
// var num3 = num1 + num2i
/* do not use above statement for creating the complex number,
if you will use then it will give compile time error. */
// instead of using charecter 'i'; use 'complex' function to create complex number.
var num3 = complex(num1, num2)            // here, complex number created.
fmt.Println(" The resultant value is :  ", num3)
} ```

Output:

When you run your go file on your command prompt; containing the same code as given in the above portion, then it will generate the output as below:

Rule no. 3:

The developer can create a complex number by using float-type numbers only. Even if he declares an integer value in a complex number, the compiler will consider it a float number.

Program:

Here is a program is given below, which contains integer type variables:

``` package main
// program of creating a complex number by using an integer type variable.
import "fmt"
func main() {
var num1 = 45                       // by default these variables are integer type.
var num2 = 56                       // variable declared.
var exp = complex(num1, num2)                // use complex function.
/* above command will generate compile time error
because variables used for making complex number are in integer type. */
fmt.Println(" The resultant complex number is :  ", exp)
// for successful execution, use same type float variables.
} ```

Output:

When you run your go file on your command prompt; containing the same code as given in the above part, then it will generate the output as below:

### Operations on complex numbers:

You can perform any operations on the complex numbers in the go language like addition, subtraction, multiplication, division, and many more, but there is one condition. The condition is that both complex numbers on which operation is going to perform; must be the same type, either complex64 or complex128 type. If the first complex number's type is complex64 and the second complex number's type is complex128, you can't perform operations like addition, subtraction, etc. When you practice the complex numbers, it will be fully clear how operations perform on complex numbers.

Program:

Here is a program, which will give you more clarification on the operations based on complex numbers. The program is given below:

``` package main
// operations that can be performed on complex variable
import "fmt"
func main() {
// float variable declaration
var num1 float64 = 2.5
var num2 float64 = 5.6
var num3 float32 = 4.5
var num4 float32 = 7.3        // float variable declaration completed.
// complex variable declaration --- type 1
exp1 := 2 + 4i
// complex variable declaration --- type 2
var exp2 = complex(3, 7)
// complex variable declaration --- type 3
var exp3 = complex(num1, num2)
var exp4 = complex(num3, num4)              // complex variable declaration completed.
/* here, exp4 variable is complex64 type expression.
Because num3 and num4 are float32 type variable and 32 bit variable
makes complex64 type variable by joining each other. */
fmt.Printf(" first complex number : %T \n second complex number : %T \n", exp1, exp2)
fmt.Printf(" third complex number: %T \n fourth complex number : %T \n", exp3, exp4 )
// addition of Complex numbers having different type variable.
/* var add_res = exp2 + exp4
above addition command will generate error (mismatch type of variable) at the time of compilation,
because exp2 is complex128 type and exp4 is complex64 type variable. for the successful execution
of above command, it requires type conversion of variable. either exp4 coverted into complex128 type
or exp2 coverted into complex64 type variable.
*/
// now, we are going to perform type conversion on variable exp4.
var exp5 = complex128(exp4)                     // type conversion performed.
fmt.Printf(" after type conversion, fourth complex number : %T \n", exp5)
// now addition of Complex numbers having same type variable.
var add_res = exp2 + exp5
// subtraction of Complex numbers.
var sub_res = exp1 - exp3
fmt.Println(" The subtraction of two complex variable is : ", sub_res)
// multiplication of complex numbers.
var mul_res = exp1 * exp2
fmt.Println(" The mutiplication of two complex variable is : ", mul_res)
// division of complex numbers.
var div_res = exp2 / exp1
fmt.Println(" The division of two complex variable is : ", div_res)
// modulas of complex numbers.
/* modulas operation can't be perfomed on complex numbers,
because complex numbers are float type variable.
var mod_res = exp5 % exp1
above statement will generate compile time errror i.e.
operator % is not defined for complex128 type variables.
fmt.Println(" The modulas of two complex variable is : ", mod_res)
*/
} ```

Output:

When you run your go file on your command prompt; containing the same code as given in the above part, then it will give the output as below: