Negative zero is always zero

I have some C ++ code where I sometimes get very small double numbers and after rounding they become either +0

or -0

. Is it guaranteed to -0

always behave like a normal zero in comparison? 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 -0

equal 0

, as shown here .

The question is, is it guaranteed to behave like this on any computer?

+3


source to share





All Articles