I want to serialize a floating point in such a way that sign info is not lost. Specifically, I would like to distinguish IEEE-754 negative zero from regular zero.
The language spec says
The result of a floating-point division by zero is not specified beyond the IEEE-754 standard; whether a run-time panic occurs is implementation-specific.
which suggests that I cannot do
n == 0 && (float64(1) / n) < 0
and I tried math.Copysign and math.Signbit which says
func Signbit(x float64) bool
Signbitreturnstrueif x is negative or negative zero.
but
n == 0 && math.Signbit(n)
doesn’t seem to work for
n := -float64(0)
Any ideas?
EDIT:
I filed issue 2196 to track what I think is a confusing difference between
nz := -float64(0)
and
pz := float64(0)
nz := -pz
as suggested by peterSO.
Output: