fork download
  1. /*****************************************************************
  2. Name :
  3. Date : 2018/02/07
  4. By : CharlotteHonG
  5. Final: 2018/02/07
  6. *****************************************************************/
  7. #include <iostream>
  8. #include <cmath>
  9. #include <iomanip>
  10. using namespace std;
  11. #define M_PI 3.14159265358979323846
  12.  
  13. // 快速 atan2 算法
  14. float fastAtan2f(float dy, float dx){
  15. static const float atan2_p1 = 0.9997878412794807f*(float)(180/M_PI);
  16. static const float atan2_p3 = -0.3258083974640975f*(float)(180/M_PI);
  17. static const float atan2_p5 = 0.1555786518463281f*(float)(180/M_PI);
  18. static const float atan2_p7 = -0.04432655554792128f*(float)(180/M_PI);
  19. static const float atan2_DBL_EPSILON = 2.2204460492503131e-016;
  20.  
  21. float ax = std::abs(dx), ay = std::abs(dy);
  22. float a, c, c2;
  23. if (ax >= ay) {
  24. c = ay/(ax + static_cast<float>(atan2_DBL_EPSILON));
  25. c2 = c*c;
  26. a = (((atan2_p7*c2 + atan2_p5)*c2 + atan2_p3)*c2 + atan2_p1)*c;
  27. } else {
  28. c = ax/(ay + static_cast<float>(atan2_DBL_EPSILON));
  29. c2 = c*c;
  30. a = 90.f - (((atan2_p7*c2 + atan2_p5)*c2 + atan2_p3)*c2 + atan2_p1)*c;
  31. }
  32. if (dx < 0)
  33. a = 180.f - a;
  34. if (dy < 0)
  35. a = 360.f - a;
  36. return a;
  37. }
  38. float fastAtan2f_rad(float dy, float dx){
  39. static const float atan2_p1 = 0.9997878412794807f;
  40. static const float atan2_p3 = -0.3258083974640975f;
  41. static const float atan2_p5 = 0.1555786518463281f;
  42. static const float atan2_p7 = -0.04432655554792128f;
  43. static const float atan2_DBL_EPSILON = 2.2204460492503131e-016;
  44.  
  45. float ax = std::abs(dx), ay = std::abs(dy);
  46. float a, c, c2;
  47. if (ax >= ay) {
  48. c = ay/(ax + static_cast<float>(atan2_DBL_EPSILON));
  49. c2 = c*c;
  50. a = (((atan2_p7*c2 + atan2_p5)*c2 + atan2_p3)*c2 + atan2_p1)*c;
  51. } else {
  52. c = ax/(ay + static_cast<float>(atan2_DBL_EPSILON));
  53. c2 = c*c;
  54. a = M_PI/0.5 - (((atan2_p7*c2 + atan2_p5)*c2 + atan2_p3)*c2 + atan2_p1)*c;
  55. }
  56. if (dx < 0)
  57. a = M_PI - a;
  58. if (dy < 0)
  59. a = M_PI*2.0 - a;
  60. return a;
  61. }
  62. // 快速 atan 算法
  63. float fastAtanf(float dy){
  64. static const float atan2_p1 = 0.9997878412794807f*(float)(180/M_PI);
  65. static const float atan2_p3 = -0.3258083974640975f*(float)(180/M_PI);
  66. static const float atan2_p5 = 0.1555786518463281f*(float)(180/M_PI);
  67. static const float atan2_p7 = -0.04432655554792128f*(float)(180/M_PI);
  68. static const float atan2_DBL_EPSILON = 2.2204460492503131e-016;
  69.  
  70. float ax = 1.0, ay = std::abs(dy);
  71. float a, c, c2;
  72. if (ax >= ay) {
  73. c = ay/(ax + static_cast<float>(atan2_DBL_EPSILON));
  74. c2 = c*c;
  75. a = (((atan2_p7*c2 + atan2_p5)*c2 + atan2_p3)*c2 + atan2_p1)*c;
  76. } else {
  77. c = ax/(ay + static_cast<float>(atan2_DBL_EPSILON));
  78. c2 = c*c;
  79. a = 90.f - (((atan2_p7*c2 + atan2_p5)*c2 + atan2_p3)*c2 + atan2_p1)*c;
  80. }
  81.  
  82. if (dy < 0)
  83. a = - a;
  84.  
  85. return a;
  86. }
  87. float fastAtanf_rad(float dy){
  88. static const float atan2_p1 = 0.9997878412794807f;
  89. static const float atan2_p3 = -0.3258083974640975f;
  90. static const float atan2_p5 = 0.1555786518463281f;
  91. static const float atan2_p7 = -0.04432655554792128f;
  92. static const float atan2_DBL_EPSILON = 2.2204460492503131e-016;
  93.  
  94. float ax = 1.0, ay = std::abs(dy);
  95. float a, c, c2;
  96. if (ax >= ay) {
  97. c = ay/(ax + static_cast<float>(atan2_DBL_EPSILON));
  98. c2 = c*c;
  99. a = (((atan2_p7*c2 + atan2_p5)*c2 + atan2_p3)*c2 + atan2_p1)*c;
  100. } else {
  101. c = ax/(ay + static_cast<float>(atan2_DBL_EPSILON));
  102. c2 = c*c;
  103. a = M_PI*0.5 - (((atan2_p7*c2 + atan2_p5)*c2 + atan2_p3)*c2 + atan2_p1)*c;
  104. }
  105.  
  106. if (dy < 0)
  107. a = - a;
  108.  
  109. return a;
  110. }
  111.  
  112. double rounding(double num, int index)
  113. {
  114. bool isNegative = false; // whether is negative number or not
  115.  
  116. if(num < 0) // if this number is negative, then convert to positive number
  117. {
  118. isNegative = true;
  119. num = -num;
  120. }
  121.  
  122. if(index >= 0)
  123. {
  124. int multiplier;
  125. multiplier = pow(10, index);
  126. num = (int)(num * multiplier + 0.5) / (multiplier * 1.0);
  127. }
  128.  
  129. if(isNegative) // if this number is negative, then convert to negative number
  130. {
  131. num = -num;
  132. }
  133.  
  134. return num;
  135. }
  136.  
  137. void test_fast(int i) {
  138.  
  139. int corn = i;
  140. float val = corn *M_PI/180.0;
  141. float test = tan(val);
  142.  
  143. float theta2 = atan(test) *180.0/M_PI;
  144. float theta3 = fastAtanf_rad(test) *180.0/M_PI;
  145.  
  146. int idx = 2;
  147. if( rounding(theta2, idx) != rounding(theta3, idx)) {
  148. cout << fixed << setprecision(2) << "atan- cmath/fast = (" << theta2 << ", " << theta3 << ")" << endl;
  149. }
  150. //cout << fixed << setprecision(2) << "atan- cmath/fast = (" << theta2 << ", " << theta3 << ")" << endl;
  151. }
  152. //================================================================
  153. int main(int argc, char const *argv[]){
  154.  
  155. for(int i = -720; i < 720; ++i) {
  156. test_fast(i);
  157. }
  158. return 0;
  159. }
  160. //====================================================================================
  161.  
