[thelist] [JS] Modulo Operator question
Kasimir K
evolt at kasimir-k.fi
Mon Mar 29 15:14:40 CST 2004
As JavaScript seems a bit vague with this - reference says int, but you
do in practice get float - I had a look at what ecma 262 says (sorry,
only found it as pdf, so I paste some here):
"
11.5.3 Applying the % Operator
The % operator yields the remainder of its operands from an implied
division; the left operand is the dividend and the right operand is the
divisor.
NOTE
In C and C++, the remainder operator accepts only integral operands; in
ECMAScript, it also accepts floating-point operands.
The result of a floating-point remainder operation as computed by the %
operator is not the same as the “remainder” operation defined by IEEE
754. The IEEE 754 “remainder” operation computes the remainder from a
rounding division, not a truncating division, and so its behaviour is
not analogous to that of the usual integer remainder operator. Instead
the ECMAScript language defines % on floatingpoint operations to behave
in a manner analogous to that of the Java integer remainder operator;
this may be compared with the C library function fmod.
The result of a ECMAScript floating-point remainder operation is
determined by the rules of IEEE arithmetic:
• If either operand is NaN, the result is NaN.
• The sign of the result equals the sign of the dividend.
• If the dividend is an infinity, or the divisor is a zero, or both, the
result is NaN.
• If the dividend is finite and the divisor is an infinity, the result
equals the dividend.
• If the dividend is a zero and the divisor is finite, the result is the
same as the dividend.
• In the remaining cases, where neither an infinity, nor a zero, nor NaN
is involved, the floating-point remainder r from a dividend n and a
divisor d is defined by the mathematical relation r = n - (d * q) where
q is an integer that is negative only if n/d is negative and positive
only if n/d is positive, and whose magnitude is as large as possible
without exceeding the magnitude of the true mathematical quotient of n
and d.
"
According to this it is pretty clear: if dividend or divisor is float,
the mod is flot too.
.k
More information about the thelist
mailing list