Hacker News new | ask | show | jobs
by CupricTea 248 days ago
Division by zero is mathematically undefined. So two's complement integer division by zero is always undefined.

For floating point there is the interesting property that 0 is signed due to its signed magnitude representation. Mathematically 0 is not signed but in floating point signed magnitude representation, "+0" is equivalent to lim x->0+ x and "-0" is equivalent to lim x->0- x.

This is the only situation where a floating point division by "zero" makes mathematical sense, where a finite number divided by a signed zero will return a signed +/-Inf, and a 0/0 will return a NaN.

Why should 0/0 return a NaN instead of Inf? Because lim x->0 4x/x = 4, NOT Inf.

1 comments

OK, but I think it's not up to the programming language designers to define mathematical properties of the operations on specific data types.

I think the most pragmatic solution is to have 2 tiers:

1. use existing standards (i.e. IEEE 754 for FP, de-facto standards for integers, like two's complement, Big-Endian, etc.)

2. fast, native format per each compute device, using different sub-types so you will not be able to mix them in the same expression