Success #stdin #stdout 0s 4224KB
stdin
Standard input is empty
stdout
atan- cmath/fast = (32.00, 31.99)
atan- cmath/fast = (33.00, 32.99)
atan- cmath/fast = (34.00, 33.99)
atan- cmath/fast = (35.00, 34.99)
atan- cmath/fast = (36.00, 35.99)
atan- cmath/fast = (37.00, 36.99)
atan- cmath/fast = (41.00, 41.01)
atan- cmath/fast = (42.00, 42.01)
atan- cmath/fast = (43.00, 43.01)
atan- cmath/fast = (45.00, 44.99)
atan- cmath/fast = (47.00, 46.99)
atan- cmath/fast = (48.00, 47.99)
atan- cmath/fast = (49.00, 48.99)
atan- cmath/fast = (53.00, 53.01)
atan- cmath/fast = (54.00, 54.01)
atan- cmath/fast = (55.00, 55.01)
atan- cmath/fast = (56.00, 56.01)
atan- cmath/fast = (57.00, 57.01)
atan- cmath/fast = (58.00, 58.01)
atan- cmath/fast = (-58.00, -58.01)
atan- cmath/fast = (-57.00, -57.01)
atan- cmath/fast = (-56.00, -56.01)
atan- cmath/fast = (-55.00, -55.01)
atan- cmath/fast = (-54.00, -54.01)
atan- cmath/fast = (-53.00, -53.01)
atan- cmath/fast = (-49.00, -48.99)
atan- cmath/fast = (-48.00, -47.99)
atan- cmath/fast = (-47.00, -46.99)
atan- cmath/fast = (-45.00, -45.01)
atan- cmath/fast = (-43.00, -43.01)
atan- cmath/fast = (-42.00, -42.01)
atan- cmath/fast = (-41.00, -41.01)
atan- cmath/fast = (-37.00, -36.99)
atan- cmath/fast = (-36.00, -35.99)
atan- cmath/fast = (-35.00, -34.99)
atan- cmath/fast = (-34.00, -33.99)
atan- cmath/fast = (-33.00, -32.99)
atan- cmath/fast = (-32.00, -31.99)
atan- cmath/fast = (32.00, 31.99)
atan- cmath/fast = (33.00, 32.99)
atan- cmath/fast = (34.00, 33.99)
atan- cmath/fast = (35.00, 34.99)
atan- cmath/fast = (36.00, 35.99)
atan- cmath/fast = (37.00, 36.99)
atan- cmath/fast = (41.00, 41.01)
atan- cmath/fast = (42.00, 42.01)
atan- cmath/fast = (43.00, 43.01)
atan- cmath/fast = (45.00, 45.01)
atan- cmath/fast = (47.00, 46.99)
atan- cmath/fast = (48.00, 47.99)
atan- cmath/fast = (49.00, 48.99)
atan- cmath/fast = (53.00, 53.01)
atan- cmath/fast = (54.00, 54.01)
atan- cmath/fast = (55.00, 55.01)
atan- cmath/fast = (56.00, 56.01)
atan- cmath/fast = (57.00, 57.01)
atan- cmath/fast = (58.00, 58.01)
atan- cmath/fast = (-58.00, -58.01)
atan- cmath/fast = (-57.00, -57.01)
atan- cmath/fast = (-56.00, -56.01)
atan- cmath/fast = (-55.00, -55.01)
atan- cmath/fast = (-54.00, -54.01)
atan- cmath/fast = (-53.00, -53.01)
atan- cmath/fast = (-49.00, -48.99)
atan- cmath/fast = (-48.00, -47.99)
atan- cmath/fast = (-47.00, -46.99)
atan- cmath/fast = (-45.00, -44.99)
atan- cmath/fast = (-43.00, -43.01)
atan- cmath/fast = (-42.00, -42.01)
atan- cmath/fast = (-41.00, -41.01)
atan- cmath/fast = (-37.00, -36.99)
atan- cmath/fast = (-36.00, -35.99)
atan- cmath/fast = (-35.00, -34.99)
atan- cmath/fast = (-34.00, -33.99)
atan- cmath/fast = (-33.00, -32.99)
atan- cmath/fast = (-32.00, -31.99)
atan- cmath/fast = (32.00, 31.99)
atan- cmath/fast = (33.00, 32.99)
atan- cmath/fast = (34.00, 33.99)
atan- cmath/fast = (35.00, 34.99)
atan- cmath/fast = (36.00, 35.99)
atan- cmath/fast = (37.00, 36.99)
atan- cmath/fast = (41.00, 41.01)
atan- cmath/fast = (42.00, 42.01)
atan- cmath/fast = (43.00, 43.01)
atan- cmath/fast = (45.00, 45.01)
atan- cmath/fast = (47.00, 46.99)
atan- cmath/fast = (48.00, 47.99)
atan- cmath/fast = (49.00, 48.99)
atan- cmath/fast = (53.00, 53.01)
atan- cmath/fast = (54.00, 54.01)
atan- cmath/fast = (55.00, 55.01)
atan- cmath/fast = (56.00, 56.01)
atan- cmath/fast = (57.00, 57.01)
atan- cmath/fast = (58.00, 58.01)
atan- cmath/fast = (-58.00, -58.01)
atan- cmath/fast = (-57.00, -57.01)
atan- cmath/fast = (-56.00, -56.01)
atan- cmath/fast = (-55.00, -55.01)
atan- cmath/fast = (-54.00, -54.01)
atan- cmath/fast = (-53.00, -53.01)
atan- cmath/fast = (-49.00, -48.99)
atan- cmath/fast = (-48.00, -47.99)
atan- cmath/fast = (-47.00, -46.99)
atan- cmath/fast = (-45.00, -44.99)
atan- cmath/fast = (-43.00, -43.01)
atan- cmath/fast = (-42.00, -42.01)
atan- cmath/fast = (-41.00, -41.01)
atan- cmath/fast = (-37.00, -36.99)
atan- cmath/fast = (-36.00, -35.99)
atan- cmath/fast = (-35.00, -34.99)
atan- cmath/fast = (-34.00, -33.99)
atan- cmath/fast = (-33.00, -32.99)
atan- cmath/fast = (-32.00, -31.99)
atan- cmath/fast = (32.00, 31.99)
atan- cmath/fast = (33.00, 32.99)
atan- cmath/fast = (34.00, 33.99)
atan- cmath/fast = (35.00, 34.99)
atan- cmath/fast = (36.00, 35.99)
atan- cmath/fast = (37.00, 36.99)
atan- cmath/fast = (41.00, 41.01)
atan- cmath/fast = (42.00, 42.01)
atan- cmath/fast = (43.00, 43.01)
atan- cmath/fast = (45.00, 44.99)
atan- cmath/fast = (47.00, 46.99)
atan- cmath/fast = (48.00, 47.99)
atan- cmath/fast = (49.00, 48.99)
atan- cmath/fast = (53.00, 53.01)
atan- cmath/fast = (54.00, 54.01)
atan- cmath/fast = (55.00, 55.01)
atan- cmath/fast = (56.00, 56.01)
atan- cmath/fast = (57.00, 57.01)
atan- cmath/fast = (58.00, 58.01)
atan- cmath/fast = (-58.00, -58.01)
atan- cmath/fast = (-57.00, -57.01)
atan- cmath/fast = (-56.00, -56.01)
atan- cmath/fast = (-55.00, -55.01)
atan- cmath/fast = (-54.00, -54.01)
atan- cmath/fast = (-53.00, -53.01)
atan- cmath/fast = (-49.00, -48.99)
atan- cmath/fast = (-48.00, -47.99)
atan- cmath/fast = (-47.00, -46.99)
atan- cmath/fast = (-45.00, -44.99)
atan- cmath/fast = (-43.00, -43.01)
atan- cmath/fast = (-42.00, -42.01)
atan- cmath/fast = (-41.00, -41.01)
atan- cmath/fast = (-37.00, -36.99)
atan- cmath/fast = (-36.00, -35.99)
atan- cmath/fast = (-35.00, -34.99)
atan- cmath/fast = (-34.00, -33.99)
atan- cmath/fast = (-33.00, -32.99)
atan- cmath/fast = (-32.00, -31.99)
atan- cmath/fast = (32.00, 31.99)
atan- cmath/fast = (33.00, 32.99)
atan- cmath/fast = (34.00, 33.99)
atan- cmath/fast = (35.00, 34.99)
atan- cmath/fast = (36.00, 35.99)
atan- cmath/fast = (37.00, 36.99)
atan- cmath/fast = (41.00, 41.01)
atan- cmath/fast = (42.00, 42.01)
atan- cmath/fast = (43.00, 43.01)
atan- cmath/fast = (45.00, 44.99)
atan- cmath/fast = (47.00, 46.99)
atan- cmath/fast = (48.00, 47.99)
atan- cmath/fast = (49.00, 48.99)
atan- cmath/fast = (53.00, 53.01)
atan- cmath/fast = (54.00, 54.01)
atan- cmath/fast = (55.00, 55.01)
atan- cmath/fast = (56.00, 56.01)
atan- cmath/fast = (57.00, 57.01)
atan- cmath/fast = (58.00, 58.01)
atan- cmath/fast = (-58.00, -58.01)
atan- cmath/fast = (-57.00, -57.01)
atan- cmath/fast = (-56.00, -56.01)
atan- cmath/fast = (-55.00, -55.01)
atan- cmath/fast = (-54.00, -54.01)
atan- cmath/fast = (-53.00, -53.01)
atan- cmath/fast = (-49.00, -48.99)
atan- cmath/fast = (-48.00, -47.99)
atan- cmath/fast = (-47.00, -46.99)
atan- cmath/fast = (-45.00, -44.99)
atan- cmath/fast = (-43.00, -43.01)
atan- cmath/fast = (-42.00, -42.01)
atan- cmath/fast = (-41.00, -41.01)
atan- cmath/fast = (-37.00, -36.99)
atan- cmath/fast = (-36.00, -35.99)
atan- cmath/fast = (-35.00, -34.99)
atan- cmath/fast = (-34.00, -33.99)
atan- cmath/fast = (-33.00, -32.99)
atan- cmath/fast = (-32.00, -31.99)
atan- cmath/fast = (32.00, 31.99)
atan- cmath/fast = (33.00, 32.99)
atan- cmath/fast = (34.00, 33.99)
atan- cmath/fast = (35.00, 34.99)
atan- cmath/fast = (36.00, 35.99)
atan- cmath/fast = (37.00, 36.99)
atan- cmath/fast = (41.00, 41.01)
atan- cmath/fast = (42.00, 42.01)
atan- cmath/fast = (43.00, 43.01)
atan- cmath/fast = (45.00, 44.99)
atan- cmath/fast = (47.00, 46.99)
atan- cmath/fast = (48.00, 47.99)
atan- cmath/fast = (49.00, 48.99)
atan- cmath/fast = (53.00, 53.01)
atan- cmath/fast = (54.00, 54.01)
atan- cmath/fast = (55.00, 55.01)
atan- cmath/fast = (56.00, 56.01)
atan- cmath/fast = (57.00, 57.01)
atan- cmath/fast = (58.00, 58.01)
atan- cmath/fast = (-58.00, -58.01)
atan- cmath/fast = (-57.00, -57.01)
atan- cmath/fast = (-56.00, -56.01)
atan- cmath/fast = (-55.00, -55.01)
atan- cmath/fast = (-54.00, -54.01)
atan- cmath/fast = (-53.00, -53.01)
atan- cmath/fast = (-49.00, -48.99)
atan- cmath/fast = (-48.00, -47.99)
atan- cmath/fast = (-47.00, -46.99)
atan- cmath/fast = (-45.00, -45.01)
atan- cmath/fast = (-43.00, -43.01)
atan- cmath/fast = (-42.00, -42.01)
atan- cmath/fast = (-41.00, -41.01)
atan- cmath/fast = (-37.00, -36.99)
atan- cmath/fast = (-36.00, -35.99)
atan- cmath/fast = (-35.00, -34.99)
atan- cmath/fast = (-34.00, -33.99)
atan- cmath/fast = (-33.00, -32.99)
atan- cmath/fast = (-32.00, -31.99)
atan- cmath/fast = (32.00, 31.99)
atan- cmath/fast = (33.00, 32.99)
atan- cmath/fast = (34.00, 33.99)
atan- cmath/fast = (35.00, 34.99)
atan- cmath/fast = (36.00, 35.99)
atan- cmath/fast = (37.00, 36.99)
atan- cmath/fast = (41.00, 41.01)
atan- cmath/fast = (42.00, 42.01)
atan- cmath/fast = (43.00, 43.01)
atan- cmath/fast = (45.00, 44.99)
atan- cmath/fast = (47.00, 46.99)
atan- cmath/fast = (48.00, 47.99)
atan- cmath/fast = (49.00, 48.99)
atan- cmath/fast = (53.00, 53.01)
atan- cmath/fast = (54.00, 54.01)
atan- cmath/fast = (55.00, 55.01)
atan- cmath/fast = (56.00, 56.01)
atan- cmath/fast = (57.00, 57.01)
atan- cmath/fast = (58.00, 58.01)
atan- cmath/fast = (-58.00, -58.01)
atan- cmath/fast = (-57.00, -57.01)
atan- cmath/fast = (-56.00, -56.01)
atan- cmath/fast = (-55.00, -55.01)
atan- cmath/fast = (-54.00, -54.01)
atan- cmath/fast = (-53.00, -53.01)
atan- cmath/fast = (-49.00, -48.99)
atan- cmath/fast = (-48.00, -47.99)
atan- cmath/fast = (-47.00, -46.99)
atan- cmath/fast = (-45.00, -45.01)
atan- cmath/fast = (-43.00, -43.01)
atan- cmath/fast = (-42.00, -42.01)
atan- cmath/fast = (-41.00, -41.01)
atan- cmath/fast = (-37.00, -36.99)
atan- cmath/fast = (-36.00, -35.99)
atan- cmath/fast = (-35.00, -34.99)
atan- cmath/fast = (-34.00, -33.99)
atan- cmath/fast = (-33.00, -32.99)
atan- cmath/fast = (-32.00, -31.99)
atan- cmath/fast = (32.00, 31.99)
atan- cmath/fast = (33.00, 32.99)
atan- cmath/fast = (34.00, 33.99)
atan- cmath/fast = (35.00, 34.99)
atan- cmath/fast = (36.00, 35.99)
atan- cmath/fast = (37.00, 36.99)
atan- cmath/fast = (41.00, 41.01)
atan- cmath/fast = (42.00, 42.01)
atan- cmath/fast = (43.00, 43.01)
atan- cmath/fast = (45.00, 45.01)
atan- cmath/fast = (47.00, 46.99)
atan- cmath/fast = (48.00, 47.99)
atan- cmath/fast = (49.00, 48.99)
atan- cmath/fast = (53.00, 53.01)
atan- cmath/fast = (54.00, 54.01)
atan- cmath/fast = (55.00, 55.01)
atan- cmath/fast = (56.00, 56.01)
atan- cmath/fast = (57.00, 57.01)
atan- cmath/fast = (58.00, 58.01)
atan- cmath/fast = (-58.00, -58.01)
atan- cmath/fast = (-57.00, -57.01)
atan- cmath/fast = (-56.00, -56.01)
atan- cmath/fast = (-55.00, -55.01)
atan- cmath/fast = (-54.00, -54.01)
atan- cmath/fast = (-53.00, -53.01)
atan- cmath/fast = (-49.00, -48.99)
atan- cmath/fast = (-48.00, -47.99)
atan- cmath/fast = (-47.00, -46.99)
atan- cmath/fast = (-45.00, -44.99)
atan- cmath/fast = (-43.00, -43.01)
atan- cmath/fast = (-42.00, -42.01)
atan- cmath/fast = (-41.00, -41.01)
atan- cmath/fast = (-37.00, -36.99)
atan- cmath/fast = (-36.00, -35.99)
atan- cmath/fast = (-35.00, -34.99)
atan- cmath/fast = (-34.00, -33.99)
atan- cmath/fast = (-33.00, -32.99)
atan- cmath/fast = (-32.00, -31.99)