A double in C + + has a negative 0 (negative zero). Negative 0 is meaningful in some calculations. For example:
1.0/0.0 = = infinity1.0/-0.0 ==–infinity
In addition, negative 0 is also used in some mathematical functions, such as sqrt (Std::complex)
At the same time, the addition and subtraction of 0 and minus Zero also has its particularity, such as the following code:
#include <iostream>int main () {Double A (1.0), B (0.0), C ( -0.0); Std::cout << std::hex;std::cout << "a =" & lt;< a << std::endl;std::cout << "b =" << b << std::endl;std::cout << "c =" << C << std::endl;std::cout << "as divisor" << std::endl;std::cout << a << "/" << B << " = "<< A/b << std::endl;std::cout << a <<"/"<< c <<" = "<< A/C << ; Std::endl;std::cout << "Add" << std::endl;std::cout << a << "+" << c << "=" << ; A + c << std::endl;std::cout << b << "+" << c << "=" << B + C << Std::endl; Std::cout << C << "+" << c << "=" << C + C << std::endl;std::cout << "subtraction" &l t;< std::endl;std::cout << a << "-" << c << "=" << a-c << std::endl;std::cout << b << "-" << c << "=" << b-c << std::endl;std::cout << C << "-" << b << "=" << c-b << std::endl;std::cout << C << "-" << c << "=" << c-c < < std::endl;std::cout << "multiplication" << std::endl;std::cout << a << "*" << c << "=" &l t;< A * c << std::endl;std::cout << b << "*" << c << "=" << b * C << std: : Endl;std::cout << C << "*" << c << "=" << c * C << std::endl;std::cout << " Division "<< Std::endl;std::cout << B <<"/"<< b <<" = "<< b/b << std::endl;std :: cout << B << "/" << c << "=" << b/c << Std::endl; return 0;}
The result of this code compilation run in VS2015 is:
From the results, we can see the results of 0 and negative zeros.
Operations of double type 0 and –0 in C + +