在Go的常量规范中 ,提到:
数字常量表示任意精度的精确值,不会溢出。
所以我试过了
const VeryVeryBigNumber = 1 << 200
它的工作原理。 但是,我可以尝试的最大移位数是511,使用512会抛出:
shift count too large: 512 。
512代表什么? 我无意使用它,我只是想知道为什么它在我的机器中限制为511(我使用ubuntu 64位并且去1.9.2)?
谢谢
In Go's constant specification, it is mentioned that:
Numeric constants represent exact values of arbitrary precision and do not overflow.
So I tried
const VeryVeryBigNumber = 1 << 200
and it works. However, the biggest shift count I could try is 511 and using 512 will throw:
shift count too large: 512.
What does 512 represents? I have no intention to use it, I just want to know why is it limited to 511 in my machine (I'm using ubuntu 64 bit and go 1.9.2)?
Thanks
最满意答案
512是任意限制。 规范唯一说的是:
实现限制:虽然数字常量在语言中具有任意精度,但编译器可以使用精度有限的内部表示来实现它们。 也就是说,每个实施必须:
表示至少256位的整数常量。
不幸的是, 围绕限制的评论并没有给出理由。
在某些时候,必须使用限制。 我建议坚持要求的256。
512 is kind of an arbitrary limit. The only thing the spec says is:
Implementation restriction: Although numeric constants have arbitrary precision in the language, a compiler may implement them using an internal representation with limited precision. That said, every implementation must:
Represent integer constants with at least 256 bits.
Unfortunately, the comments around the limits don't give a reason.
At some point, a limit has to be used. I would recommend sticking to the required 256.
更多推荐
发布评论