# Why does Go have a special case for abs(0)

I was playing around with Go, and found this particular interesting code for the abs function in the math package:

http://golang.org/src/pkg/math/abs.go

``````14 func abs(x float64) float64 {
15      switch {
16      case x < 0:
17          return -x
18      case x == 0:
19          return 0 // return correctly abs(-0)
20      }
21      return x
22  }
``````

Why do we need to have the special case of x == 0? What will happen if I delete line 18 and 19?

## 2 Answers

The comment explains the reason - `abs(-0)` should return 0, but without the special case, `abs(-0)` would return -0.

I assume Go uses IEEE floats so both +0 and -0 can be represented using different values for the sign bit.

• Okay then, but aren't 0 and -0 represented the same way in the memory? – user84386 Mar 14 '13 at 19:10
• @user84386 - I assume Go uses IEEE floats, so it will have a sign bit, so both +0 and -0 are representible. – Lee Mar 14 '13 at 19:12

The IEEE 754 floating-point standard allows signed zeros. A negative zero is equal to a positive zero, so it wouldn't be covered by the `< 0` test.