1

Following C++ code outputs negative zero instead of positive.

double a = 0, b = -1;
double c = a / b ;
std::cout << c << std::endl;

As both zeros (+0 and -0) will compare equal, have the same mathematical value, then why it is required to support signed zero?

What is the significance of this difference?

Arafat Hasan
  • 2,811
  • 3
  • 21
  • 38
  • 3
    The main implication is that a non-zero number divided by zero may result in plus or minus infinity depending on whether it is "plus zero" or "minus zero". Also is kind of a "natural consequence" of how floating point numbers are represented in IEEE-754. – jdehesa May 11 '18 at 12:29
  • Have you tried searching for it? Does e.g. https://en.wikipedia.org/wiki/Signed_zero help you? – Angew is no longer proud of SO May 11 '18 at 12:29
  • double a = 0, b = -1; double c = a / b ; std::cout << (c==0) << std::endl; this outputs 1, so I guess its just for noting that it was a result of negative value operations – Eduard Rostomyan May 11 '18 at 12:31
  • Pretty sure that fits, if it doesn't, please edit to explain how exactly your question is different. – Baum mit Augen May 11 '18 at 12:32
  • 2
    The same question has also been asked on another SE site and received a good answer: https://softwareengineering.stackexchange.com/questions/280648/why-is-negative-zero-important –  May 11 '18 at 12:33

0 Answers0