在JavaScript中,我如何得到:
一个给定整数除另一个整数的整数倍是多少? 其余的呢?
在JavaScript中,我如何得到:
一个给定整数除另一个整数的整数倍是多少? 其余的呢?
当前回答
你也可以使用三元来决定如何处理正整数值和负整数值。
var myInt = (y > 0) ? Math.floor(y/x) : Math.floor(y/x) + 1
如果这个数字是正数,就没有问题。如果这个数字是负数,它会加1,因为数学。地板处理否定。
其他回答
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的代码完美地工作。
var remainder = x % y;
return (x - remainder) / y;
如果你需要计算非常大的整数的余数,而JS运行时不能这样表示(任何大于2^32的整数都表示为浮点数,所以它失去了精度),你需要做一些技巧。
这对于检查我们日常生活中许多情况下的支票数字(银行账号、信用卡等)尤为重要。
首先,你需要你的数字作为一个字符串(否则你已经失去了精度,余数没有意义)。
str = '123456789123456789123456789'
现在需要将字符串分割成更小的部分,足够小,以便任何余数和一段字符串的连接可以容纳9位数字。
digits = 9 - String(divisor).length
准备一个正则表达式来分割字符串
splitter = new RegExp(`.{1,${digits}}(?=(.{${digits}})+$)`, 'g')
例如,如果digits为7,则regexp为
/.{1,7}(?=(.{7})+$)/g
它匹配最大长度为7的非空子字符串,后面跟着((?=…)是正前向)若干个字符,该字符是7的倍数。“g”是让表达式遍历所有字符串,而不是在第一次匹配时停止。
现在将每个部分转换为整数,并通过reduce计算余数(将之前的余数加回去-或0 -乘以正确的10次方):
reducer = (rem, piece) => (rem * Math.pow(10, digits) + piece) % divisor
这是可行的,因为“减法”余数算法:
n mod d = (n - kd) mod d
它允许用余数替换十进制表示的任何“初始部分”,而不影响最后的余数。
最终代码看起来像这样:
function remainder(num, div) {
const digits = 9 - String(div).length;
const splitter = new RegExp(`.{1,${digits}}(?=(.{${digits}})+$)`, 'g');
const mult = Math.pow(10, digits);
const reducer = (rem, piece) => (rem * mult + piece) % div;
return str.match(splitter).map(Number).reduce(reducer, 0);
}
我通常使用:
const quotient = (a - a % b) / b;
const remainder = a % b;
它可能不是最优雅的,但它是有效的。
对于某个数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]由一个数除以另一个数得到的整数