这个问题的无关理由:
我在调用 Lua 时遇到错误format
:
整数溢出试图存储 -1.#IND
变量type(n)
真的是a number
,我可以format
将它作为一个字符串(即%s
),但它不是一个数字,例如:
print(string.format("value=%s, type=%s", n, type(n)));
对于NaN
值返回:
值=-1.#IND,类型=数字
我想解决这个问题,但我不知道是谁在生成这个NaN
(Lua 没有调试器)。
所以我不得不asserts
在代码中抛出很多,直到我可以确定这个间歇NaN
值的来源。
但是我找不到任何可以捕获它的条件,而 Lua 没有isnan(x)
.
问题:
如何-1.#IND
在 Lua 中测试一个数字?
更新:
我试过:
if (n ~= n) then
print(string.format("NaN: value=%s, type=%s", n, type(n)));
else
print(string.format("value=%s, type=%s", n, type(n)));
end;
它打印
值=-1.#IND,数字
更新二:以防万一我错过了什么,我的实际代码是:
if (oldValue ~= oldValue) then
print(string.format("Is NaN: labelNumber=%d, formatString=\"%s\", oldValue=%s (%s)", labelNumber or 0, formatString or "nil", oldValue or "nil", type(oldValue)));
else
print(string.format("Is not NaN: labelNumber=%d, formatString=\"%s\", oldValue=%s (%s)", labelNumber or 0, formatString or "nil", oldValue or "nil", type(oldValue)));
end;
错误值输出:
不是 NaN:labelNumber=4, formatString="%d", oldValue=-1.#IND (number)
更新三
仍在尝试解决这个问题,我只是注意到现实的荒谬:
function isnan(x)
if type(x) ~= "number" then
return false; --only a number can not be a number
end;
...
end;