In the Go programming language, variables are containers marked by identifiers
or names that can hold different values in a program.
These variables can all hold different data types, whether they are numbers, words, or any other type. To restrict the type of data stored inside these variables, we need to specify the data type of the variables.
float
is one of the available numeric data types in Go that is used to store decimal numbers. float64
is a version of float
that stores decimal values using a total of 64-bits of data.
For example, if you try to store a string
value in it, or a float
beyond what can be made using 64-bits, the program will either return an error or truncation
overflow
.
The numeric data type of float
has one other version besides float64
, float32
. Compared to its 32-bit version, float64
can hold much larger decimal values and at far greater precision.
A variable of type float64
can store decimal numbers ranging from 2.2E-308 to 1.7E+308. This applies to negative and positive numbers.
The following code shows what happens if you try to store something larger than the stipulated range of values in a float64
variable:
package mainimport "fmt"func main() {// initializing with the maximum allowed negative decimal valuevar num float64 = -1.7E+308// printing the value and data typefmt.Printf("Value is: %f and type is: %T\n", num, num);// making the value out of range by multiply by +/-10var num_n float64 = num*10var num_p float64 = num*-10// printing out new value and typefmt.Printf("Value is: %f and type is: %T\n", num_n, num_n);fmt.Printf("Value is: %f and type is: %T\n", num_p, num_p);}
As we can see from the outputs of the code above, when we stored the max magnitude of a negative number of -1.7E+308 in num
and printed it, the value came out to be what was stored.
However, when we multiplied it by 10, the value was pushed out of the allowed range, which resulted in the stored value being interpreted as negative infinity -Inf
when printed again.
When this happens, we call it a negative overflow
. Similarly, when multiplied by -10, the value was interpreted as positive infinity +Inf
, called a positive overflow
.
In the following example, we declare a variable num
and explicitly state its data type to be float64
. Later, we can use the Printf
function to see that num is indeed stored as a float64
data type:
package mainimport "fmt"func main() {var num float64num = 200.0fmt.Printf("Data type of %f is %T\n", num, num);}
It is also possible to create const
values of type float64
instead of variables. The only difference is that const
values are just variables whose values can not be changed from what it was initialized to.
We then check the stored data type again by printing it out using the Printf
function.
const
values must be declared and initialized in the same line.
package mainimport "fmt"func main() {// declaring and initializing a const value with an floatconst c_num float64 = 200.0// %T represents the type of the variable numfmt.Printf("Data type of %f is %T\n", c_num, c_num);}
Free Resources