整数除以JavaScript中的余数?

本文翻译自:Integer division with remainder in JavaScript?

In JavaScript, how do I get: 在JavaScript中,我如何获得:

  1. the whole number of times a given integer goes into another? 给定整数进入另一个整数的整数倍?
  2. the remainder? 剩下的?

#1楼

参考:https://stackoom.com/question/HjzI/整数除以JavaScript中的余数


#2楼

var remainder = x % y;
return (x - remainder) / y;

#3楼

I did some speed tests on Firefox. 我在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

The above is based on 10 million trials for each. 以上是基于每个1000万次试验。

Conclusion: Use (a/b>>0) (or (~~(a/b)) or (a/b|0) ) to achieve about 20% gain in efficiency. 结论:使用(a/b>>0) (或(~~(a/b))(a/b|0) )可以使效率提高约20%。 Also keep in mind that they are all inconsistent with Math.floor , when a/b<0 && a%b!=0 . 还要记住,当a/b<0 && a%b!=0时,它们都与Math.floor不一致。


#4楼

JavaScript calculates right the floor of negative numbers and the remainder of non-integer numbers, following the mathematical definitions for them. JavaScript根据数学定义计算负数的底限和非整数的其余部分。

FLOOR is defined as "the largest integer number smaller than the parameter", thus: FLOOR定义为“小于参数的最大整数”,因此:

  • positive numbers: FLOOR(X)=integer part of X; 正数:FLOOR(X)= X的整数部分;
  • negative numbers: FLOOR(X)=integer part of X minus 1 (because it must be SMALLER than the parameter, ie, more negative!) 负数:FLOOR(X)= X的整数部分减1(因为它必须比参数小,即更负!)

REMAINDER is defined as the "left over" of a division (Euclidean arithmetic). REMAINDER被定义为除法的“遗留”(欧几里德算术)。 When the dividend is not an integer, the quotient is usually also not an integer, ie, there is no remainder, but if the quotient is forced to be an integer (and that's what happens when someone tries to get the remainder or modulus of a floating-point number), there will be a non-integer "left over", obviously. 当被除数不是整数时,商通常也不是整数,即,没有余数,但如果商被强制为整数(当有人试图获得余数或模数时会发生这种情况)浮点数),显然会有一个非整数“遗留”。

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". JavaScript确实按预期计算了所有内容,因此程序员必须小心提出正确的问题(人们应该小心回答问题!)Yarin的第一个问题不是“X乘Y的整数除法是什么”,但是,相反,“给定整数进入另一个整数的次数”。 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). 对于正数,两者的答案是相同的,但不是负数,因为整数除法(除数除数)将比数字(除数)“进入”另一个(被除数)的次数小-1。 In other words, FLOOR will return the correct answer for an integer division of a negative number, but Yarin didn't ask that! 换句话说,FLOOR将返回负数的整数除法的正确答案,但Yarin没有问这个!

gammax answered correctly, that code works as asked by Yarin. gammax回答正确,该代码按Yarin的要求工作。 On the other hand, Samuel is wrong, he didn't do the maths, I guess, or he would have seen that it does work (also, he didn't say what was the divisor of his example, but I hope it was 3): 另一方面,塞缪尔错了,我猜他没有做数学,或者他会看到它确实有效(同样,他没有说他的例子的除数是什么,但我希望它是3):

扫描二维码关注公众号,回复: 10627732 查看本文章

Remainder = X % Y = -100 % 3 = -1 剩余= X%Y = -100%3 = -1

GoesInto = (X - Remainder) / Y = (-100 - -1) / 3 = -99 / 3 = -33 GoesInto =(X - 剩余)/ Y =( - 100 - -1)/ 3 = -99 / 3 = -33

By the way, I tested the code on Firefox 27.0.1, it worked as expected, with positive and negative numbers and also with non-integer values, both for dividend and divisor. 顺便说一句,我测试了Firefox 27.0.1上的代码,它按预期工作,有正数和负数,也有非整数值,分别用于红利和除数。 Example: 例:

-100.34 / 3.57: GoesInto = -28, Remainder = -0.3800000000000079 -100.34 / 3.57:GoesInto = -28,Remainder = -0.3800000000000079

Yes, I noticed, there is a precision problem there, but I didn't had time to check it (I don't know if it's a problem with Firefox, Windows 7 or with my CPU's FPU). 是的,我注意到,那里存在精确问题,但我没有时间检查它(我不知道它是Firefox,Windows 7还是我的CPU的FPU的问题)。 For Yarin's question, though, which only involves integers, the gammax's code works perfectly. 但是,对于Yarin的问题,只涉及整数,gammax的代码完美无缺。


#5楼

ES6 introduces the new Math.trunc method. ES6引入了新的Math.trunc方法。 This allows to fix @MarkElliot's answer to make it work for negative numbers too: 这允许修复@MarkElliot的答案 ,使其也适用于负数:

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

Note that Math methods have the advantage over bitwise operators that they work with numbers over 2 31 . 请注意, Math方法比按位运算符更有优势,它们可以使用超过2 31的数字。


#6楼

Math.floor(operation) returns the rounded down value of the operation. Math.floor(operation)返回Math.floor(operation)的向下舍入值。

Example of 1 st question: 第一个问题的例子:

var x = 5;
var y = 10.4;
var z = Math.floor(x + y);

console.log(z);

Console: 安慰:

15 15

Example of 2 nd question: 第二个问题的例子:

var x = 14;
var y = 5;
var z = Math.floor(x%y);

console.log(x);

Console: 安慰:

4 4

发布了0 篇原创文章 · 获赞 7 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/asdfgh0077/article/details/105382261