MongoDB的NumberLong只有54位签名?
MongoDB 的 NumberLong 真的是 64 位有符号整数吗?真的是 64 位吗?
MongoDB 的 NumberLong 据说是一个 64 位有符号整数,这意味着我们可以使用 -2^63 <= x <= 2^63-1,其中 x 是一个 NumberLong。 但是,对 NumberLong(x) 加 1 或减 1 不会返回 x <= -2^54 或 x >= 2^54 的预期值,但会返回 -2^53 <= 的正确值x <= 2^53。
因此,可靠的 NumberLong 数字似乎是 54 位有符号整数。
这是为什么?
我做错了什么吗?
来自 mongo shell 的示例:
> NumberLong( Math.pow(2,54) )
NumberLong("18014398509481984") // Expected
> NumberLong( Math.pow(2,54)-1 )
NumberLong("18014398509481984") // **NOT** Expected
> NumberLong( -Math.pow(2,54) )
NumberLong("-18014398509481984") // Expected
> NumberLong( -Math.pow(2,54)+1 )
NumberLong("-18014398509481984") // **NOT** Expected
> NumberLong( Math.pow(2,53) )
NumberLong("9007199254740992") // Expected
> NumberLong( Math.pow(2,53)-1 )
NumberLong("9007199254740991") // Expected
> NumberLong( -Math.pow(2,53) )
NumberLong("-9007199254740992") // Expected
> NumberLong( -Math.pow(2,53)+1 )
NumberLong("-9007199254740991") // Expected
使用 MongoDB 2.0.0
Is a MongoDB's NumberLong really 64 bit signed integer really 64 bit?
MongoDB's NumberLong is said to be a 64 bit signed integer, which should mean we can play with -2^63 <= x <= 2^63-1, where x is a NumberLong.
However, adding 1 or subtracting 1 from a NumberLong(x) does not return the expected value for x <= -2^54 or x >= 2^54, but correct values are returned for -2^53 <= x <= 2^53.
The reliable NumberLong numbers therefor seem to be 54 bit signed integers.
Why is this?
Am I doing someting wrong?
Sample from the mongo shell:
> NumberLong( Math.pow(2,54) )
NumberLong("18014398509481984") // Expected
> NumberLong( Math.pow(2,54)-1 )
NumberLong("18014398509481984") // **NOT** Expected
> NumberLong( -Math.pow(2,54) )
NumberLong("-18014398509481984") // Expected
> NumberLong( -Math.pow(2,54)+1 )
NumberLong("-18014398509481984") // **NOT** Expected
> NumberLong( Math.pow(2,53) )
NumberLong("9007199254740992") // Expected
> NumberLong( Math.pow(2,53)-1 )
NumberLong("9007199254740991") // Expected
> NumberLong( -Math.pow(2,53) )
NumberLong("-9007199254740992") // Expected
> NumberLong( -Math.pow(2,53)+1 )
NumberLong("-9007199254740991") // Expected
Using MongoDB 2.0.0
哇,这真是令人费解。显然这里发生了某种舍入错误。
这可能是 shell 运行的 JavaScript 引擎出了问题,而不是 MongoDB 本身。例如,检查一下 -
$inc
工作正常:不过,您必须小心。如果您仅使用普通文字
1
,它会将类型转换为数字,然后破坏$inc
:即使您返回
$ inc
与NumberLong("1")
,它仍然损坏:绝对要记住。
Wow, this is puzzling. Clearly there is some sort of rounding error happening here.
This is probably something wrong with the JavaScript engine that the shell runs in, rather than MongoDB itself. Check this out, for example--
$inc
works fine:You have to be careful, though. If you use just a normal literal
1
, it will convert the type to a Number, which then breaks the$inc
:And even if you go back to
$inc
withNumberLong("1")
, it's still broken:Definitely good to keep in mind.