Y
Y
YchenikPMI2020-09-17 19:47:03
C++ / C#
YchenikPMI, 2020-09-17 19:47:03

Why does dividing 0 by -5 result in -0?

Or, in general, when dividing 0 by any real negative number, you get -0. How to get rid of this, and make it so that it outputs not -0, but 0 to the console. I can roughly guess why: when dividing an infinitely small by a simply negative number, it turns out infinite a small negative value, it cannot be written to double, so it turns out -0. So how do you get rid of this -0 ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2020-09-17
@YchenikPMI

You can do something like this:

#include <cfloat>
...
if (res > -DBL_EPSILON && res < DBL_EPSILON) res = 0;
cout << res;

As DBL_EPSILON, you can choose another number that is more convenient for you.
It won't be nice here. Just as it is useless to do an exact comparison (==) of two floating point numbers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question