Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang

What is type float32 in Golang?

Faraz Karim

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.

In the Go programming language, variables are containers marked by identifiers or names, which can hold different values in a program. These variables can hold many 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 used to store decimal numbers. float32 is a version of float that stores decimal values composed of 32 bits of data.

For example, if you try to store a string value in it, or a float beyond what can be made using 32 bits, the program would either return an error, store a rounded-off value of the original (this is called truncation), or result in an overflow.

Similar variants

The numeric data type of float has one other version besides float32, called float 64.

Range

A variable of type float32 can store decimal numbers ranging from 1.2E-38 to 3.4E+38. This is the range of magnitudes that a float32 variable can store. This applies to negative and positive numbers.

If a float32 variable is assigned a value beyond the range mentioned above, then an overflow error occurs, which basically means that the float32 variable cannot properly store the assigned number.

Overflow

The following code shows what happens if you try to store something larger than the stipulated range of values in a float32 variable:

package main
import "fmt"
func main() {
// initializing with the maximum allowed negative decimal value
var num float32 = -3.4E+38
// printing the value and data type
fmt.Printf("Value is: %f and type is: %T\n", num, num);
// making the value out of range by multiply by +/-10
var num_n float32 = num*10
var num_p float32 = num*-10
// printing out new value and type
fmt.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 -3.4E+38 in num and printed it, the value came out to be as 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. This is called a positive overflow.

Examples

In the following example, we declare a variable num, explicitly stating its data type to be float32. Later, we can use the Printf function to see that num is indeed stored as a float32 data type:

package main
import "fmt"
func main() {
var num float32
num = 200.0
fmt.Printf("Data type of %f is %T\n", num, num);
}

It is also possible to create const values of type float32 instead of variables. The only difference is that const values are just variables whose values can not be changed from what they were 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 main
import "fmt"
func main() {
// declaring and initializing a const value with an float
const c_num float32 = 200.0
// %T represents the type of the variable num
fmt.Printf("Data type of %f is %T\n", c_num, c_num);
}

RELATED TAGS

golang

CONTRIBUTOR

Faraz Karim
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.

Keep Exploring