Floating point number Data Types in GoLang
Floating point number:
For representing the real numbers in go language, floating-point number or float is used. Floating-point numbers are further categorized into two parts in the go language. These parts are given below in the table:
Data Type | Details |
float32 | It is a 32-bit IEEE 754 floating-point number. |
float64 | It is a 64-bit IEEE 754 floating-point number. |
Float32:
Variable declared in float32 data type will occupy 32 bits of memory from the storage. It stores the value in single-precision floating-point format.
Float64:
Variable declared in float64 data type will occupy 64 bits of memory from the storage. It stores the value in double-precision floating-point format.
Note:
When you declare a float variable in the go program by using the short-hand declaration method without specifying the type of variable, your variable’s type will be float64.
For example:
num := 22.23
OR
var num = 22.34
In both cases of variable declaration, by default variable ‘num' will be assigned as float64 number means a type of variable 'num’ will be float64.
Program:
Here is a go program in which the default type of float variable is defined. The program is given below:
package main // program of floating-point numbers. import "fmt" func main() { fmt.Println("default data type of float numbers \n ") /* default type of float variables, if they declared without assigning their data type. */ var enum1 = 12.23 enum2 := 23.34 // variable declared. fmt.Printf(" The entrusted value to the variable (enum1) is : %f \n ", enum1) fmt.Printf("The determinates value to the variable (enum2) is: %f \n ", enum2) fmt.Printf("Type of the value that is determinates to the variable enum1 is (by default) : %T \n ", enum1) fmt.Printf("Type of the value that is determinates to the variable enum2 is (by default) : %T \n ", enum2) }
Output:
When you run your go file on your command prompt; containing the same code as given in the above portion, then it will produce the output as below:
Difference between float32 and float64 data type:
Float32 and float64 both represent the floating-point variable in go language. If you provide a large float number in the go program, then both (float32 and float64) type of variables will take up to 6 digits from the decimal point. For example:
If var num = 78.0123456789
The compiler will then take up to 6 digits in Go language, and it will be 78.012345; the remaining digits will be ignored.
Although float32 and float64 both represent float value and look the same in go language, there are some differences between them.
Float32 | Float64 |
The variable occupies 32-bit memory in the program. | The variable occupies 64-bit memory in the program. |
It uses the ‘float32’ keyword to define a variable in the program. | It uses the ‘float64’ keyword to define a variable in the program. |
It stores the value in a single-precision format. | It stores the value in double precision format. |
To know more about the differences between float32 and float64, let’s take a look at a go program.
Program:
Here is a program which will clarify the differences between float32 and float64 type variable in go language. The program is given below:
package main // program to give the difference between float32 and float64 type number. import "fmt" func main() { fmt.Println(" 'float32 type and float64 type variable's disparity' \n ") /* in float32, the decimal value of variable got increment by .1 (point 1) if 6th digit after decimal is equal to 2 or greater than 2 and post value is equal or greater than 5. */ var enum1 float32 = 12.123452689 // here, value increased. fmt.Printf(" Entrusted value to the variable (enum1) is : %f \n ", enum1) /* in float64, the decimal value of variable got increment by .1 (point 1) if post value from 6th digit after decimal is greater than 5. */ var enum2 float64 = 23.123450689 // here, value increased. fmt.Printf("Determinates value to the variable (enum2) is : %f \n \n ", enum2) // declared value will not change in below statement. var count1 float32 = 12.1234503789 // value will not change. fmt.Printf("The entrusted value to the variable (count1) is : %f \n ", count1) var count2 float64 = 23.1234504789 // value will not change. fmt.Printf("The entrusted value to the variable (count2) is : %f \n ", count2) }
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: