math_test.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright 2010-2018 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #include "test.h"
  6. #include <bx/math.h>
  7. #include <bx/file.h>
  8. #include <math.h>
  9. #if !BX_COMPILER_MSVC || BX_COMPILER_MSVC >= 1800
  10. TEST_CASE("isFinite, isInfinite, isNan", "")
  11. {
  12. for (uint64_t ii = 0; ii < UINT32_MAX; ii += rand()%(1<<13)+1)
  13. {
  14. union { uint32_t ui; float f; } u = { uint32_t(ii) };
  15. REQUIRE(std::isnan(u.f) == bx::isNan(u.f) );
  16. REQUIRE(std::isfinite(u.f) == bx::isFinite(u.f) );
  17. REQUIRE(std::isinf(u.f) == bx::isInfinite(u.f) );
  18. }
  19. }
  20. #endif // !BX_COMPILER_MSVC || BX_COMPILER_MSVC >= 1800
  21. bool log2_test(float _a)
  22. {
  23. return bx::log2(_a) == bx::log(_a) * (1.0f / bx::log(2.0f) );
  24. }
  25. TEST_CASE("log2", "")
  26. {
  27. log2_test(0.0f);
  28. log2_test(256.0f);
  29. }
  30. TEST_CASE("libm", "")
  31. {
  32. bx::WriterI* writer = bx::getNullOut();
  33. REQUIRE(1389.0f == bx::abs(-1389.0f) );
  34. REQUIRE(1389.0f == bx::abs( 1389.0f) );
  35. REQUIRE( 0.0f == bx::abs(-0.0f) );
  36. REQUIRE( 0.0f == bx::abs( 0.0f) );
  37. REQUIRE(389.0f == bx::mod(1389.0f, 1000.0f) );
  38. REQUIRE(bx::isNan(bx::mod(0.0f, 0.0f) ) );
  39. REQUIRE( 13.0f == bx::floor( 13.89f) );
  40. REQUIRE(-14.0f == bx::floor(-13.89f) );
  41. REQUIRE( 14.0f == bx::ceil( 13.89f) );
  42. REQUIRE(-13.0f == bx::ceil( -13.89f) );
  43. REQUIRE( 13.0f == bx::trunc( 13.89f) );
  44. REQUIRE(-13.0f == bx::trunc(-13.89f) );
  45. REQUIRE(bx::equal( 0.89f, bx::fract( 13.89f), 0.000001f) );
  46. REQUIRE(bx::equal(-0.89f, bx::fract(-13.89f), 0.000001f) );
  47. for (int32_t yy = -10; yy < 10; ++yy)
  48. {
  49. for (float xx = -100.0f; xx < 100.0f; xx += 0.1f)
  50. {
  51. bx::writePrintf(writer, "ldexp(%f, %d) == %f (expected: %f)\n", xx, yy, bx::ldexp(xx, yy), ::ldexpf(xx, yy) );
  52. REQUIRE(bx::equal(bx::ldexp(xx, yy), ::ldexpf(xx, yy), 0.00001f) );
  53. }
  54. }
  55. for (float xx = -80.0f; xx < 80.0f; xx += 0.1f)
  56. {
  57. bx::writePrintf(writer, "exp(%f) == %f (expected: %f)\n", xx, bx::exp(xx), ::expf(xx) );
  58. REQUIRE(bx::equal(bx::exp(xx), ::expf(xx), 0.00001f) );
  59. }
  60. for (float xx = 0.0f; xx < 100.0f; xx += 0.1f)
  61. {
  62. bx::writePrintf(writer, "sqrt(%f) == %f (expected: %f)\n", xx, bx::sqrt(xx), ::sqrtf(xx) );
  63. REQUIRE(bx::equal(bx::sqrt(xx), ::sqrtf(xx), 0.00001f) );
  64. }
  65. for (float xx = -100.0f; xx < 100.0f; xx += 0.1f)
  66. {
  67. bx::writePrintf(writer, "pow(1.389f, %f) == %f (expected: %f)\n", xx, bx::pow(1.389f, xx), ::powf(1.389f, xx) );
  68. REQUIRE(bx::equal(bx::pow(1.389f, xx), ::powf(1.389f, xx), 0.00001f) );
  69. }
  70. for (float xx = -1.0f; xx < 1.0f; xx += 0.001f)
  71. {
  72. bx::writePrintf(writer, "asin(%f) == %f (expected: %f)\n", xx, bx::asin(xx), ::asinf(xx) );
  73. REQUIRE(bx::equal(bx::asin(xx), ::asinf(xx), 0.0001f) );
  74. }
  75. for (float xx = -100.0f; xx < 100.0f; xx += 0.1f)
  76. {
  77. bx::writePrintf(writer, "sin(%f) == %f (expected: %f)\n", xx, bx::sin(xx), ::sinf(xx) );
  78. REQUIRE(bx::equal(bx::sin(xx), ::sinf(xx), 0.00001f) );
  79. }
  80. for (float xx = -1.0f; xx < 1.0f; xx += 0.1f)
  81. {
  82. bx::writePrintf(writer, "sinh(%f) == %f (expected: %f)\n", xx, bx::sinh(xx), ::sinhf(xx) );
  83. REQUIRE(bx::equal(bx::sinh(xx), ::sinhf(xx), 0.00001f) );
  84. }
  85. for (float xx = -1.0f; xx < 1.0f; xx += 0.001f)
  86. {
  87. bx::writePrintf(writer, "acos(%f) == %f (expected: %f\n)", xx, bx::acos(xx), ::acosf(xx) );
  88. REQUIRE(bx::equal(bx::acos(xx), ::acosf(xx), 0.0001f) );
  89. }
  90. for (float xx = -100.0f; xx < 100.0f; xx += 0.1f)
  91. {
  92. bx::writePrintf(writer, "cos(%f) == %f (expected: %f)\n", xx, bx::cos(xx), ::cosf(xx) );
  93. REQUIRE(bx::equal(bx::cos(xx), ::cosf(xx), 0.00001f) );
  94. }
  95. for (float xx = -100.0f; xx < 100.0f; xx += 0.1f)
  96. {
  97. bx::writePrintf(writer, "tan(%f) == %f (expected: %f)\n", xx, bx::tan(xx), ::tanf(xx) );
  98. REQUIRE(bx::equal(bx::tan(xx), ::tanf(xx), 0.001f) );
  99. }
  100. for (float xx = -1.0f; xx < 1.0f; xx += 0.1f)
  101. {
  102. bx::writePrintf(writer, "tanh(%f) == %f (expected: %f\n", xx, bx::tanh(xx), ::tanhf(xx) );
  103. REQUIRE(bx::equal(bx::tanh(xx), ::tanhf(xx), 0.00001f) );
  104. }
  105. for (float xx = -100.0f; xx < 100.0f; xx += 0.1f)
  106. {
  107. bx::writePrintf(writer, "atan(%f) == %f (expected: %f)\n", xx, bx::atan(xx), ::atanf(xx) );
  108. REQUIRE(bx::equal(bx::atan(xx), ::atanf(xx), 0.00001f) );
  109. }
  110. for (float yy = -100.0f; yy < 100.0f; yy += 0.1f)
  111. {
  112. for (float xx = -100.0f; xx < 100.0f; xx += 0.1f)
  113. {
  114. bx::writePrintf(writer, "atan2(%f, %f) == %f (expected: %f)\n", yy, xx, bx::atan2(yy, xx), ::atan2f(yy, xx) );
  115. REQUIRE(bx::equal(bx::atan2(yy, xx), ::atan2f(yy, xx), 0.00001f) );
  116. }
  117. }
  118. }
  119. TEST_CASE("ToBits", "")
  120. {
  121. REQUIRE(UINT32_C(0x12345678) == bx::floatToBits( bx::bitsToFloat( UINT32_C(0x12345678) ) ) );
  122. REQUIRE(UINT64_C(0x123456789abcdef0) == bx::doubleToBits(bx::bitsToDouble(UINT32_C(0x123456789abcdef0) ) ) );
  123. }
  124. void mtxCheck(const float* _a, const float* _b)
  125. {
  126. if (!bx::equal(_a, _b, 16, 0.01f) )
  127. {
  128. DBG("\n"
  129. "A:\n"
  130. "%10.4f %10.4f %10.4f %10.4f\n"
  131. "%10.4f %10.4f %10.4f %10.4f\n"
  132. "%10.4f %10.4f %10.4f %10.4f\n"
  133. "%10.4f %10.4f %10.4f %10.4f\n"
  134. "B:\n"
  135. "%10.4f %10.4f %10.4f %10.4f\n"
  136. "%10.4f %10.4f %10.4f %10.4f\n"
  137. "%10.4f %10.4f %10.4f %10.4f\n"
  138. "%10.4f %10.4f %10.4f %10.4f\n"
  139. , _a[ 0], _a[ 1], _a[ 2], _a[ 3]
  140. , _a[ 4], _a[ 5], _a[ 6], _a[ 7]
  141. , _a[ 8], _a[ 9], _a[10], _a[11]
  142. , _a[12], _a[13], _a[14], _a[15]
  143. , _b[ 0], _b[ 1], _b[ 2], _b[ 3]
  144. , _b[ 4], _b[ 5], _b[ 6], _b[ 7]
  145. , _b[ 8], _b[ 9], _b[10], _b[11]
  146. , _b[12], _b[13], _b[14], _b[15]
  147. );
  148. CHECK(false);
  149. }
  150. }
  151. TEST_CASE("quaternion", "")
  152. {
  153. float mtxQ[16];
  154. float mtx[16];
  155. float quat[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
  156. bx::mtxQuat(mtxQ, quat);
  157. bx::mtxIdentity(mtx);
  158. mtxCheck(mtxQ, mtx);
  159. float ax = bx::kPi/27.0f;
  160. float ay = bx::kPi/13.0f;
  161. float az = bx::kPi/7.0f;
  162. bx::quatRotateX(quat, ax);
  163. bx::mtxQuat(mtxQ, quat);
  164. bx::mtxRotateX(mtx, ax);
  165. mtxCheck(mtxQ, mtx);
  166. float euler[3];
  167. bx::quatToEuler(euler, quat);
  168. CHECK(bx::equal(euler[0], ax, 0.001f) );
  169. bx::quatRotateY(quat, ay);
  170. bx::mtxQuat(mtxQ, quat);
  171. bx::mtxRotateY(mtx, ay);
  172. mtxCheck(mtxQ, mtx);
  173. bx::quatToEuler(euler, quat);
  174. CHECK(bx::equal(euler[1], ay, 0.001f) );
  175. bx::quatRotateZ(quat, az);
  176. bx::mtxQuat(mtxQ, quat);
  177. bx::mtxRotateZ(mtx, az);
  178. mtxCheck(mtxQ, mtx);
  179. bx::quatToEuler(euler, quat);
  180. CHECK(bx::equal(euler[2], az, 0.001f) );
  181. }