Skip to content

Instantly share code, notes, and snippets.

@zostay
Created August 31, 2023 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zostay/c1bd2e592723edae5773b1640ab47b92 to your computer and use it in GitHub Desktop.
Save zostay/c1bd2e592723edae5773b1640ab47b92 to your computer and use it in GitHub Desktop.
A little program to show the innards of a floating point number.
package main
import (
"fmt"
"math"
"math/big"
"os"
)
func main() {
v := os.Args[1]
num := big.NewInt(0)
mark := -1
for i, c := range v {
if c == '.' {
mark = i
}
digit := c - '0'
num = num.Mul(num, big.NewInt(10))
num = num.Add(num, big.NewInt(int64(digit)))
}
den := big.NewInt(1)
if mark > -1 {
den = den.Exp(big.NewInt(10), big.NewInt(int64(len(v)-mark-1)), nil)
}
rat := big.NewRat(0, 1)
rat = rat.SetFrac(num, den)
f64, _ := rat.Float64()
fmt.Printf("%064b\n", math.Float64bits(f64))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment