Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang

# What is the Float32frombits function 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.

The programming language Go uses the Float32frombits  function to find the 32-bit floating-point number represented by an IEEE 754 binary number of 32 bits.

To use this function, you must import the math package in your file and access the Float32frombits  function within it using the . notation (math.Float32frombits). Here, Float32frombits is the actual function, while math is the Go package that stores the definition of this function.

## Function definition

The definition of the Float32frombits function inside the math package is:

## Parameters

Float32frombits function takes a single argument of type uint32. This argument represents the number whose IEEE 754 representation you want to find.

Since the input argument type is a 32-bit unsigned integer, the input values can range from 0 to 4294967295

## Return value

The Float32frombits function returns a single 32-bit floating-point number, represented by the IEEE 754 binary in the input argument.

The Float32frombits function returns an error if the input argument is left empty or if a NAN or undefined numeric value is passed as an argument.

## Examples

The following is a simple example where we find the number represented by the IEEE 754 representation 500:

package mainimport (    "fmt"    "math")func main() {  const x uint32 = 500.0  myfloat := math.Float32frombits(x)  fmt.Print("The floating point number represented by ", x, " is: " , myfloat) }

Even though the input values are unsigned (positive), the value is an unsigned integer. You can still generate negative floats by passing the IEEE 754 representation of negative numbers. The first bit of the binary representation of the input for Float32frombits is considered to be the sign bit when you interpret it as an IEEE 754 binary. The following example shows how you can generate the negative number -5:

package mainimport (    "fmt"    "math")func main() {  const x uint32 = 3231711232  myfloat := math.Float32frombits(x)  fmt.Print("The floating point number represented by ", x, " is: " , myfloat) }

RELATED TAGS

golang

CONTRIBUTOR

Faraz Karim 