Page 1 of 1

cosine and sine values

Posted: Fri Sep 01, 2017 10:26 pm
by jpott
ok I'm missing something obvious, obviously. But I'm sure someone can quickly tell me what the issue is.
this code:

Code: Select all

  angle = 0
  print("angle: "..angle.." cos: "..math.cos(angle).." sin: "..math.sin(angle))
  angle = 90
  print("angle: "..angle.." cos: "..math.cos(angle).." sin: "..math.sin(angle))
  angle = 180
  print("angle: "..angle.." cos: "..math.cos(angle).." sin: "..math.sin(angle))
  
outputs this:
angle: 0 cos: 1 sin: 0
angle: 90 cos: -0.44807361612917 sin: 0.89399666360056
angle: 180 cos: -0.59846006905786 sin: -0.80115263573383

shouldn't it be:
angle: 0 cos: 1 sin: 0
angle: 90 cos: 0 sin: 1
angle: 180 cos: 1 sin: 0
?

Re: cosine and sine values

Posted: Fri Sep 01, 2017 10:47 pm
by drunken_munki
Angles in those functions are in radians.

http://lua-users.org/wiki/MathLibraryTutorial

Re: cosine and sine values

Posted: Fri Sep 01, 2017 11:32 pm
by zorg

Code: Select all

a = 0 --math.rad(0)
print(math.cos(a), math.sin(a))
--  1.0,  0.0
a = math.rad(90)
print(math.cos(a), math.sin(a))
--  0.0,  1.0
a = math.rad(180)
print(math.cos(a), math.sin(a))
-- -1.0,  0.0
a = math.rad(270)
print(math.cos(a), math.sin(a))
--  0.0, -1.0
The zeroes might be almost nearly zero though since floating point precision issues may arise.

Re: cosine and sine values

Posted: Sat Sep 02, 2017 3:47 am
by raidho36
Technically those functions do not even work on angles, they take double the area of a unit circle sector as an argument. Because it is a unit circle, its sector area happens to coincide with angle of that sector, by factor of two. Hyperbolic trigonometric functions take double the area of a hyperbola sector.