Last active
August 29, 2015 14:15
-
-
Save shawnsmithdev/58dfbb89c07b90cdfaa3 to your computer and use it in GitHub Desktop.
Breakdown of float64 (IEEE 754 double-precision binary floating-point format)
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" | |
) | |
const float64MantissaMask = 0xFFFFFFFFFFFFF | |
const float64ExponentMask = 0x7FF0000000000000 | |
const float64SignMask = 0x8000000000000000 | |
const float64ExponentBitOffset = 52 | |
const float64ExponentValueOffset = -1023 | |
const float64SignOffset = 63 | |
func main() { | |
floatDebug(math.Pi) | |
} | |
func floatDebug(bar float64) { | |
barBits := math.Float64bits(bar) | |
fmt.Printf("Value : %f\n", bar) | |
fmt.Printf("Bits : %X\n", barBits) | |
fmt.Printf("Mantissa : %X\n", barBits&float64MantissaMask) | |
fmt.Printf("Exponent : %X\n", (int64(barBits&float64ExponentMask)>>float64ExponentBitOffset)+float64ExponentValueOffset) | |
fmt.Printf("Sign : %X\n", (barBits&float64SignMask)>>float64SignOffset) | |
} |
Author
shawnsmithdev
commented
Feb 16, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment