Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

127 wiersze
3.7 KiB

  1. //
  2. // Lol Engine - Sample math program: chek trigonometric functions
  3. //
  4. // Copyright: (c) 2005-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 <cstdio>
  14. #include "core.h"
  15. using namespace lol;
  16. using namespace std;
  17. static float adjustf(float f, int i)
  18. {
  19. union { float f; uint32_t x; } u = { f };
  20. u.x += i;
  21. return u.f;
  22. }
  23. static double adjust(double f, int i)
  24. {
  25. union { double f; uint64_t x; } u = { f };
  26. u.x += i;
  27. return u.f;
  28. }
  29. static void inspect(float f)
  30. {
  31. union { float f; uint32_t x; } u = { f };
  32. printf("%08x %g -- ", u.x, u.f);
  33. int i = (u.x & 0x7fffffu) | 0x800000u;
  34. int j = 23 - ((u.x >> 23) & 0xff) + ((1 << 7) - 1);
  35. if (u.f <= 0)
  36. i = -i;
  37. printf("%i / 2^%i = %g\n", i, j, (float)i / (1LLu << j));
  38. }
  39. //#define float double
  40. #if 1
  41. static float const a0 = adjust(0.9999999999999376, 0);
  42. static float const a1 = adjust(-0.1666666666643236, 0);
  43. static float const a2 = adjust(0.008333333318766562, 0);
  44. static float const a3 = adjust(-0.0001984126641174625, 0);
  45. static float const a4 = adjust(2.755693193297308e-006, 0);
  46. static float const a5 = adjust(-2.502951900290311e-008, 0);
  47. static float const a6 = adjust(1.540117123154927e-010, 0);
  48. #elif 0
  49. static float const a0 = adjust(1.0, 0);
  50. static float const a1 = adjust(-0.1666666666372165, 0);
  51. static float const a2 = adjust(0.008333332748323419, 0);
  52. static float const a3 = adjust(-0.0001984108245332497, 0);
  53. static float const a4 = adjust(2.753619853326498e-06, 0);
  54. static float const a5 = adjust(-2.407483949485896e-08, 0);
  55. static float const a6 = 0.0;
  56. #else
  57. static float const a0 = adjust(0.9999999946887117, 0);
  58. static float const a1 = adjust(-0.1666665668590824, 0);
  59. static float const a2 = adjust(0.008333025160523476, 0);
  60. static float const a3 = adjust(-0.0001980741944205014, 0);
  61. static float const a4 = adjust(2.60190356966559e-06, 0); // -900 in floats
  62. static float const a5 = 0.0;
  63. static float const a6 = 0.0;
  64. #endif
  65. static float floatsin(float f)
  66. {
  67. //return lol_sin(f);
  68. //static float const k = (float)real::R_2_PI;
  69. //f *= k;
  70. float f2 = f * f;
  71. float f4 = f2 * f2;
  72. return f * (a0 + f4 * (a2 + f4 * (a4 + f4 * a6)) + f2 * (a1 + f4 * (a3 + f4 * a5)));
  73. //return f * (a0 + f2 * (a1 + f2 * (a2 + f2 * (a3 + f2 * (a4 + f2 * (a5 + f2 * a6))))));
  74. //return f * (a0 + a1 * f2 + a2 * f2 * f2 + a3 * f2 * f2 * f2 + a4 * f2 * f2 * f2 * f2 + a5 * f2 * f2 * f2 * f2 * f2 + a6 * f2 * f2 * f2 * f2 * f2 * f2);
  75. #undef float
  76. }
  77. int main(void)
  78. {
  79. int error[5] = { 0 };
  80. inspect(a0);
  81. inspect(a1);
  82. inspect(a2);
  83. inspect(a3);
  84. inspect(a4);
  85. inspect(a5);
  86. for (float f = (float)real::R_PI_2; f > 1e-30; f = adjustf(f, -1))
  87. {
  88. union { float f; uint32_t x; } u = { f };
  89. union { float f; uint32_t x; } s1 = { sinf(f) };
  90. union { float f; uint32_t x; } s2 = { floatsin(f) };
  91. int e = abs((int)(s1.x - s2.x));
  92. switch (e)
  93. {
  94. case 3:
  95. if (fabs((double)s1.f - (double)s2.f) > 1e-10 * fabs(s1.f))
  96. printf("%15.13g %08x: %15.13g %15.13g %08x %08x\n", f, u.x, s1.f, s2.f, s1.x, s2.x);
  97. case 2:
  98. case 1:
  99. case 0:
  100. error[e]++;
  101. break;
  102. default:
  103. error[4]++;
  104. break;
  105. }
  106. }
  107. printf("exact: %i off by 1: %i by 2: %i by 3: %i error: %i\n",
  108. error[0], error[1], error[2], error[3], error[4]);
  109. return EXIT_SUCCESS;
  110. }