javascript 如何在 Node.js 中进行 64 位整数运算?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24037751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 02:02:30  来源:igfitidea点击:

How to do 64bit Integer arithmetic in Node.js?

javascriptnode.js

提问by user1502301

Anyone have any suggestions on how to perform arithmetic on 64bit integers in Node.js? The node-int64 package doesn't seem to support that.

有人对如何在 Node.js 中对 64 位整数执行算术有任何建议吗?node-int64 包似乎不支持。

回答by Sarath

Javascript does not support 64 bit integers, because the native number type is a 64-bit double, giving only 53 bits of integer range.

Javascript 不支持 64 位整数,因为本机数字类型是 64 位双精度型,仅给出 53 位整数范围。

You can create arrays of 32-bit numbers (i.e. Uint32Array) but if there were a 64-bit version of those there'd be no way to copy values from it into standalone variables.

您可以创建 32 位数字的数组(即Uint32Array),但如果有 64 位版本的数组,则无法将其中的值复制到独立变量中。

There are some modules around to provide 64bit integer support:

有一些模块可以提供 64 位整数支持:

Maybe your problem can be solved using one of those libraries.

也许您的问题可以使用这些库之一来解决。

回答by Alex Tullenhoff

As of v10.4.0 NodeJS supports the BigInt type natively (see MDN BigInt docs). These support arithmetic operations too.

从 v10.4.0 开始,NodeJS 原生支持 BigInt 类型(参见MDN BigInt 文档)。这些也支持算术运算。