I have a C++ code where I sometimes get very small double numbers and after rounding they become either +0
or -0
. Is it guaranteed that -0
will always behave like normal zero in comparisons? For example, see this code:
#include <iostream>
#include <cmath>
int main() {
double accum = 0;
for (int step = 0; step < 32; ++step) {
double result = .1 * step - accum;
accum += .1;
result = round(result * 1e8) * 1e-8;
std::cout << result << " ";
if (result < 0) {
std::cout << "< 0";
}
else if (result == 0) {
std::cout << "== 0";
}
else {
std::cout << "> 0";
}
std::cout << "\n";
}
return 0;
}
If I run it online it always says that -0
is equal to 0
as seen here.
The question is, is it guaranteed to behave like this on any computer?