top | item 25517156

(no title)

lock-free | 5 years ago

Why not NaN for floating point division and INT_MAX for integer division?

When you write code that may divide by zero you need to check the result afterwards (and in truly defensive programming that is every time you divide numbers). 0 is not a great choice because it doesn't necessarily mean you divided by zero - in floating point, dividing by a sufficiently large number or when working with the status register set to flush denormals to zero then normal division can result in 0. For integer division, any fraction will result in 0.

No valid division can result in NaN or INT_MAX.

discuss

order

No comments yet.