lua中float与int判断时不相等

以下是问题代码:

此行:if curPerChangeNum >= maxPerChangeNum then 有错误,没有注意浮点数的问题

local function startIncrease(logonLayer, srcPro, targetPro, perCall, overCall)
    local currentPro = srcPro
    local frameChangeV = (targetPro - srcPro)/10
    print("!!!srcPro, targetPro, frameChangeV --- ", srcPro, targetPro, frameChangeV)
    
    local curPerChangeNum = 0
    local maxPerChangeNum = 1
    
    -- self:setProgressInfo(srcPro)
    local m_increaseTimer = nil
    m_increaseTimer = schedule(logonLayer,
        function()
            currentPro = currentPro + frameChangeV
            curPerChangeNum = curPerChangeNum + frameChangeV
            if curPerChangeNum >= maxPerChangeNum then
                curPerChangeNum = math.max(0, curPerChangeNum - maxPerChangeNum)
                print("!!! curPerChangeNum  ", curPerChangeNum)
                if perCall then
                    perCall(math.floor(currentPro))
                end
            end
            if currentPro >= targetPro then
                print("!!! currentPro  ", currentPro)
                if m_increaseTimer ~= nil then
                    logonLayer:stopAction(m_increaseTimer)
                    m_increaseTimer = nil
                end
                if overCall then
                    overCall()
                end
            else
                -- self:setProgressInfo(currentPro)
            end
        end,
        1
    )
end

 探讨以下例子:

local intA = 21
local floatA = 0.07*300
local isNumberEqual = tonumber(21) == tonumber(0.07*300) -- false
local isStringEqual = tostring(21) == tostring(0.07*300) -- true
local int_float = 21 - 0.07*300 -- -3.5527136788005e-15
local float_int = 0.07*300 - 21 -- 3.5527136788005e-15

local isIntBigger = int_float > 0 -- false
local isFloatBigger = float_int > 0 -- true

-- 21    21    false    true    -3.5527136788005e-15    3.5527136788005e-15    false    true
print(intA, floatA, isNumberEqual, isStringEqual, int_float, float_int, isIntBigger, isFloatBigger)

可以看出:

看似相等的两个正数,float数比int数大一点

所以,不能直接用于相等判断。在使用大于或者小于判断时也要注意。

进一步探讨以下例子:

print(100*0.01 == 1) -- true
print(100*0.02 == 2) -- true
print(100*0.03 == 3) -- true
print(100*0.04 == 4) -- true
print(100*0.05 == 5) -- true
print(100*0.06 == 6) -- true
print(100*0.07 == 7) -- false
print(100*0.08 == 8) -- true
print(100*0.09 == 9) -- true
print(100*0.1 == 10) -- true
print(100*0.11 == 11) -- true
print(100*0.12 == 12) -- true
print(100*0.13 == 13) -- true
print(100*0.14 == 14) -- false
print(100*0.15 == 15) -- true

print(300*0.07 == 21) -- false

可以隐约总结出:

因为不确定性(但也有一定不确定性的规律,上边例子中7的倍数,结果就不准确)

一定要规避此类情况的发生:

1、有浮点数时,用tostring去判断大小和相等

2、有浮点数时,两个数,相减再取绝对值,小于某个比较小的数(比如0.0001)来判断

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值