Last active
August 29, 2015 14:03
-
-
Save shawnsmithdev/0e920c9dfb1a3e6e6a48 to your computer and use it in GitHub Desktop.
Breakdown of float32 (IEEE 754 single-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 float32MantissaMask = 0x7FFFFF | |
const float32ExponentMask = 0x7F800000 | |
const float32SignMask = 0x80000000 | |
const float32ExponentBitOffset = 23 | |
const float32ExponentValueOffset = -127 | |
const float32SignOffset = 31 | |
func main() { | |
floatDebug(3.14159) | |
} | |
func floatDebug(bar float32) { | |
barBits := math.Float32bits(bar) | |
fmt.Printf("Value : %f\n", bar) | |
fmt.Printf("Bits : %X\n", barBits) | |
fmt.Printf("Mantissa : %X\n", barBits & float32MantissaMask) | |
fmt.Printf("Exponent : %X\n", (int64(barBits & float32ExponentMask) >> float32ExponentBitOffset) + float32ExponentValueOffset) | |
fmt.Printf("Sign : %X\n", (barBits & float32SignMask) >> float32SignOffset) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment