Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

trig.cpp 4.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cmath>
  14. #include "core.h"
  15. #include "lol/unit.h"
  16. namespace lol
  17. {
  18. LOLUNIT_FIXTURE(TrigTest)
  19. {
  20. LOLUNIT_TEST(Sin)
  21. {
  22. using std::fabs;
  23. using std::sin;
  24. for (int i = -10000; i < 10000; i++)
  25. {
  26. double f = (double)i * (1.0 / 1000.0);
  27. #if defined __GNUC__
  28. double a = __builtin_sin(f);
  29. #else
  30. double a = sin(f);
  31. #endif
  32. double b = lol_sin(f);
  33. LOLUNIT_SET_CONTEXT(f);
  34. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
  35. }
  36. for (int i = -10000; i < 10000; i++)
  37. {
  38. double f = (double)i * (1.0 / 100000.0);
  39. #if defined __GNUC__
  40. double a = __builtin_sin(f);
  41. #else
  42. double a = sin(f);
  43. #endif
  44. double b = lol_sin(f);
  45. LOLUNIT_SET_CONTEXT(f);
  46. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
  47. }
  48. }
  49. LOLUNIT_TEST(Cos)
  50. {
  51. using std::fabs;
  52. using std::cos;
  53. for (int i = -10000; i < 10000; i++)
  54. {
  55. double f = (double)i * (1.0 / 1000.0);
  56. #if defined __GNUC__
  57. double a = __builtin_cos(f);
  58. #else
  59. double a = cos(f);
  60. #endif
  61. double b = lol_cos(f);
  62. LOLUNIT_SET_CONTEXT(f);
  63. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
  64. }
  65. for (int i = -10000; i < 10000; i++)
  66. {
  67. double f = (double)i * (1.0 / 100000.0);
  68. #if defined __GNUC__
  69. double a = __builtin_cos(f);
  70. #else
  71. double a = cos(f);
  72. #endif
  73. double b = lol_cos(f);
  74. LOLUNIT_SET_CONTEXT(f);
  75. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
  76. }
  77. }
  78. LOLUNIT_TEST(SinCos)
  79. {
  80. using std::fabs;
  81. using std::sin;
  82. using std::cos;
  83. for (int i = -10000; i < 10000; i++)
  84. {
  85. double f = (double)i * (1.0 / 1000.0);
  86. #if defined __GNUC__
  87. double a1 = __builtin_sin(f);
  88. double a2 = __builtin_cos(f);
  89. #else
  90. double a1 = sin(f);
  91. double a2 = cos(f);
  92. #endif
  93. double b1, b2;
  94. lol_sincos(f, &b1, &b2);
  95. LOLUNIT_SET_CONTEXT(f);
  96. LOLUNIT_ASSERT_DOUBLES_EQUAL(a1, b1, fabs(f) * 1e-11);
  97. LOLUNIT_ASSERT_DOUBLES_EQUAL(a2, b2, fabs(f) * 1e-11);
  98. }
  99. for (int i = -10000; i < 10000; i++)
  100. {
  101. double f = (double)i * (1.0 / 100000.0);
  102. #if defined __GNUC__
  103. double a1 = __builtin_sin(f);
  104. double a2 = __builtin_cos(f);
  105. #else
  106. double a1 = sin(f);
  107. double a2 = cos(f);
  108. #endif
  109. double b1, b2;
  110. lol_sincos(f, &b1, &b2);
  111. LOLUNIT_SET_CONTEXT(f);
  112. LOLUNIT_ASSERT_DOUBLES_EQUAL(a1, b1, fabs(f) * 1e-11);
  113. LOLUNIT_ASSERT_DOUBLES_EQUAL(a2, b2, fabs(f) * 1e-11);
  114. }
  115. }
  116. LOLUNIT_TEST(Tan)
  117. {
  118. using std::fabs;
  119. using std::tan;
  120. for (int i = -100000; i < 100000; i++)
  121. {
  122. double f = (double)i * (1.0 / 10000.0);
  123. #if defined __GNUC__
  124. double a = __builtin_tan(f);
  125. #else
  126. double a = tan(f);
  127. #endif
  128. double b = lol_tan(f);
  129. LOLUNIT_SET_CONTEXT(f);
  130. if (fabs(a) > 1e4)
  131. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * fabs(a) * 1e-11);
  132. else if (fabs(a) > 1.0)
  133. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * 1e-11);
  134. else
  135. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
  136. }
  137. for (int i = -10000; i < 10000; i++)
  138. {
  139. double f = (double)i * (1.0 / 100000.0);
  140. #if defined __GNUC__
  141. double a = __builtin_tan(f);
  142. #else
  143. double a = tan(f);
  144. #endif
  145. double b = lol_tan(f);
  146. LOLUNIT_SET_CONTEXT(f);
  147. if (fabs(a) > 1e4)
  148. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * fabs(a) * 1e-11);
  149. else if (fabs(a) > 1.0)
  150. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(a) * 1e-11);
  151. else
  152. LOLUNIT_ASSERT_DOUBLES_EQUAL(a, b, fabs(f) * 1e-11);
  153. }
  154. }
  155. };
  156. } /* namespace lol */