本文翻译自: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):

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

整数除以JavaScript中的余数?相关推荐

  1. 在JavaScript中生成特定范围内的随机整数?

    如何可以生成两个指定的变量之间的随机整数在JavaScript中,例如x = 4和y = 8将输出任何的4, 5, 6, 7, 8 ? #1楼 对于具有范围的随机整数,请尝试: function ra ...

  2. JavaScript中判断为整数的多种方式

    JavaScript中不区分整数和浮点数,所有数字内部都采用64位浮点格式表示,和Java的double类型一样.但实际操作中比如数组索引.位操作则是基于32位整数. 方式一.使用取余运算符判断 任何 ...

  3. BigInt:JavaScript 中的任意精度整数

    Infinity的概念 Infinity代表了超出JavaScript处理范围的数值.也就是说JS无法处理的数值都是Infinity.实践证明,JS所能处理的最大值是1.797693134862315 ...

  4. 如何在JavaScript中检查变量是否为整数?

    本文翻译自:How to check if a variable is an integer in JavaScript? How do I check if a variable is an int ...

  5. python除以10取整_python中整数除以整数的结果是取整数

    整数除以整数 看官请在启动idle之后,练习下面的运算: >>> 2/5 0 >>> 2.0/5 0.4 >>> 2/5.0 0.4 >&g ...

  6. JavaScript中变量判断是否是数字,判断是否是整数,判断是否是正整数/负整数,判断奇数/偶数的方法

    (1)判断是否是整数  JavaScript中变量判断是否是数字,判断是否是整数,判断是否是正整数/负整数,判断奇数/偶数的方法. (1)判断是否是整数 // 判断整数function isInteg ...

  7. Javascript中0除以0得到NaN,1除以0得到infinity(无穷尽)

    1.任何数值除以0都会导致错误而终止程序执行.但是在 JavaScript 中,会返回出特殊的值,因此不会影响程序的执行. 2.比0大的数除以0,则会得到无穷大,所以 js 用 Infinity 来显 ...

  8. JavaScript中大数相加的解法

    一.两个大正整数字符串相加 在JavaScript中,数值类型满足不了大数据容量计算,可以用字符串进行操作 1 function add(strNum1, strNum2) { 2 // 将传进来的数 ...

  9. 检查JavaScript中变量是数字还是字符串

    有谁知道如何检查JavaScript中的变量是数字还是字符串? #1楼 如果要处理文字符号而不是构造函数,则可以使用typeof:. typeof "Hello World"; / ...

最新文章

  1. PHP根据IP获取当前所在地地址
  2. HOWTO:安装包卸载时如何保留部分文件不被卸载
  3. apache 下实现防盗链
  4. 信号处理专业名词术语
  5. 使用secondary sort实现数据关联 完整示例代码
  6. 双线程猜数字 TwoThreadGuessNumber.java
  7. Feature Flag 功能发布控制
  8. The Process class relies on proc_open, which is not available on your PHP installation.
  9. Python学习笔记8—Python函数
  10. Failed to load Idlinux.c32, Boot failed: press any key to retry
  11. abs链目前在哪个平台_ABS链怎么样?
  12. sony相机二次开发sdK C语言,sdk与开放API协议支持二次开发的摄像头
  13. android连接打印机
  14. 中国好生意 经典论述:哈林是来主持的,刘欢是来开家长会的,那英是来唠嗑的,杨坤是来做宣传的,而......
  15. 长生不老:从秦始皇到基因编辑
  16. eclipse plugin
  17. 基于卷积神经网络的高光谱分类 CNN+高光谱+印度松数据集
  18. 区别:过去完成时、现在完成时、一般过去时
  19. 望(dream-coastline 3.0)
  20. 1.2 Probability Theory (下)

热门文章

  1. 我教你怎么玩转git
  2. android stadio open recent 在同一窗口打开
  3. 电脑卡,eclipse Android stadio 卡,什么都卡解决方法
  4. 【Android】使用AIDL传递用户自定义类型数据--附完整示例代码
  5. Android Studio你不知道的调试技巧
  6. Android手机系统adb常用的命令
  7. C语言函数集(十二)
  8. swift_037(Swift之Swift和OC混编)
  9. 单点登录系统实现基于SpringBoot
  10. [HDU] Tr A