简体   繁体   English

Javascript 中的 64 位 integer

[英]64bit integer in Javascript

I saw this question on CodeSignal where the return type was supposed to be an array of 64-bit integers .我在 CodeSignal 上看到了这个问题,其中返回类型应该是一个 64 位整数数组 Here's an example return value:这是一个示例返回值:

[23, 34, 65]

The default size of number in JavaScript is 64-bit, which is a decimal value with 53-bit mantissa. JavaScript 中数字的默认大小为 64 位,即尾数为 53 位的十进制值。

Also, I tried this:另外,我试过这个:

 BigInt.UintN(64, 32n)

This too threw the same error as Type mismatch .这也引发了与Type mismatch相同的错误。 I am looking for the right way to convert a number to 64-bit integer — or is this a limitation of the language and I should simply not do algorithms in JavaScript?我正在寻找将数字转换为 64 位integer的正确方法——或者这是语言的限制,我不应该在 JavaScript 中执行算法?

According the documentation to converting number to int64根据文档将数字转换为 int64

While we want to convert any number to 64bit integer, we need to write虽然我们想将任何数字转换为 64 位 integer,但我们需要编写

 // Require, of course > Int64 = require('node-int64') // x's value is what we expect (the decimal value of 0x123456789) > x = new Int64(0x123456789) //and the value of x, is [Int64 value:4886718345 octets:00 00 00 01 23 45 67 89]

Hope it was helpful.希望对您有所帮助。 Thank you谢谢

The requirements are a bit ambiguous.要求有点模棱两可。 The "example return value" is simply an array of numbers, which will not be able to hold a full 64-bit integer, so that looks contradictory to what the wording of the requirement is. “示例返回值”只是一个数字数组,无法容纳完整的 64 位 integer,因此看起来与要求的措辞相矛盾。

In case the example value is wrong and the requirement is correct, there is still some ambiguity.如果示例值错误而要求正确,则仍然存在一些歧义。

It could be that they were expecting a typed 64-bit integer array ( BigInt64Array ):他们可能期待一个类型化的 64 位 integer 数组( BigInt64Array ):

new BigInt64Array([23n, 34n, 65n])

(It's also unclear whether signed or unsigned integers are desired. In case of unsigned, BigUInt64Array would be needed instead.) (还不清楚是否需要有符号整数或无符号整数。如果是无符号整数,则需要BigUInt64Array 。)

It could also be that they were simply expecting an array of BigInt values:也可能是他们只是期待一个BigInt值的数组:

[23n, 34n, 65n]

Or, of course, maybe the requirements are misleading and the example return value is correct, in which case regular numbers are expected (unable to hold 64-bit integers without loss of precision):或者,当然,可能要求具有误导性,并且示例返回值正确的,在这种情况下,需要常规数字(无法在不损失精度的情况下保存 64 位整数):

[23, 34, 65]

Or, perhaps, the requirement is simply to clamp the value to 64 bits and discard the overflow, in which case you'd have to use BigInt.asIntN :或者,也许要求只是将值限制为 64 位并丢弃溢出,在这种情况下,您必须使用BigInt.asIntN

function clamp (array) {
  return array.map(n => BigInt.asIntN(64, n))
}

(In case unsigned integers are expected, you'd need BigInt.asUIntN instead.) (如果需要无符号整数,则需要BigInt.asUIntN 。)

This would however assume an array of BigInt s as input and would also output BigInt s, so it would again not match the shown example return value which had regular numbers.然而,这将假设一个BigInt的数组作为输入,并且还将 output BigInt s,因此它将再次与具有常规数字的所示示例返回值不匹配。

In summary, it seems that the question is either wrong in itself (either in its wording or in its example) or it may be simply impossible to do in JavaScript.总之,似乎这个问题本身就是错误的(无论是在其措辞中还是在其示例中),或者在 JavaScript 中根本不可能做到。 There is not enough information provided to give a definite answer.没有提供足够的信息来给出明确的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM