Uses for negative zero floating point value?

前端 未结 5 1353
天命终不由人
天命终不由人 2020-12-03 04:53

Consider the following C++ code:

double someZero = 0;
std::cout << 0 - someZero << \'\\n\';   // prints 0
std::cout << -someZero << s         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 05:58

    There are only two real use-cases that I can see:

    1. You want to show that a value is negative but very very small (perhaps infinitessimal), i.e. too small to represent as a float or double.
    2. You are working with math that only allows negatives, but still want to display zero. There are a few cases in physics, complex numbers and number theory where this can be useful.

    For the mostpart, it's not useful and should be avoided.

    You may also want to take a look at this question: Is there a negative zero? and the IEEE 754 spec for floating point.

提交回复
热议问题