gpt4 book ai didi

c++ - AWK 中的大整数是否只有 53 位?

转载 作者:太空狗 更新时间:2023-10-29 11:44:38 27 4
gpt4 key购买 nike

很奇怪,我发现在awk中,大整数看起来只有53位。这是我的例子:

function bits2str(bits,data, mask)
{
if (bits == 0)
return "0"

mask = 1
for (; bits != 0; bits = rshift(bits, 1))
data = (and(bits, mask) ? "1" : "0") data

while ((length(data) % 8) != 0)
data = "0" data

return data
}

BEGIN{
print 32,"\tlshift 48:\t", lshift(32,48), "\t", bits2str(lshift(32,48))
print 429,"\tlshift 48:\t", lshift(429,48), "\t", bits2str(lshift(429,48))
}

输出是:

32  lshift 48:   0   0
429 lshift 48: 3659174697238528 00001101000000000000000000000000000000000000000000000000

但是在c++中,它的输出是:

32 lshift 48: 9007199254740992
429 lshift 48: 120752765008871424

比较两个输出后,我发现awk的只有53位,然后我研究了gawk的源代码(从文件builtin.c, gawk 4.1.1, http://ftp.gnu.org/gnu/gawk/中的3021行开始),但我没有发现对int的特殊操作。那么,是什么原因造成的呢?为什么会这样?

最佳答案

在 AWK 中,所有数字都以 float 形式存储。

来自 Bitwise function :

For all of these functions, first the double precision floating-point value is converted to the widest C unsigned integer type, then the bitwise operation is performed. If the result cannot be represented exactly as a C double, leading nonzero bits are removed one by one until it can be represented exactly. The result is then converted back into a C double.

假设使用 IEEE-754,double 只能表示最大为 253 的整数。

关于c++ - AWK 中的大整数是否只有 53 位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24382014/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com