JavaScript 中的取模 - 大数
我尝试用JS的模函数进行计算,但没有得到正确的结果(应该是1)。 这是一段硬编码的代码。
var checkSum = 210501700012345678131468;
alert(checkSum % 97);
Result: 66
这里有什么问题吗?
问候, 贝内迪克特
I try to calculate with JS' modulo function, but don't get the right result (which should be 1). Here is a hardcoded piece of code.
var checkSum = 210501700012345678131468;
alert(checkSum % 97);
Result: 66
Whats the problem here?
Regards,
Benedikt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对于 IBAN 计算形式的普通银行帐号,我最终会得到一个包含在字符串数据类型中的非常大的数字。 我必须从这个大数中找到除以 97 后的余数 -> 大数字 % 97。
一旦我将数据类型转换为整数,我就会发生溢出,导致负整数并最终产生错误的剩余值。 当我看到一些冗长的代码片段(也给出了错误的结果)时,我忍不住分享我自己的代码。 学分转到求一个非常大的数与正常数的模
注意
我在这里使用 10 个位置,因为这小于 JavaScript 中最大整数的 15 个(和一些)位置,它会导致一个大于 97 的数字,并且这是一个很好的整数。 前两个论点很重要。
For an IBAN calculation form a normal bankaccount number I end up with a very large number contained in a string datatype. From this large number I have to find the rest when divided by 97 -> large number % 97.
As soon as I convert the datatype to an integer I get an overflow resulting in a negative integer and eventually a wrong rest value. As I saw some verbose pieces of code (which also gave wrong outcome), I could not resist to share my own. Credits go to Finding Modulus of a Very Large Number with a Normal Number
N.B.
I use 10 positions here as this is smaller than the 15 (and some) positions of max integer in JavaScript, it results in a number bigger than 97 and it's a nice round number. The first two arguments matter.
Benedikt 版本的一系列改进:
cRest += '' + cDivident;
是一个错误修复;parseInt(divisor)
可以将两个参数作为字符串传递; 检查末尾的空字符串使其始终返回数值; 添加了 var 语句,因此它不使用全局变量; 将 foreach 转换为旧式 for,以便它可以在使用旧版 Javascript 的浏览器中运行; 修复了cRest == 0;
错误(感谢@Dan.StackOverflow)。A bunch of improvements to Benedikt's version:
cRest += '' + cDivident;
is a bugfix;parseInt(divisor)
makes it possible to pass both arguments as strings; check for empty string at the end makes it always return numerical values; added var statements so it's not using global variables; converted foreach to old-style for so it works in browsers with older Javascript; fixed thecRest == 0;
bug (thanks @Dan.StackOverflow).对于那些只想在 ES6 中复制并粘贴工作(功能)解决方案来检查 IBAN 的人:
您甚至可以将其编写为一行代码。
模运算是在存储实际数字的整数数组上执行的(
divident
,作为字符串应用于函数):这是有效的,因为模在加法、减法和乘法上具有分配性:
转换为 ES5 的 IBAN 函数如下所示:
For those who simply want to copy&paste a working (functional) solution in ES6 to check IBANs:
You could even write it as a one-liner.
The modulo operation is performed on the array of integers storing the actual number (
divident
, applied as string to function):This works because Modulo is distributive over addition, substraction and multiplication:
The IBAN function transpiled to ES5 looks like:
看起来你已经成为这个的受害者:JavaScript 在不丢失精度的情况下可以达到的最大整数值是多少?
只是重申一下另一个线程中的内容:
但是应得的功劳。 吉米在那里得到了公认的答案,因为他做了跑腿工作(好吧,谷歌搜索)。
looks like you've fallen victim to this: What is JavaScript's highest integer value that a Number can go to without losing precision?
just to reiterate what's in the other thread:
But credit where credit is due. Jimmy got the accepted answer over there for doing the legwork (well, googling).
最后,我的解决方案:
Finally, my solution:
Silent Matt 为大整数开发了一个 Javascript 库。 也可以解决这个问题。
Silent Matt has developed a Javascript library for Big Integers. It could solve this issue too.