angle.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*! ========================================================================
  2. ** Extended Template and Library Test Suite
  3. ** Angle Class Test
  4. **
  5. ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
  6. ** Copyright (c) 2008 Chris Moore
  7. **
  8. ** This package is free software; you can redistribute it and/or
  9. ** modify it under the terms of the GNU General Public License as
  10. ** published by the Free Software Foundation; either version 2 of
  11. ** the License, or (at your option) any later version.
  12. **
  13. ** This package is distributed in the hope that it will be useful,
  14. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. ** General Public License for more details.
  17. **
  18. ** === N O T E S ===========================================================
  19. **
  20. ** ========================================================================= */
  21. #include <stdio.h>
  22. #include <ETL/angle>
  23. #include <ETL/fastangle>
  24. #include <ETL/clock>
  25. #include <ETL/bezier>
  26. #include <ETL/hermite>
  27. ETL_FASTANGLE_INIT();
  28. using namespace std;
  29. using namespace etl;
  30. int fastangle_test(void)
  31. {
  32. int ret = 0;
  33. float largest_error;
  34. {
  35. angle theta;
  36. fastangle theta2;
  37. float error;
  38. largest_error = 0.0f;
  39. for (
  40. theta = angle::degrees(0), theta2 = fastangle::degrees(0);
  41. theta <= angle::degrees(360);
  42. theta += angle::degrees(10), theta2 += fastangle::degrees(10)
  43. ) {
  44. error = (float)angle::sin(theta).get() - (float)fastangle::sin(theta2).get();
  45. if (error > largest_error) {
  46. largest_error = error;
  47. }
  48. if (error < -largest_error) {
  49. largest_error = -error;
  50. }
  51. }
  52. }
  53. printf("fastangle: Largest SIN error: (+/-)%f\n", largest_error);
  54. if (largest_error > 0.075) {
  55. ret++;
  56. }
  57. {
  58. angle theta;
  59. fastangle theta2;
  60. float error;
  61. largest_error = 0.0f;
  62. for (
  63. theta = angle::degrees(0), theta2 = fastangle::degrees(0);
  64. theta <= angle::degrees(360);
  65. theta += angle::degrees(10), theta2 += fastangle::degrees(10)
  66. ) {
  67. error = (float)angle::cos(theta).get() - (float)fastangle::cos(theta2).get();
  68. if (error > largest_error) {
  69. largest_error = error;
  70. }
  71. if (error < -largest_error) {
  72. largest_error = -error;
  73. }
  74. }
  75. }
  76. printf("fastangle: Largest COS error: (+/-)%f\n", largest_error);
  77. if (largest_error > 0.075) {
  78. ret++;
  79. }
  80. {
  81. double val;
  82. float error;
  83. largest_error = 0.0f;
  84. for (
  85. val = -1.0f;
  86. val < 1.0f;
  87. val += 0.01
  88. ) {
  89. error = angle::radians(angle::sin(val)).get() - fastangle::radians(fastangle::sin(val)).get();
  90. if (error > largest_error) {
  91. largest_error = error;
  92. }
  93. if (error < -largest_error) {
  94. largest_error = -error;
  95. }
  96. }
  97. }
  98. printf("fastangle: Largest ASIN error: (+/-)%frad\n", largest_error);
  99. if (largest_error > 0.075) {
  100. ret++;
  101. }
  102. {
  103. double val;
  104. float error;
  105. largest_error = 0.0f;
  106. for (
  107. val = -1.0f;
  108. val < 1.0f;
  109. val += 0.01
  110. ) {
  111. error = angle::radians(angle::cos(val)).get() - fastangle::radians(fastangle::cos(val)).get();
  112. if (error > largest_error) {
  113. largest_error = error;
  114. }
  115. if (error < -largest_error) {
  116. largest_error = -error;
  117. }
  118. }
  119. }
  120. printf("fastangle: Largest ACOS error: (+/-)%frad\n", largest_error);
  121. if (largest_error > 0.075) {
  122. ret++;
  123. }
  124. {
  125. angle theta;
  126. fastangle theta2;
  127. float error;
  128. largest_error = 0.0f;
  129. for (
  130. theta = angle::degrees(0), theta2 = fastangle::degrees(0);
  131. theta < angle::degrees(360);
  132. theta += angle::degrees(38), theta2 += fastangle::degrees(38)
  133. ) {
  134. error = angle::tan(theta).get() - fastangle::tan(theta2).get();
  135. if (error > largest_error) {
  136. largest_error = error;
  137. }
  138. if (error < -largest_error) {
  139. largest_error = -error;
  140. }
  141. }
  142. }
  143. printf("fastangle: Largest TAN error: (+/-)%f\n", largest_error);
  144. if (largest_error > 0.75) {
  145. ret++;
  146. }
  147. {
  148. double val;
  149. float error;
  150. largest_error = 0.0f;
  151. for (
  152. val = -4.0f;
  153. val < 4.0f;
  154. val += 0.1
  155. ) {
  156. error = angle::radians(angle::tan(val)).get() - fastangle::radians(fastangle::tan(val)).get();
  157. if (error > largest_error) {
  158. largest_error = error;
  159. }
  160. if (error < -largest_error) {
  161. largest_error = -error;
  162. }
  163. }
  164. }
  165. printf("fastangle: Largest ATAN error: (+/-)%frad\n", largest_error);
  166. if (largest_error > 0.075) {
  167. ret++;
  168. }
  169. {
  170. angle theta;
  171. float error;
  172. largest_error = 0.0f;
  173. for (
  174. theta = angle::degrees(-179);
  175. theta < angle::degrees(180);
  176. theta += angle::degrees(5)
  177. ) {
  178. error = angle::radians(theta).get() - fastangle::radians(fastangle::tan(angle::sin(theta).get(), angle::cos(theta).get())).get();
  179. if (error > largest_error) {
  180. largest_error = error;
  181. }
  182. if (error < -largest_error) {
  183. largest_error = -error;
  184. }
  185. }
  186. }
  187. printf("fastangle: Largest ATAN2 error: (+/-)%frad\n", largest_error);
  188. if (largest_error > 0.075) {
  189. ret++;
  190. }
  191. printf("constant tests: %f==%f\n",
  192. (float)angle::degrees(angle::tan(1.01)).get(),
  193. (float)fastangle::degrees(fastangle::tan(1.01)).get());
  194. printf("constant tests: %f==%f\n",
  195. (float)angle::degrees(angle::tan(-1.0)).get(),
  196. (float)fastangle::degrees(fastangle::tan(-1.0)).get());
  197. return ret;
  198. }
  199. template <class Angle>
  200. void angle_cos_speed_test(void)
  201. {
  202. Angle a, b, c, d;
  203. float tmp, tmp2;
  204. for (tmp = -1.0; tmp < 1.0; tmp += 0.000002) {
  205. a = (typename Angle::cos)(tmp);
  206. b = (typename Angle::cos)(tmp);
  207. c = (typename Angle::cos)(tmp);
  208. d = (typename Angle::cos)(tmp);
  209. tmp2 = ((typename Angle::cos)(a)).get();
  210. tmp2 = ((typename Angle::cos)(b)).get();
  211. tmp2 = ((typename Angle::cos)(c)).get();
  212. tmp2 = ((typename Angle::cos)(d)).get();
  213. }
  214. }
  215. template <class Angle>
  216. void angle_sin_speed_test(void)
  217. {
  218. Angle a, b, c, d;
  219. float tmp, tmp2;
  220. for (tmp = -1.0; tmp < 1.0; tmp += 0.000002) {
  221. a = (typename Angle::sin)(tmp);
  222. b = (typename Angle::sin)(tmp);
  223. c = (typename Angle::sin)(tmp);
  224. d = (typename Angle::sin)(tmp);
  225. tmp2 = ((typename Angle::sin)(a)).get();
  226. tmp2 = ((typename Angle::sin)(b)).get();
  227. tmp2 = ((typename Angle::sin)(c)).get();
  228. tmp2 = ((typename Angle::sin)(d)).get();
  229. }
  230. }
  231. template <class Angle>
  232. void angle_tan_speed_test(void)
  233. {
  234. Angle a, b, c, d;
  235. float tmp, tmp2;
  236. for (tmp = -1.0; tmp < 1.0; tmp += 0.000002) {
  237. a = (typename Angle::tan)(tmp);
  238. b = (typename Angle::tan)(tmp);
  239. c = (typename Angle::tan)(tmp);
  240. d = (typename Angle::tan)(tmp);
  241. tmp2 = ((typename Angle::tan)(a)).get();
  242. tmp2 = ((typename Angle::tan)(b)).get();
  243. tmp2 = ((typename Angle::tan)(c)).get();
  244. tmp2 = ((typename Angle::tan)(d)).get();
  245. }
  246. }
  247. template <class Angle, class mytan>
  248. void angle_atan2_speed_test(void)
  249. {
  250. Angle a, b, c;
  251. float x, y;
  252. for (y = -10.0; y < 10.0; y += 0.05)
  253. for (x = -10.0; x < 10.0; x += 0.05) {
  254. a = mytan(y, x);
  255. a = mytan(x, y);
  256. b = mytan(y, x);
  257. b = mytan(x, y);
  258. c = mytan(y, x);
  259. c = mytan(x, y);
  260. a = mytan(y, x);
  261. a = mytan(x, y);
  262. b = mytan(y, x);
  263. b = mytan(x, y);
  264. c = mytan(y, x);
  265. c = mytan(x, y);
  266. }
  267. }
  268. int fastangle_speed_test(void)
  269. {
  270. int ret = 0;
  271. float
  272. angle_cos_time,
  273. fastangle_cos_time,
  274. angle_tan_time,
  275. fastangle_tan_time,
  276. angle_atan2_time,
  277. fastangle_atan2_time,
  278. angle_sin_time,
  279. fastangle_sin_time ;
  280. etl::clock MyTimer;
  281. MyTimer.reset();
  282. angle_cos_speed_test<angle>();
  283. angle_cos_time = MyTimer();
  284. printf("angle: Cosine test: %f seconds\n", angle_cos_time);
  285. MyTimer.reset();
  286. angle_cos_speed_test<fastangle>();
  287. fastangle_cos_time = MyTimer();
  288. printf("fastangle: Cosine test: %f seconds\n", fastangle_cos_time);
  289. printf("fastangle is %.02f%% faster\n", (angle_cos_time / fastangle_cos_time) * 100.0 - 100.0);
  290. MyTimer.reset();
  291. angle_sin_speed_test<angle>();
  292. angle_sin_time = MyTimer();
  293. printf("angle: Sine test: %f seconds\n", angle_sin_time);
  294. MyTimer.reset();
  295. angle_sin_speed_test<fastangle>();
  296. fastangle_sin_time = MyTimer();
  297. printf("fastangle: Sine test: %f seconds\n", fastangle_sin_time);
  298. printf("fastangle is %.02f%% faster\n", (angle_sin_time / fastangle_sin_time) * 100.0 - 100.0);
  299. MyTimer.reset();
  300. angle_tan_speed_test<angle>();
  301. angle_tan_time = MyTimer();
  302. printf("angle: Tangent test: %f seconds\n", angle_tan_time);
  303. MyTimer.reset();
  304. angle_tan_speed_test<fastangle>();
  305. fastangle_tan_time = MyTimer();
  306. printf("fastangle: Tangent test: %f seconds\n", fastangle_tan_time);
  307. printf("fastangle is %.02f%% faster\n", (angle_tan_time / fastangle_tan_time) * 100.0 - 100.0);
  308. MyTimer.reset();
  309. angle_atan2_speed_test<angle, angle::tan>();
  310. angle_atan2_time = MyTimer();
  311. printf("angle: arcTangent2 test: %f seconds\n", angle_atan2_time);
  312. MyTimer.reset();
  313. angle_atan2_speed_test<fastangle, fastangle::tan>();
  314. fastangle_atan2_time = MyTimer();
  315. printf("fastangle: arcTangent2 test: %f seconds\n", fastangle_atan2_time);
  316. printf("fastangle is %.02f%% faster\n", (angle_atan2_time / fastangle_atan2_time) * 100.0 - 100.0);
  317. return ret;
  318. }
  319. int angle_test()
  320. {
  321. int ret = 0;
  322. float dist;
  323. dist = angle::deg(angle::deg(330).dist(angle::deg(30))).get();
  324. printf("angle: angular difference between 330deg and 30deg is %0.1fdeg\n", dist);
  325. if (floor(dist + 0.5) != 300) {
  326. printf("angle: error: should be 300deg!\n");
  327. ret++;
  328. }
  329. dist = angle::deg(angle::deg(30).dist(angle::deg(330))).get();
  330. printf("angle: angular difference between 30deg and 330deg is %0.1fdeg\n", dist);
  331. if (floor(dist + 0.5) != -300) {
  332. printf("angle: error: should be -300deg!\n");
  333. ret++;
  334. }
  335. dist = angle::deg(angle::deg(30).dist(angle::deg(-30))).get();
  336. printf("angle: angular difference between 30deg and -30deg is %0.1fdeg\n", dist);
  337. if (floor(dist + 0.5) != 60) {
  338. printf("angle: error: should be 60deg!\n");
  339. ret++;
  340. }
  341. dist = angle::deg(angle::deg(-30).dist(angle::deg(30))).get();
  342. printf("angle: angular difference between -30deg and 30deg is %0.1fdeg\n", dist);
  343. if (floor(dist + 0.5) != -60) {
  344. printf("angle: error: should be -60deg!\n");
  345. ret++;
  346. }
  347. dist = angle::deg(angle::deg(20).dist(angle::deg(195))).get();
  348. printf("angle: angular difference between 20deg and 195deg is %0.1fdeg\n", dist);
  349. if (floor(dist + 0.5) != -175) {
  350. printf("angle: error: should be -175deg!\n");
  351. ret++;
  352. }
  353. dist = angle::deg(angle::deg(20).dist(angle::deg(205))).get();
  354. printf("angle: angular difference between 20deg and 205deg is %0.1fdeg\n", dist);
  355. if (floor(dist + 0.5) != -185) {
  356. printf("angle: error: should be -185deg!\n");
  357. ret++;
  358. }
  359. int i;
  360. for (i = -1000; i < 1000; i++) {
  361. dist = angle::deg(angle::deg(20 + i + 360).dist(angle::deg(205 + i - 360))).get();
  362. if (floor(dist + 0.5) != 535) {
  363. printf("angle: error: Badness at %d!\n", i);
  364. ret++;
  365. }
  366. }
  367. for (i = -1000; i < 1000; i++) {
  368. dist = angle::deg(angle::deg(20 + i - 360).dist(angle::deg(195 + i + 360))).get();
  369. if (floor(dist + 0.5) != -895) {
  370. printf("angle: error: Badness at %d!\n", i);
  371. ret++;
  372. }
  373. }
  374. {
  375. float f;
  376. angle a(angle::deg(-2005));
  377. angle b(angle::deg(200));
  378. affine_combo<angle> combo;
  379. hermite<angle> hermie(a, b, b.dist(a), b.dist(a));
  380. for (f = 0; f < 1.001; f += 0.1) {
  381. printf("@%f--affine_combo: %f hermie: %f\n", angle::deg(f).get(), angle::deg(combo(a, b, f)).get(), angle::deg(hermie(f)).get());
  382. }
  383. }
  384. return ret;
  385. }
  386. int main()
  387. {
  388. int error = 0;
  389. error += fastangle_test();
  390. error += fastangle_speed_test();
  391. error += angle_test();
  392. return error;
  393. }