I would argue that dividing by very tiny numbers is also something that you should probably be catching, since the output very large numbers are likely not going to do what you’re expecting...
There are some cool videos of wrist flips online, and I’ve heard of a pickup truck being thrown by an articulated arm on a factory floor due to it.
Sounds worth the precaution, given the right context!
For a 32-bit float, the following are true:
Positive infinity: 011111111(and then 23 zeroes)
Negative infinity: 111111111(and then 23 zeroes)
NaN: x11111111(and then anything except all zeroes)
... x being either 0 or 1.
Source: https://en.wikipedia.org/wiki/IEEE_754-1985#Positive_and_neg...So, below, the first one is defined, while the second one isn’t.
lim_{x->0+} 1/x == +Infinity
1/0 != (anything)So in general, assuming anything like \infty for a division by zero is wrong wrong wrong.
lim_{x->1+} (x^2-1)/(x-1) == 2
Of course ideally evaluating this particular formula for x == 0 would give 0/0 which IEEE FP defines as NaN, but you could also get a non-zero finite number divided by zero due to rounding for x ≅ 1 and then the default result of +Infinity would be very far from the correct answer. Any situation where you might come close to a 0/0 or ∞/∞ term requires very careful consideration since there is very little separation between a NaN result and ±Infinity.More formally, if you have a potential division by zero and get an Inf (or NaN in case it's 0/0), you have to show what impact this will have. Can it result in a safety violation? If yes, what ASIL level would this result in? Based on this, you define your mitigation. It seems the safety folks have defined the mitigation to be "shut down the device". If you can show that there is a simpler mitigation, or that a mitigation is not needed (i.e. has no safety impact), then they should back off. This mitigation does need to be noted and documented, though.
Of course, this could be an organizational issue: Perhaps the safety folks are being the safety gatekeepers as a small portion of their job, so they are not willing to spend time to deal with nuances. I was the guy responsible for safety for my team, and it sucked - but I was doing it full time so I could analyze scenarios. The challenge was that the developers were not well versed with the ISO standard (had never read it), and it was a small portion of their job, and so they kept arguing.
> This is stupid because a division by a very small number has the same effect as division by zero.
As others have pointed out, this is not true. There is a big difference between a very large result and Inf. And of course, if a very large result could lead to a safety violation, then you need to handle it.
> For floating point, division-by-zero is perfectly fine and well defined by IEEE 754.
Keep in mind that conformance to the IEEE 754 standard is often not complete. Don't assume your language (or your libraries) are fully conformant, unless they are advertised as such.
Even if the ISO 26262 standard seems to have definitive language, I seem to recall there is a portion of the standard that allows you to waive requirements if the requirement makes no sense in your use case. There is a procedure to do it (documenting, etc).
FWIW, we did not handle Inf or NaN or divide by zero anywhere. We were writing a general purpose library for auto manufacturers, and it was up to them to decide where/how it would be used (called "Safety Element Out of Context" in the standard). Since we did not know the context, we argued that we can't assess if a division by zero would be hazardous, and merely noted in the documentation where it could occur. It's up to the folks who buy our libraries to then do that analysis. We only took care of things like showing there were no known crashes or hangs.
You are correct that it is possible in general. However, in this specific project, the decision was to shut down the device for safety reasons if the CPU detects a floating point division by zero. In my opinion this reduces the availability of the functions unnecessarily.
It doesn't, it cannot ever return NaN.
ISO 26262 includes the requirement to prevent “implausible values, execution errors, division by zero, and errors in data flow and control flow” (8.4.4). I'm confident the division by zero aims at integer division because the result is undefined. For floating point, division-by-zero is perfectly fine and well defined by IEEE 754.
Nevertheless, our safety folks require us to enable the traps and shut down the device in case a floating division by zero occurs. This is stupid because a division by a very small number has the same effect as division by zero. However, since it is explicitly mentioned in ISO 26262 nobody has the balls to allow it officially.
I don't even want to estimate how many man-months have been burned to fix such "defects".