lua-users home
lua-l archive

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


Thanks for the clarification. I'm aware that an error is to be expected with floating point numbers. I just was surprised that it was so big (nearly two units on a 100 units distance), but now I think I'm seeing my misconception. Sorry for the noise. Tomorrow I'll bring a Rubber Duck to the office
http://c2.com/cgi/wiki?RubberDucking

Kind regards,

Roman

>---- Original Message ----
>From: KHMan
>To: "Lua mailing list"
>Sent: Fr, Feb 10, 2012, 10:14 AM
>Subject: Re: Deviation in math functions
>
>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