在JavaScript中,我如何得到:

一个给定整数除另一个整数的整数倍是多少? 其余的呢?


当前回答

ES6引入了新的数学。trunc方法。这允许修复@MarkElliot的答案,使其适用于负数:

var div = Math.trunc(y/x);
var rem = y % x;

注意,Math方法与位运算符相比有一个优势,那就是它们处理大于231的数字。

其他回答

您可以使用parseInt函数来获得截断的结果。

parseInt(a/b)

要得到余数,使用mod操作符:

a%b

parseInt有一些陷阱字符串,以避免使用基数参数以10为基数

parseInt("09", 10)

在某些情况下,数字的字符串表示可以是科学符号,在这种情况下,parseInt将产生错误的结果。

parseInt(100000000000000000000000000000000, 10) // 1e+32

这个调用的结果是1。

如果你只是用2的幂除法,你可以使用位运算符:

export function divideBy2(num) {
  return [num >> 1, num & 1];
}

export function divideBy4(num) {
  return [num >> 2, num & 3];
}

export function divideBy8(num) {
  return [num >> 3, num & 7];
}

(第一个是商,第二个是余数)

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的代码完美地工作。

对于某个数y和某个除数x,计算商(商)[1]和余数(余)为:

const quotient = Math.floor(y/x);
const remainder = y % x;

例子:

const quotient = Math.floor(13/3); // => 4 => the times 3 fits into 13  
const remainder = 13 % 3;          // => 1

[1]由一个数除以另一个数得到的整数