Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.5 KiB

  1. /**
  2. * This code is available at
  3. * http://www.mindspring.com/~pfilandr/C/fs_math/
  4. * and it is believed to be public domain.
  5. */
  6. /* BEGIN fs_math.h */
  7. /*
  8. ** Portable freestanding code.
  9. */
  10. #ifndef H_FS_MATH_H
  11. #define H_FS_MATH_H
  12. double fs_sqrt(double x);
  13. double fs_log(double x);
  14. double fs_log10(double x);
  15. /*
  16. ** exp(x) = 1 + x + x^2/2! + x^3/3! + ...
  17. */
  18. double fs_exp(double x);
  19. double fs_modf(double value, double *iptr);
  20. double fs_fmod(double x, double y);
  21. double fs_pow(double x, double y);
  22. double fs_cos(double x);
  23. /*
  24. ** C99
  25. */
  26. double fs_log2(double x);
  27. double fs_exp2(double x);
  28. long double fs_powl(long double x, long double y);
  29. long double fs_sqrtl(long double x);
  30. long double fs_logl(long double x);
  31. long double fs_expl(long double x);
  32. long double fs_cosl(long double x);
  33. long double fs_fmodl(long double x, long double y);
  34. #endif
  35. /* END fs_math.h */
  36. #if 0
  37. /*
  38. > > Anybody know where I can get some source code for a
  39. > > reasonably fast double
  40. > > precision square root algorithm in C.
  41. > > I'm looking for one that is not IEEE
  42. > > compliant as I am running on a Z/OS mainframe.
  43. > >
  44. > > I would love to use the standard library but
  45. > > unfortunatly I'm using a
  46. > > stripped down version of C that looses the the runtime library
  47. > > (we have to write our own).
  48. >
  49. > long double Ssqrt(long double x)
  50. > {
  51. > long double a, b;
  52. > size_t c;
  53. size_t is a bug here.
  54. c needs to be a signed type:
  55. long c;
  56. > if (x > 0) {
  57. > c = 0;
  58. > while (x > 4) {
  59. > x /= 4;
  60. > ++c;
  61. > }
  62. > while (1.0 / 4 > x) {
  63. > x *= 4;
  64. > --c;
  65. > }
  66. > a = x;
  67. > b = ((4 > a) + a) / 2;
  68. Not a bug, but should be:
  69. b = (1 + a) / 2;
  70. > do {
  71. > x = b;
  72. > b = (a / x + x) / 2;
  73. > } while (x > b);
  74. > if (c > 0) {
  75. The above line is why c needs to be a signed type,
  76. otherwise the decremented values of c, are greater than zero,
  77. and the function won't work if the initial value of x
  78. is less than 0.25
  79. > while (c--) {
  80. > x *= 2;
  81. > }
  82. > } else {
  83. > while (c++) {
  84. > x /= 2;
  85. > }
  86. > }
  87. > }
  88. > return x;
  89. > }
  90. >
  91. > >
  92. > > That algorithm was actually 4 times slower
  93. > > then the one below, and more
  94. > > code. It was accurate though.
  95. > >
  96. >
  97. > Sorry Pete, I wasn't looking very carefully.
  98. > When I converted your function
  99. > to double precision it's was much quicker, the best I've seen yet.
  100. */
  101. #endif