Advertisement
Oysi

Tangent function in Lua

Oct 4th, 2013
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.17 KB | None | 0 0
  1.  
  2. local B = {
  3.     [2] = 1/6; -- not used in the thing
  4.     [4] = -1/30;
  5.     [6] = 1/42;
  6.     [8] = -1/30;
  7.     [10] = 5/66;
  8.     [12] = -691/2730;
  9.     [14] = 7/6;
  10.     [16] = -3617/510;
  11.     [18] = 43867/798;
  12.     [20] = -174611/330;
  13.     [22] = 854513/138;
  14.     [24] = -236364091/2730;
  15.     [26] = 8553103/6;
  16.     [28] = -23749461029/870;
  17.     [30] = 8615841276005/14322;
  18.     [32] = -7709321041217/510;
  19.     [34] = 2577687858367/6;
  20.     [36] = -26315271553053477373/1919190;
  21.     [38] = 2929993913841559/6;
  22.     [40] = -261082718496449122051/13530;
  23.     [42] = 1520097643918070802691/1806;
  24.     [44] = -27833269579301024235023/690;
  25.     [46] = 596451111593912163277961/282;
  26.     [48] = -5609403368997817686249127547/46410;
  27.     [50] = 495057205241079648212477525/66;
  28.     [52] = -801165718135489957347924991853/1590;
  29.     [54] = 29149963634884862421418123812691/798;
  30.     [56] = -2479392929313226753685415739663229/870;
  31.     [58] = 84483613348880041862046775994036021/354;
  32.     [60] = -1215233140483755572040304994079820246041491/56786730;
  33.     [62] = 12300585434086858541953039857403386151/6;
  34.     [64] = -106783830147866529886385444979142647942017/510;
  35.     [66] = 1472600022126335654051619428551932342241899101/64722;
  36.     [68] = -78773130858718728141909149208474606244347001/30;
  37.     [70] = 1505381347333367003803076567377857208511438160235/4686;
  38.     [72] = -5827954961669944110438277244641067365282488301844260429/140100870;
  39.     [74] = 34152417289221168014330073731472635186688307783087/6;
  40.     [76] = -24655088825935372707687196040585199904365267828865801/30;
  41.     [78] = 414846365575400828295179035549542073492199375372400483487/3318;
  42.     [80] = -4603784299479457646935574969019046849794257872751288919656867/230010;
  43.     [82] = 1677014149185145836823154509786269900207736027570253414881613/498;
  44.     [84] = -2024576195935290360231131160111731009989917391198090877281083932477/3404310;
  45.     [86] = 660714619417678653573847847426261496277830686653388931761996983/6;
  46.     [88] = -1311426488674017507995511424019311843345750275572028644296919890574047/61410;
  47.     [90] = 1179057279021082799884123351249215083775254949669647116231545215727922535/272118;
  48.     [92] = -1295585948207537527989427828538576749659341483719435143023316326829946247/1410;
  49.     [94] = 1220813806579744469607301679413201203958508415202696621436215105284649447/6;
  50.     [96] = -211600449597266513097597728109824233673043954389060234150638733420050668349987259/4501770;
  51.     [98] = 67908260672905495624051117546403605607342195728504487509073961249992947058239/6;
  52.     [100] = -94598037819122125295227433069493721872702841533066936133385696204311395415197247711/33330;
  53. }
  54.  
  55. function factorial(x)
  56.     local n = 1
  57.     for i = 2, x do
  58.         n = n * i
  59.     end
  60.     return n
  61. end
  62.  
  63. function tan(x)
  64.     local final = x
  65.     for k = 2, 50 do
  66.         local nom = ((-1)^(k-1)) * 2^(2*k) * ((2^(2*k))-1) * B[2*k] / factorial(2*k)
  67.         final = final + nom*x^(2*k - 1)
  68.     end
  69.     return final
  70. end
  71.  
  72. local pi = math.pi
  73. function tangent(x)
  74.     x = x % (math.pi*2)
  75.     if 0 <= x and x <= pi/2 then return tan(x)
  76.     elseif pi/2 <= x and x <= pi then return -tan(pi - x)
  77.     elseif pi <= x and x <= 3*pi/2 then return tan(x - pi)
  78.     elseif 3*pi/2 <= x and x <= 2*pi then return -tan(2*pi - x)
  79.     end
  80.     return 0
  81. end
  82.  
  83. for i = 0, 6.3, 0.1 do
  84.     local tan1 = math.tan(i)
  85.     local tan2 = tangent(i)
  86.     print(i, math.floor((tan1 - tan2)*1000 + 0.5)/1000, tan1, tan2)
  87. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement