Lua math.floor返回错误的值

问题描述 投票:0回答:1

。有我的代码:

for k,v in pairs(result) do
  result[k] = math.floor(v*1000)/1000
  if k == 215 then
     print(v, math.floor(v*1000))
  end
end
for k,v in pairs(extra) do
  extra[k] = math.floor(v*1000)/1000
end

哪里

result[215] = 113

但是当我用C ++ Lua-Tinker运行它时,我得到了打印:

113, 112999

这很混乱!

lua floating-point precision
1个回答
0
投票

感谢Egor Skriptunoff,我得到了答案。有一个例子:

local num1 = 100 + 1300/100
print(num1, math.floor(num1))

local num2 = (1 + 13/100) * 100
print(num2, math.floor(num2))

结果是:

113 113
113 112

实际上,num2 = 112.9999999999999 ...,因为13/100.But当使用'print'来显示它时,有一个圆形动作:

local a=112.99999999999 --(the count of 9 is 11)
print(a)
local b=112.999999999999 --(the count of 9 is 12)
print(b)

结果是:

112.99999999999
113
© www.soinside.com 2019 - 2024. All rights reserved.