无法将计算出的号码与身份证号码进行比较?
Unable to compare calculated number with idential number?
我正在研究向量 class。在测试向量 class 时,我 运行 将单位向量的大小与 1.
进行比较时失败的测试用例
当比较它们时,它们似乎都是 1
,那么什么会导致此测试失败?
我已经尽可能多地切入问题的根本原因。我可以将数字转换为字符串并进行比较,但这只会修复测试用例,导致问题稍后再次出现。
我正在使用 Lua 5.1 解释器(以确定根本原因),与 LuaRocks.
捆绑在一起
local v = 0.70710678118655^2 + 0.70710678118655^2
print(v, v == 1)
v == 1
应该是 true
,而不是 false
。
当然这是Is floating point math broken的例子。
更多解释:执行这段代码
local half = 0.50
local doubleHalf = half + half
if doubleHalf == 1 then
print('doubleHalf is one')
else
print('doubleHalf differs '.. doubleHalf - 1 ..'from one')
end
--> doubleHalf is one
local rootHalf = math.sqrt(half)
print('rootHalf is a '.. type(rootHalf) ..' with value', rootHalf)
--> rootHalf is a number with value 0.70710678118654
local square = rootHalf^2
print('square is a '.. type(square) ..' with value', square)
--> square is a number with value 0.5
if square == half then
print('square is half')
else
print('square differs '.. square - half ..' from half')
end
--> square differs 1.110223E-16 from half
vector_length = square + square
if vector_length == 1 then
print('vector length is one')
else
print('vector length differs '.. vector_length - 1 ..' from one')
end
--> vector length differs 2.220446E-16 from one
任何使用值的计算机都将以有限的精度进行计算。
将浮点数转换为字符串时,Lua 四舍五入为仅 15 位有效数字。尝试将其设为 17 位以获得准确的表示。
local v = 0.70710678118655^2 + 0.70710678118655^2
print(("%.17g"):format(v), v == 1)
输出:
1.0000000000000071 false
我正在研究向量 class。在测试向量 class 时,我 运行 将单位向量的大小与 1.
进行比较时失败的测试用例当比较它们时,它们似乎都是 1
,那么什么会导致此测试失败?
我已经尽可能多地切入问题的根本原因。我可以将数字转换为字符串并进行比较,但这只会修复测试用例,导致问题稍后再次出现。 我正在使用 Lua 5.1 解释器(以确定根本原因),与 LuaRocks.
捆绑在一起local v = 0.70710678118655^2 + 0.70710678118655^2
print(v, v == 1)
v == 1
应该是 true
,而不是 false
。
当然这是Is floating point math broken的例子。
更多解释:执行这段代码
local half = 0.50
local doubleHalf = half + half
if doubleHalf == 1 then
print('doubleHalf is one')
else
print('doubleHalf differs '.. doubleHalf - 1 ..'from one')
end
--> doubleHalf is one
local rootHalf = math.sqrt(half)
print('rootHalf is a '.. type(rootHalf) ..' with value', rootHalf)
--> rootHalf is a number with value 0.70710678118654
local square = rootHalf^2
print('square is a '.. type(square) ..' with value', square)
--> square is a number with value 0.5
if square == half then
print('square is half')
else
print('square differs '.. square - half ..' from half')
end
--> square differs 1.110223E-16 from half
vector_length = square + square
if vector_length == 1 then
print('vector length is one')
else
print('vector length differs '.. vector_length - 1 ..' from one')
end
--> vector length differs 2.220446E-16 from one
任何使用值的计算机都将以有限的精度进行计算。
将浮点数转换为字符串时,Lua 四舍五入为仅 15 位有效数字。尝试将其设为 17 位以获得准确的表示。
local v = 0.70710678118655^2 + 0.70710678118655^2
print(("%.17g"):format(v), v == 1)
输出:
1.0000000000000071 false