# Negative and positive floating point zeros

Back to Blog

```// The negative and positive floating point zero
//
// Sometimes algorithms make use
// of the property that 1/0 = Inf. However,
// there are two kinds of zeros, -0 and +0.
// 1/+0 = Inf, but 1/-0 = -Inf!
// This can cause bugs that are hard to resolve:
// the possibility of -0 must be explicitly
// taken into account when designing such
// algorithms.
// It is very easy to get negative zeros:
// a = -2;
// b = 0;
// c = a * b;
// There is no way to check whether
// the zero you have at hand is -0 or +0 (except
// by explicitly checking the sign bit).
// Run this program for details.

#include <iostream>
#include <string>

using namespace std;

int main()
{
float a = 1;
float b = 0;
float c = -b;

cout << "1 / +0 = " << a / b << endl;
cout << "1 / -0 = " << a / c << endl;
cout << endl;
cout << "(+0 == -0) = " << (b == c) << endl;
cout << "(-0 == +0) = " << (c == b) << endl;
cout << endl;
cout << "(+0 < -0) = " << (b < c) << endl;
cout << "(-0 < +0) = " << (c < b) << endl;
cout << "(+0 < +0) = " << (b < b) << endl;
cout << "(-0 < -0) = " << (c < c) << endl;
cout << endl;
cout << "(+0 + -0) = " << (b + c) << endl;
cout << "(-0 + +0) = " << (c + b) << endl;
cout << "(+0 + +0) = " << (b + b) << endl;
cout << "(-0 + -0) = " << (c + c) << endl;
cout << endl;
cout << "(+0 - -0) = " << (b - c) << endl;
cout << "(-0 - +0) = " << (c - b) << endl;
cout << "(+0 - +0) = " << (b - b) << endl;
cout << "(-0 - -0) = " << (c - c) << endl;
cout << endl;
cout << "(+0 * -0) = " << (b * c) << endl;
cout << "(-0 * +0) = " << (c * b) << endl;
cout << "(+0 * +0) = " << (b * b) << endl;
cout << "(-0 * -0) = " << (c * c) << endl;
cout << endl;
cout << "(+0 / -0) = " << (b / c) << endl;
cout << "(-0 / +0) = " << (c / b) << endl;
cout << "(+0 / +0) = " << (b / b) << endl;
cout << "(-0 / -0) = " << (c / c) << endl;
cout << endl;

string tmp;
getline(cin, tmp);

return 0;
}
```