The behaviour of floating point division by zero

C++ standard does not force the IEEE 754 standard, because that depends mostly on hardware architecture. If the hardware/compiler implement correctly the IEEE 754 standard, the division will provide the expected INF, -INF and NaN, otherwise… it depends. Undefined means, the compiler implementation decides, and there are many variables to that like the hardware architecture, … Read more

c++ division by 0

For IEEE floats, division of a finite nonzero float by 0 is well-defined and results in +infinity (if the value was >zero) or -infinity (if the value was less than zero). The result of 0.0/0.0 is NaN. If you use integers, the behaviour is undefined.

Is 1/0 a legal Java expression?

Is 1/0 actually a legal Java expression that should compile anytime anywhere? Yes. What does JLS say about it? Nothing specific … apart from saying that division by zero will result in a runtime exception. However, the JLS acknowledges that possibility of runtime exceptions in the following definition: “A compile-time constant expression is an expression … Read more

How do I catch a numpy warning like it’s an exception (not just for testing)?

It seems that your configuration is using the print option for numpy.seterr: >>> import numpy as np >>> np.array([1])/0 #’warn’ mode __main__:1: RuntimeWarning: divide by zero encountered in divide array([0]) >>> np.seterr(all=”print”) {‘over’: ‘warn’, ‘divide’: ‘warn’, ‘invalid’: ‘warn’, ‘under’: ‘ignore’} >>> np.array([1])/0 #’print’ mode Warning: divide by zero encountered in divide array([0]) This means that … Read more

Can’t Mod Zero?

The C++ Standard(2003) says in ยง5.6/4, […] If the second operand of / or % is zero the behavior is undefined; […] That is, following expressions invoke undefined-behavior(UB): X / 0; //UB X % 0; //UB Note also that -5 % 2 is NOT equal to -(5 % 2) (as Petar seems to suggest in … Read more

Why doesn’t Java throw an Exception when dividing by 0.0?

Java’s float and double types, like pretty much any other language out there (and pretty much any hardware FP unit), implement the IEEE 754 standard for floating point math, which mandates division by zero to return a special “infinity” value. Throwing an exception would actually violate that standard. Integer arithmetic (implemented as two’s complement representation … Read more