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

基本值比较评估在Lua中不起作用,lua,evaluation,Lua,Evaluation,我试图比较两个值,它们看起来相等,但仍被评估为不同 我做错了什么?有什么想法吗?我添加了tonumber(),只是为了确保我没有在某处将一转换为字符串 --Check to see if the current health and the target health differ if tonumber( characterStatus.current[ statusColor .. "Health" ] ) ~= tonumber( characterStatus.target[ statu

我试图比较两个值,它们看起来相等,但仍被评估为不同

我做错了什么?有什么想法吗?我添加了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

输出为“Monster Amounts difference~~~~~~~~~~=99=“

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

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来格式化输出

Lua数只是双倍数,不能简单地测试任意的双倍数是否相等


在显示中四舍五入其中一个数字的浮点值,但在tonumber中可能不是?
打印(characterStatus.current[statusColor.“Health”]%1)
打印(characterStatus.target[statusColor.“Health”]%1)
的区别是什么?发生了一些事情。。。其输出为:7.1054253546001e-015,0。我用math.ceil在我的一些计算中使用这个数字。这有什么关系吗?更好的是,我该如何解决这个问题?不管是谁-我都会评论为什么,否则我该如何改进答案?