Created
August 31, 2023 19:35
-
-
Save zostay/c1bd2e592723edae5773b1640ab47b92 to your computer and use it in GitHub Desktop.
A little program to show the innards of a floating point number.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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