任意点的准确性问题
Trouble with accuracy at arbitrary points
target="/home/walia6/Math/fib"
os.execute("echo 1 > "..target)
os.execute("echo 1 >> "..target)
while true do
local handle = io.popen("wc -l < "..target)
ct = handle:read("*a")
handle:close()
os.execute("echo "..ct)
tmp=("sed -n "..(ct-1).."p "..target)
--os.execute("echo '"..tmp.."'")
local handle = io.popen(tmp)
pn = handle:read("*a")
handle:close()
tmp=("sed -n "..(ct-0).."p "..target)
--os.execute("echo '"..tmp.."'")
local handle = io.popen(tmp)
cn = handle:read("*a")
handle:close()
os.execute("echo "..(string.format("%.0f",cn+pn)).." >>"..target)
end
以上是我在 Linux 中试验 Lua 时编写的一些代码。我测试 Linux 比测试 Lua.
不过,我注意到我的斐波那契生成器在生成第 78 个数字后变得不准确。这似乎是一个相当随意的数字,所以我想不出为什么它会像某种溢出以外的错误。
有人知道为什么吗?
这很可能是溢出,因为斐波那契数呈指数增长。
在Lua5.3之前,Lua中的所有数字都是双精度浮点数。这意味着最多可以精确表示 52 位的整数。事实上,F(78) = 8944394323791464 可以精确表示,但 F(79) = 14472334024676221 不能。
在具有64位整数的Lua5.3中,可以精确表示最大为F(92) = 754011380474634642的斐波那契数
target="/home/walia6/Math/fib"
os.execute("echo 1 > "..target)
os.execute("echo 1 >> "..target)
while true do
local handle = io.popen("wc -l < "..target)
ct = handle:read("*a")
handle:close()
os.execute("echo "..ct)
tmp=("sed -n "..(ct-1).."p "..target)
--os.execute("echo '"..tmp.."'")
local handle = io.popen(tmp)
pn = handle:read("*a")
handle:close()
tmp=("sed -n "..(ct-0).."p "..target)
--os.execute("echo '"..tmp.."'")
local handle = io.popen(tmp)
cn = handle:read("*a")
handle:close()
os.execute("echo "..(string.format("%.0f",cn+pn)).." >>"..target)
end
以上是我在 Linux 中试验 Lua 时编写的一些代码。我测试 Linux 比测试 Lua.
不过,我注意到我的斐波那契生成器在生成第 78 个数字后变得不准确。这似乎是一个相当随意的数字,所以我想不出为什么它会像某种溢出以外的错误。
有人知道为什么吗?
这很可能是溢出,因为斐波那契数呈指数增长。
在Lua5.3之前,Lua中的所有数字都是双精度浮点数。这意味着最多可以精确表示 52 位的整数。事实上,F(78) = 8944394323791464 可以精确表示,但 F(79) = 14472334024676221 不能。
在具有64位整数的Lua5.3中,可以精确表示最大为F(92) = 754011380474634642的斐波那契数