Error Bars for Lua Maths in Warcraft 3

Status
Not open for further replies.
Level 1
Joined
May 30, 2012
Messages
2
Hi.

I was testing some Lua maths in Warcraft 3 involving quadratic and apparently the obtained numerical value differs depending on whether math.sqrt or "^(1/2)" is used. Mathematically, they should be equivalent, thus, the same answer should yield. However, there seem to be a large difference in accuracy depending on which one of them is used.

So the computed answer deviated approximately 2.2e-5 to 2.3e-5 from the real answer.
Lua:
function maths()
    a = -1
    b = -4
    c = 7
    do
        local a2 = 2*a
        local d = math.sqrt(b^2 - 4*a*c)
        print("The value of d is "..d)
        x1 = (-b + d)/a2
        x2 = (-b - d)/a2
    end
    print("The roots are"..x1.." and "..x2)
    ans1 = -5.31662479 --The answers that should be obtained
    ans2 = 1.31662479
    D1 = math.abs(ans1 - x1)
    D2 = math.abs(ans2 -x2)
    print("The differences are "..D1.." and "..D2)
end

Now the computed answer deviated approximately 1.4e-6 to 1.5e-6 from the real answer.
Lua:
function maths()
    a = -1
    b = -4
    c = 7
    do
        local a2 = 2*a
        local d = (b^2 - 4*a*c)^(1/2) -- Note that ^(1/2) is now used instead of math.sqrt
        print("The value of d is "..d)
        x1 = (-b + d)/a2
        x2 = (-b - d)/a2
    end
    print("The roots are"..x1.." and "..x2)
    ans1 = -5.31662479 --The answers that should be obtained
    ans2 = 1.31662479
    D1 = math.abs(ans1 - x1)
    D2 = math.abs(ans2 -x2)
    print("The differences are "..D1.." and "..D2)
end

There is no deviation from the true answer at all when both computation method are utilised in Lua but when it is used in Warcraft 3 Worldeditor it yields different results. Although a deviation of e-5 or e-6 shouldn't dramatically affect any coding that requires sqrt but it is just interesting how different results yield. :con:

Edit: I guess it is just some floating point error issue and that two methods are considered different in war3.
 
Last edited:
Status
Not open for further replies.
Top