Menu Close

Go – How to get IEEE 754 binary representation of number with Float64bits in go

Here, We will learn how to get IEEE 754 binary representation of number with Float64bits in go. We can get it by using Float64bits() function in math package in go golang.

Function prototype:

func Float64bits(no float64) uint64

Return value:

Float64bits() function in math package returns the
IEEE 754 binary representation of number with the
sign bit of number.
It gives result in the same bit position

Float64bits(Float64frombits(no)) == no

Example with code:

package main

import (
  "fmt"
  "math"
)

func main() {

  no := math.Float64bits(2)
  fmt.Println(no)

  no = math.Float64bits(1)
  fmt.Println(no)

  no = math.Float64bits(2.12)
  fmt.Println(no)

  no = math.Float64bits(3.4)
  fmt.Println(no)
  
}

Output:

4611686018427387904
4607182418800017408
4611956234405030134
4614838538166547251

To learn more about golang, Please refer given below link:

https://www.techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org/pkg/
Posted in golang, math

Leave a Reply

Your email address will not be published. Required fields are marked *