11.5.2 Applying the / operator

2010-05-17

Ecmascript does not do "integer division", just double precision floating point division (regardless of precision). Which is something you should usually not observe, but sometimes may.

Code: (Meta Ecma)
function ApplyDivision(left, right) {
// if either operand is NaN, the result is NaN
if (isNaN(left) || isNaN(right)) return NaN;
// sign is positive if both values have same sign, negative otherwise (implicitly implemented)
// infinity divided by infinite equals crap
if ((IsInfinite(left) && IsInfinite(right)) return NaN;
// finite divided by zero is infinite
if (IsInfinite(left) || IsInfinite(right)) return (Sign(left)==Sign(right)?infinite:-infinite);
// finite divided by finite equals zero
if (IsInfinite(left) != IsInfinite(right)) return (Sign(left)==Sign(right)?+0:-0);
// 0/0 is NaN
if (left === 0 && right === 0) return NaN;
// 0/anything else is 0
if (left === 0) return (Sign(left)==Sign(right)?+0:-0);
// anything/0 is infinite
if (right === 0) return (Sign(left)==Sign(right)?+infinite:-infinite);
// otherwise, apply * according to the IEEE 754 round-to-nearest mode, or whatever.
return left / right; // could have just done this right from the start ;)
}