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
c ++ floating-point rounding


source to share


No one has answered this question yet

See similar questions:

4
Behavior of negative zero (-0.0) versus positive zero (+0.0)

or similar:

690
Format number that should always show 2 decimal places
568
Why is the sizeof for a structure not equal to the sum of the sizeof of each member?
144
Is floating point math in C #? Could it be so?
68
Is (x - x) always positive zero for doubles, or sometimes negative zero?
27
How do you check for infinite and undefined values ​​in C ++?
13
Floating point - is an equality comparison sufficient to prevent division by zero?
8
float / double equality with exact zero
6
.NET - floating point comparison
five
In Java, how to achieve this type of rounding of numbers?
0
Matrix multiplication by scalar zero



All Articles
Loading...
X
Show
Funny
Dev
Pics