在JavaScript中,我如何得到:
一个给定整数除另一个整数的整数倍是多少? 其余的呢?
在JavaScript中,我如何得到:
一个给定整数除另一个整数的整数倍是多少? 其余的呢?
当前回答
const idivmod = (a, b) => [a/b |0, a%b];
还有一项关于这个问题的提案 模量和附加整数数学
其他回答
JavaScript根据负数的数学定义计算负数的底数和非整数的余数。
FLOOR定义为“小于参数的最大整数”,即:
正数:FLOOR(X)= X的整数部分; 负数:FLOOR(X)= X - 1的整数部分(因为它必须小于参数,即更负!)
余数被定义为除法(欧几里得算术)的“剩余部分”。当被除数不是整数时,商通常也不是整数,即没有余数,但如果商被强制为整数(这就是当有人试图得到一个浮点数的余数或模量时发生的情况),显然会有一个非整数“剩下”。
JavaScript does calculate everything as expected, so the programmer must be careful to ask the proper questions (and people should be careful to answer what is asked!) Yarin's first question was NOT "what is the integer division of X by Y", but, instead, "the WHOLE number of times a given integer GOES INTO another". For positive numbers, the answer is the same for both, but not for negative numbers, because the integer division (dividend by divisor) will be -1 smaller than the times a number (divisor) "goes into" another (dividend). In other words, FLOOR will return the correct answer for an integer division of a negative number, but Yarin didn't ask that!
gammax回答正确,代码按照Yarin的要求工作。另一方面,塞缪尔是错的,我猜他没有做数学运算,否则他会看到这是可行的(而且,他没有说他的例子的除数是多少,但我希望是3):
余数= X % Y = - 100% 3 = -1
GoesInto = (X -余数)/ Y = (-100 -1) / 3 = -99 / 3 = -33
顺便说一下,我在Firefox 27.0.1上测试了代码,它按预期工作,有正数和负数,也有非整数值,包括被除数和除数。例子:
-100.34 / 3.57:Goesinto = -28,余数 = -0.38000000000000079
是的,我注意到,这里有一个精度问题,但我没有时间检查它(我不知道这是否是Firefox, Windows 7或我的CPU的FPU的问题)。然而,对于Yarin的问题,它只涉及整数,gammax的代码完美地工作。
它总是截断为零。 不知道是不是太迟了,但我要说的是:
function intdiv(dividend, divisor) {
divisor = divisor - divisor % 1;
if (divisor == 0) throw new Error("division by zero");
dividend = dividend - dividend % 1;
var rem = dividend % divisor;
return {
remainder: rem,
quotient: (dividend - rem) / divisor
};
}
我在Firefox上做了一些速度测试。
-100/3 // -33.33..., 0.3663 millisec
Math.floor(-100/3) // -34, 0.5016 millisec
~~(-100/3) // -33, 0.3619 millisec
(-100/3>>0) // -33, 0.3632 millisec
(-100/3|0) // -33, 0.3856 millisec
(-100-(-100%3))/3 // -33, 0.3591 millisec
/* a=-100, b=3 */
a/b // -33.33..., 0.4863 millisec
Math.floor(a/b) // -34, 0.6019 millisec
~~(a/b) // -33, 0.5148 millisec
(a/b>>0) // -33, 0.5048 millisec
(a/b|0) // -33, 0.5078 millisec
(a-(a%b))/b // -33, 0.6649 millisec
以上是基于每种方法的1000万次试验。
结论:使用(a/b>>0)(或(~~(a/b))或(a/b|0))可获得20%左右的效率提升。还要记住,它们都与数学不一致。地板,当a/b<0 && a%b!=0。
你也可以使用三元来决定如何处理正整数值和负整数值。
var myInt = (y > 0) ? Math.floor(y/x) : Math.floor(y/x) + 1
如果这个数字是正数,就没有问题。如果这个数字是负数,它会加1,因为数学。地板处理否定。
var remainder = x % y;
return (x - remainder) / y;