基本价值比较评估在Lua不起作用

时间:2014-01-03 18:04:42

标签: lua evaluation

我试图比较2个值,它们似乎相等,但仍然被评估为不同。

我做错了什么?有任何想法吗?我添加tonumber()只是为了确保我没有将其中一个转换为字符串。

--Check to see if the current health and the target health differ
if tonumber( characterStatus.current[ statusColor .. "Health" ] ) ~= tonumber( characterStatus.target[ statusColor .. "Health" ] ) then
    --Current and Target Heath amounts differ

    if statusColor == "monster" then print( "\nMonster Amounts Differ  ~~~~~~~~~~=" .. characterStatus.current[ statusColor .. "Health" ] .. characterStatus.target[ statusColor .. "Health" ] .. "=" ) end
end

输出是“怪物量不同~~~~~~~~~~ = 99 =”

2 个答案:

答案 0 :(得分:1)

Lua数字只是双倍数,而且不仅仅是测试任意双打的相等性。

http://floating-point-gui.de/errors/comparison/

答案 1 :(得分:-1)

清理代码以减少拼写错误的风险,并打印两个值之间的差异:

local scCurrent = tonumber(characterStatus.current[ statusColor .. "Health" ])
local scTarget  = tonumber(characterStatus.target [ statusColor .. "Health" ])
if scCurrent ~= scTarget then
    local scDiff = scCurrent - scTarget
    if statusColor == "monster" then 
        print( scCurrent, scTarget, scDiff) 
    end
end

您将看到两个值之间的区别。您可能需要使用string.format来格式化输出。