I understand what sin does but I need help using it.
I was testing around and I ran print(math.sin(180)
.
It put out −0.801153. I got confused because I knew sin of 180 is 0.
So I look in the math section in API reference and it says expecting radians. So I do
print(math.sin(math.rad(180)))
and it put out a really small weird number.
So I got confused and I thought maybe its returning in rads? so I do
print(math.deg(math.sin(math.rad(180))))
and it puts out another weird number.
I need to use this function for something but I don’t know how to use it.
Do I have this all confused?
That’s just floating point stuff. I ran it in studio and got 1.2246467991474e-16
, which is around 0.00000000000000012246467991474
.
There is not way to get completely precise number because of so called floating points. You can help yourself with math.floor function to some extent. Otherwise, instead of explaining myself, I found a really comprehensive explanation of floating points by @hiimgoodpack, who is not a member of this forum, at least not under this username. Here is the original post, which can be found on Scripting Helpers.
Roblox uses doubles, which are 64-bit floating point values. Floating point values compromise precision for the space it takes up, meaning that when you have a fractional value, you (usually) won’t get the exact value you wanted. This will, however, not affect any integer with an absolute value less than 2^53. However, Lua manages to hide these floating point errors, but I couldn’t find any documentation of the process. If you want to find more information on this, look at this Wikipedia article. Hope this helps!
Luckily, as you can see, Xe-16 is an extremely small value, really close to zero. Size and accuracy are relative, but when working in Roblox studio, such floating point errors aren’t usually a big problem.
it takes in the radians not the degrees. You would have to pass something like degree * (math.pi/180)
The reason why it is 180 is to do with its domain.
There is no way to calculate the precise value of the sine function without using some kind of infinite sum (which as the name suggests needs to use infinite terms), therefore computers uses very fast and accurate approximations.
I believe roblox still uses the math library provided in c, the exact algorithm can be found here.
http://www.netlib.org/fdlibm/k_sin.
You can always round the result to the nearest thousandth or hundredth if you need it to match an expected result.