lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 2/10/2012 4:18 PM, roman.maire@besonet.ch wrote:
[snip]
angle = 270
originx = 0
originy = 100
distance = 100

x = (originx + (math.cos(math.rad(angle)) * distance))
y = (originy + (math.sin(math.rad(angle)) * distance))
print("x: ".. x ..", y: ".. y)

Output:
-> x: -1.836970198721e-014, y: 0

I was expecting that x should be 0 too.

am I missing something here?

It's near the expected error magnitude. A double has about 16 digits of precision with 53 significand bits. For the calculation you used, each of the following are not perfectly precise so error accumulates:

(1) #define PI (3.14159265358979323846) in lmathlib.c
(2) #define RADIANS_PER_DEGREE (PI/180.0) in lmathlib.c
(3) luaL_checknumber(L, 1)*RADIANS_PER_DEGREE in lmathlib.c
    -- for math.rad(angle)
(4) math.sin(...)
(5) math.sin(... * distance)
(6) (originx + ...)

Each operation adds to the amount of error. So 14-15 digits is about right. But it should be enough for almost everyone, after all, 1000km to 1mm is only a magnitude of 9. As HyperHacker suggests, some Googling and study on the Internet should sort it out...

--
Cheers,
Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia