real.H 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // (c) Daniel Llorens - 2005, 2015
  2. // This library is free software; you can redistribute it and/or modify it under
  3. // the terms of the GNU Lesser General Public License as published by the Free
  4. // Software Foundation; either version 3 of the License, or (at your option) any
  5. // later version.
  6. #ifndef RA_REAL_H
  7. #define RA_REAL_H
  8. /// @file real.H
  9. /// @brief Define real number type and related global definitions.
  10. #include "ra/int_t.H"
  11. #include <limits>
  12. #include <cstdlib>
  13. #include <cmath>
  14. #include <algorithm>
  15. using real = double;
  16. // abs(int) is in ::, so why should I qualify abs(double)? also need max() and min() to be found for the POD types, as they are defined for others, and also found (through ADL).
  17. using std::abs;
  18. using std::max;
  19. using std::min;
  20. using std::isfinite;
  21. using std::fma;
  22. // using std::isinf; // there's apparently an ::isinf already
  23. #define IS_REAL(T) (std::numeric_limits<T>::is_integer || std::is_floating_point<T>::value)
  24. // As an array op; special definitions for rank 0.
  25. template <class T> inline std::enable_if_t<IS_REAL(T), T> amax(T const x) { return x; }
  26. template <class T> inline std::enable_if_t<IS_REAL(T), T> amin(T const x) { return x; }
  27. #undef IS_REAL
  28. // @TODO see is_scalar in wedge.H>
  29. template <class T, enableif_<std::is_integral<T>, int> =0> T sqr(T const x) { return x*x; }
  30. inline constexpr real sqr(real const x) { return x*x; }
  31. inline constexpr real real_part(real const x) { return x; }
  32. inline constexpr real imag_part(real const x) { return 0.; }
  33. inline constexpr real conj(real const x) { return x; }
  34. inline constexpr real mul_conj(real const x, real const y) { return x*y; }
  35. inline constexpr real fma_conj(real const a, real const b, real const c) { return a*b + c; }
  36. inline constexpr float sqrm(float const x) { return x*x; }
  37. inline constexpr real sqrm(real const x) { return x*x; }
  38. inline constexpr real sqrm(real const x, real const y) { return sqrm(x-y); }
  39. inline /* constexpr */ real norm2(real const x) { return std::abs(x); }
  40. inline /* constexpr */ real norm2(real const x, real const y) { return std::abs(x-y); }
  41. inline /* constexpr */ real abs(real const x, real const y) { return std::abs(x-y); }
  42. inline constexpr real dot(real const x, real const y) { return x*y; }
  43. inline void swap(real & a, real & b) { std::swap(a, b); }
  44. inline real rel_error(real const a, real const b)
  45. {
  46. return (a==0. && b==0.) ? 0. : 2.*abs(a, b)/(abs(a)+abs(b));
  47. }
  48. real const EPS = std::numeric_limits<real>::epsilon();
  49. real const ALINF = std::numeric_limits<real>::max();
  50. real const PINF = std::numeric_limits<real>::infinity();
  51. real const QNAN = std::numeric_limits<real>::quiet_NaN();
  52. // These TAU, PI, PI2 are rounded down.
  53. real const PI = 3.14159265358979323846264338327950288419716939937510582;
  54. real const PI2 = PI/2.;
  55. real const EXP1 = 2.71828182845904523536028747135266249775724709369995957;
  56. real const TAU = 2*PI;
  57. real const TTAU = TAU*2;
  58. real const TAU6 = TAU/6;
  59. real const TAU12 = TAU/12;
  60. /// 1/(4*pi).
  61. real const I4PI = 1./TTAU;
  62. real const SQRT2 = sqrt(2.);
  63. real const ISQRT2 = 1/sqrt(2.);
  64. real const SQRTPI = sqrt(PI);
  65. real const LNPI = log(PI);
  66. real const C0 = 2.99792458e8;
  67. real const M0 = (4e-7)*PI;
  68. real const E0 = 1./(sqrt(M0)*C0*C0);
  69. real const ECHAR = 1.602176487e-19;
  70. real const EMASS = 9.10938215e-31;
  71. /// Impedance of vacuum.
  72. real const Z0 = 376.730313461;
  73. real const LOG2E = 1.44269504088896340735992468100189213742664595415299;
  74. real const LOGE2 = .693147180559945309417232121458176568075500134360255;
  75. /// (1+sqrt(5))/2.
  76. real const GOLDEN = 1.61803398874989484820458683436563811772030917980576;
  77. inline real frand() { return real(random())/RAND_MAX; }
  78. inline int irand(int const p) { return int(frand()*p); }
  79. inline real rad2deg(real const r) { return r*(180./PI); }
  80. inline real deg2rad(real const d) { return d*(PI/180.); }
  81. #endif // RA_REAL_H