Divide into zero prevention

What is 1.#INF and why does casting in float or double prevent division by 0 from crashes?
Also, any great ideas on how to prevent division by 0? (Like any macro or template)?

 int nQuota = 0; int nZero = 3 / nQuota; //crash cout << nZero << endl; float fZero = 2 / nQuota; //crash cout << fZero << endl; 

if i use instead:

 int nZero = 3 / (float)nQuota; cout << nZero << endl; //Output = -2147483648 float fZero = 2 / (float)nQuota; cout << fZero << endl; //Output = 1.#INF 
+8
c ++
source share
3 answers

1.#INF - positive infinity. You will get it when you divide the positive float by zero (if you divide the float itself by zero, then the result will be "not a number").

On the other hand, if you divide the integer by zero, the program crashes.

Cause of emergencies float fZero = 2 / nQuota; consists in the fact that both operands of the operator / are integers; therefore, division is performed by integers. It doesnโ€™t matter that you then save the result in a float; C ++ has no concept of target input.

Why a positive infinity other than a whole is the smallest integer, I have no idea.

+12
source share

Wwhy using (float) or (double) prevents division by 0 failure?

It's not obligatory. The standard is surprisingly reserved when it comes to floating point. Most systems currently use the IEEE floating point standard, and it says that the default action for dividing by zero is a return ยฑ infinity, not a crash. You can make this crash by including the appropriate floating point exceptions.

Remember well: the only floating point exception model and the C ++ exception model have the common word "exception". On every machine I work on, the floating point exception does not throw a C ++ exception.

Also, any great ideas on how to prevent division by 0?

  • The simple answer is: do not do this.
    This is one of those "Doctor, the doctor hurts when I do this!" kinds of situations. So donโ€™t do it!

  • Make sure the divisor is not zero.
    Perform health checks on dividers that are user inputs. Always filter your user inputs for convenience. A user input value of zero, when the number should be in millions, will cause all kinds of chaos, except overflow. Make health checks on intermediate values.

  • Include floating point exceptions.
    The default behavior to allow errors (and these are almost always errors) to go through unchecked was IMHO a big mistake on the part of the standards committee. Use the default value, and these infinities and non-numbers will eventually turn everything into Inf or NaN.
    By default, floating point errors should have stopped on their tracks, with the ability to resolve things like 1.0 / 0.0 and 0.0 / 0.0. This is not the case, so you need to enable these traps. Do this, and you can often find the cause of the problem in a short time.

  • Customize custom separator, custom multiplier, custom square root, custom sines, ... functions.
    Unfortunately, this is the route that many critical software security systems must follow. This is a royal pain. Option number 1 is absent, because it is just wishful thinking. Option 3 is missing because the system cannot be allowed to fail. Option # 2 is still a good idea, but it doesnโ€™t always work, because bad data always has a way to get inside. This is Murphy's Law.

By the way, the problem is a little worse than just dividing by zero. 10,200 / 10-200 will also overflow.

+3
source share

Usually you check that you do not divide by zero. The code below is not particularly useful if nQuota is legal but prevents crashes

 int nQuota = 0; int nZero = 0; float fZero = 0; if (nQuota) nZero = 3 / nQuota; cout << nZero << endl; if (nQuota) fZero = 2 / nQuota; cout << fZero << endl; 
+1
source share

All Articles