math.sin and math.cos error

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
yegorf1
Prole
Posts: 19
Joined: Tue Dec 25, 2012 7:46 pm

math.sin and math.cos error

Post by yegorf1 »

Hi, all I'm running love 0.8.0 and when I launched this:

Code: Select all

print('math.sin 0: ' .. math.sin(0))
print('math.sin 90: ' .. math.sin(math.pi / 2))
print('math.sin 180: ' .. math.sin(math.pi))
print('math.sin 270: ' .. math.sin(math.pi / 2 * 3))
print('math.cos 0: ' .. math.cos(0))
print('math.cos 90: ' .. math.cos(math.pi / 2))
print('math.cos 180: ' .. math.cos(math.pi))
print('math.cos 270: ' .. math.cos(math.pi / 2 * 3))
I see this:

Code: Select all

math.sin 0: 0
math.sin 90: 1
math.sin 180: 1.2246467991474e-16
math.sin 270: -1
math.cos 0: 1
math.cos 90: 6.1232339957368e-17
math.cos 180: -1
math.cos 270: -1.836970198721e-16
What's the error?
Thanks (:
User avatar
markgo
Party member
Posts: 190
Joined: Sat Jan 05, 2013 12:21 am
Location: USA

Re: math.sin and math.cos error

Post by markgo »

bekey
Party member
Posts: 255
Joined: Tue Sep 03, 2013 6:27 pm

[]

Post by bekey »

-snip-
Last edited by bekey on Fri Jan 24, 2014 1:41 am, edited 1 time in total.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: math.sin and math.cos error

Post by Robin »

No, Lua trig functions work with radians.
Help us help you: attach a .love.
User avatar
XCaptain
Prole
Posts: 23
Joined: Tue Apr 09, 2013 11:18 am

Re: math.sin and math.cos error

Post by XCaptain »

shouldn't there be some conversion from radians to degrees too?
math.rad(degrees)
This will return x degrees in radians.
math.deg(radians)
This is just the opposite.
bekey
Party member
Posts: 255
Joined: Tue Sep 03, 2013 6:27 pm

[]

Post by bekey »

-snip-
Last edited by bekey on Fri Jan 24, 2014 1:40 am, edited 2 times in total.
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: math.sin and math.cos error

Post by Plu »

Short answer: rounding errors.

This: 1.2246467991474e-16
Is this: 0.00000000000000012246467991474

Which is 0 for all practical purposes.

I'm guessing (but I'm not sure) that even Lua will agree and say it equals zero if you compare with it. Otherwise, you'd have to use a bit of floating point comparisons (ie; sin(x) <= 0.0000001) or make an exact comparison against the input for the function before you calculate the sin value of it.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: math.sin and math.cos error

Post by Robin »

Plu wrote:I'm guessing (but I'm not sure) that even Lua will agree and say it equals zero if you compare with it.
Nope:

Code: Select all

> print(1.2246467991474e-16 == 0)
false
Floating point comparisons are finicky, and you usually want to compare them within an epsilon (basically, a very small value that is the margin of error you choose).
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Google [Bot], rabbitboots and 212 guests