Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is that the outputs of the sin function is different from the calculator?

Asked by 8 years ago
Edited 8 years ago
print(math.sin(math.pi / 2))
print(math.sin(math.pi))
print(math.sin(3 * math.pi / 2))
print(math.sin(2 * math.pi))

I am learning trigonometry on Khan Academy because I'm not at the age when they teach trig in school. So when I put these values in here is what I get from the console.

1 <--- This value is what I expected 1.2246467991474e-016 <--- I was expecting 0 -1 <--- This value is what I expected -2.4492935982947e-016 <--- I was expecting 0

Here is what I get from my calculator..

1 0 -1 0

Can someone tell me why this happens in ROBLOX?

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago
Edited 8 years ago

You're getting almost zero. That's scientific notation, with an exponent of -16. The first number, written out in full, is 0.00000000000000012246467991474 -- almost zero.

You get that number because math.pi isn't exact, it's a (very good) approximation. So math.sin(math.pi) gives you a (very good) approximation of 0.

Your calculator actually gets a similarly "fuzzy", but because it's meant for humans, it rounds it out a bit when it shows it to you. This isn't acceptable for Lua because unrequested rounding violates the requirements of math set by IEEE.

Ad

Answer this question