Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

golang

What is the Float64bits 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 Go programming language uses the Float64bits function to find the IEEE 754 binary representation for any input number in 64 bits.

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

Function definition

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

Parameters

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

Return value

The Float64bits function returns a single 64-bit unsigned integer or uint64, which represents the IEEE 754 binary of the input argument.

Since the return type is 64 bit numeric, the output values range from 00 to 2642^{64} - 1.

The Float64bits 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

Following is a simple example where we find out the IEEE 754 representation of 5000000:

package main
import (
"fmt"
"math"
)
func main() {
x := 5000000.0
ieee := math.Float64bits(5)
fmt.Print("IEEE 754 representation of ", x, " is: " , ieee)
}

Even though the return value is an unsigned integer, you can still find the IEEE 754 representation of negative numbers. The first bit of the binary representation of the output of Float64bits is considered the sign bit when you interpret it as an IEEE 754 binary. The following example shows this representation for -5:

package main
import (
"fmt"
"math"
)
func main() {
x := -5.0
ieee := math.Float64bits(x)
fmt.Print("IEEE 754 representation of ", x, " is: " , ieee)
}

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