using namespace std; int main() { double u = 0; double w = -u; cout << w << endl; return 0; } ...">

Cout prints "-0" instead of "0"

#include <iostream> using namespace std; int main() { double u = 0; double w = -u; cout << w << endl; return 0; } 

Why is this large piece of code outputting -0 , not 0 , as you would expect?

+4
source share
7 answers

The IEEE 754 standard for floating point numbers has a sign bit separate from the mantissa, which allows you to negate zero. Wikipedia should help explain this.

+13
source

In the floating point IEEE 0 and -0 are both different values: here in the section "Special values":

Note that -0 and +0 are different but they are both compared as equal.

+2
source

The IEEE 754 standard for floating point arithmetic makes a distinction between +0 and -0 , this can be used when working with very small numbers, rounded to zero, where the sign still matters.

+2
source

Because "negative zero" is a valid number!

http://en.wikipedia.org/wiki/%E2%88%920_(number)

+1
source

Take a look at this article: http://en.wikipedia.org/wiki/Floating_point . Note that there is a sign bit, even if the value is zero.

+1
source

Since the double can really have values ​​-0, +0, -infinity, + infinity and NaN, which can be the result of various interesting expressions, such as 0/0.

Check here for more information.

+1
source

Because your expectations are wrong.

IEEE requires that positive and negative zero be presented separately.

Here is what you see here.

+1
source

Source: https://habr.com/ru/post/1276757/


All Articles