unsafe.go 1.1 KB

12345678910111213141516171819202122232425
  1. package yu_math
  2. import "unsafe"
  3. // Float32bits returns the IEEE 754 binary representation of f,
  4. // with the sign bit of f and the result in the same bit position.
  5. // Float32bits(Float32frombits(x)) == x.
  6. func Float32bits(f float32) uint32 { return *(*uint32)(unsafe.Pointer(&f)) }
  7. // Float32frombits returns the floating-point number corresponding
  8. // to the IEEE 754 binary representation b, with the sign bit of b
  9. // and the result in the same bit position.
  10. // Float32frombits(Float32bits(x)) == x.
  11. func Float32frombits(b uint32) float32 { return *(*float32)(unsafe.Pointer(&b)) }
  12. // Float64bits returns the IEEE 754 binary representation of f,
  13. // with the sign bit of f and the result in the same bit position,
  14. // and Float64bits(Float64frombits(x)) == x.
  15. func Float64bits(f float64) uint64 { return *(*uint64)(unsafe.Pointer(&f)) }
  16. // Float64frombits returns the floating-point number corresponding
  17. // to the IEEE 754 binary representation b, with the sign bit of b
  18. // and the result in the same bit position.
  19. // Float64frombits(Float64bits(x)) == x.
  20. func Float64frombits(b uint64) float64 { return *(*float64)(unsafe.Pointer(&b)) }