NUMmath.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #ifndef _NUMmath_h_
  2. #define _NUMmath_h_
  3. /* NUMmath.h
  4. *
  5. * Copyright (C) 1992-2018 Paul Boersma
  6. *
  7. * This code is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * This code is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* More mathematical and numerical things than there are in <math.h>. */
  21. /********** Inherit all the ANSI functions from math.h **********/
  22. /* On the sgi, math.h declares some bessel functions. */
  23. /* The following statements suppress these declarations */
  24. /* so that the compiler will issue no warnings */
  25. /* when you redeclare y0 etc. in your code. */
  26. #ifdef sgi
  27. #define y0 sgi_y0
  28. #define y1 sgi_y1
  29. #define yn sgi_yn
  30. #define j0 sgi_j0
  31. #define j1 sgi_j1
  32. #define jn sgi_jn
  33. #endif
  34. #include <math.h>
  35. #ifdef sgi
  36. #undef y0
  37. #undef y1
  38. #undef yn
  39. #undef j0
  40. #undef j1
  41. #undef jn
  42. #endif
  43. #undef M_PI /* M_PI is a GNU extension, so not always available under -std=c++17 (one can also use NUMpi) */
  44. #define M_PI 3.1415926535897932384626433832795028841972
  45. void NUMshift (double *x, double xfrom, double xto);
  46. void NUMscale (double *x, double xminfrom, double xmaxfrom, double xminto, double xmaxto);
  47. /********** Constants **********
  48. * Forty-digit constants computed by e.g.:
  49. * bc -l
  50. * scale=42
  51. * print e(1)
  52. * Then rounding away the last two digits.
  53. */
  54. // print e(1)
  55. constexpr double NUMe = 2.7182818284590452353602874713526624977572;
  56. // print 1/l(2)
  57. constexpr double NUMlog2e = 1.4426950408889634073599246810018921374266;
  58. // print l(10)/l(2)
  59. constexpr double NUMlog2_10 = 3.3219280948873623478703194294893901758648;
  60. // print 1/l(10)
  61. constexpr double NUMlog10e = 0.4342944819032518276511289189166050822944;
  62. // print l(2)/l(10)
  63. constexpr double NUMlog10_2 = 0.3010299956639811952137388947244930267682;
  64. // print l(2)
  65. constexpr double NUMln2 = 0.6931471805599453094172321214581765680755;
  66. // print l(10)
  67. constexpr double NUMln10 = 2.3025850929940456840179914546843642076011;
  68. // print a(1)*8
  69. constexpr double NUM2pi = 6.2831853071795864769252867665590057683943;
  70. // print a(1)*4
  71. constexpr double NUMpi = 3.1415926535897932384626433832795028841972;
  72. // print a(1)*2
  73. constexpr double NUMpi_2 = 1.5707963267948966192313216916397514420986;
  74. // print a(1)
  75. constexpr double NUMpi_4 = 0.7853981633974483096156608458198757210493;
  76. // print 0.25/a(1)
  77. constexpr double NUM1_pi = 0.3183098861837906715377675267450287240689;
  78. // print 0.5/a(1)
  79. constexpr double NUM2_pi = 0.6366197723675813430755350534900574481378;
  80. // print sqrt(a(1)*4)
  81. constexpr double NUMsqrtpi = 1.7724538509055160272981674833411451827975;
  82. // print sqrt(a(1)*8)
  83. constexpr double NUMsqrt2pi = 2.5066282746310005024157652848110452530070;
  84. // print 1/sqrt(a(1)*8)
  85. constexpr double NUM1_sqrt2pi = 0.3989422804014326779399460599343818684759;
  86. // print 1/sqrt(a(1))
  87. constexpr double NUM2_sqrtpi = 1.1283791670955125738961589031215451716881;
  88. // print l(a(1)*4)
  89. constexpr double NUMlnpi = 1.1447298858494001741434273513530587116473;
  90. // print sqrt(2)
  91. constexpr double NUMsqrt2 = 1.4142135623730950488016887242096980785697;
  92. // print sqrt(0.5)
  93. constexpr double NUMsqrt1_2 = 0.7071067811865475244008443621048490392848;
  94. // print sqrt(3)
  95. constexpr double NUMsqrt3 = 1.7320508075688772935274463415058723669428;
  96. // print sqrt(5)
  97. constexpr double NUMsqrt5 = 2.2360679774997896964091736687312762354406;
  98. // print sqrt(6)
  99. constexpr double NUMsqrt6 = 2.4494897427831780981972840747058913919659;
  100. // print sqrt(7)
  101. constexpr double NUMsqrt7 = 2.6457513110645905905016157536392604257102;
  102. // print sqrt(8)
  103. constexpr double NUMsqrt8 = 2.8284271247461900976033774484193961571393;
  104. // print sqrt(10)
  105. constexpr double NUMsqrt10 = 3.1622776601683793319988935444327185337196;
  106. // print sqrt(5)/2-0.5
  107. constexpr double NUM_goldenSection = 0.6180339887498948482045868343656381177203;
  108. // The Euler-Mascheroni constant cannot be computed by bc.
  109. // Instead we use the 40 digits computed by Johann von Soldner in 1809.
  110. constexpr double NUM_euler = 0.5772156649015328606065120900824024310422;
  111. const double undefined = (0.0/0.0); // NaN
  112. /*
  113. isdefined() shall capture not only `undefined`, but all infinities and NaNs.
  114. This can be done with a single test for the set bits in 0x7FF0'0000'0000'0000,
  115. at least for 64-bit IEEE implementations. The correctness of this assumption is checked in sys/praat.cpp.
  116. The portable version of isdefined() involves both isinf() and isnan(), or perhaps just isfinite(),
  117. but that would be slower (as tested in fon/Praat_tests.cpp)
  118. and it would also run into problems on some platforms when both <cmath> and <math.h> are included,
  119. as in dwsys/NUMcomplex.cpp.
  120. */
  121. //inline bool isdefined (double x) { return ! isinf (x) && ! isnan (x); } /* portable */
  122. //inline bool isdefined (double x) { return isfinite (x); } /* portable */
  123. inline bool isdefined (double x) { return ((* (uint64 *) & x) & 0x7FF0'0000'0000'0000) != 0x7FF0'0000'0000'0000; }
  124. inline bool isundef (double x) { return ((* (uint64 *) & x) & 0x7FF0'0000'0000'0000) == 0x7FF0'0000'0000'0000; }
  125. /* End of file NUMmath.h */
  126. #endif