real.H 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /// @file real.H
  7. /// @brief Define real number type and related global definitions.
  8. #pragma once
  9. #include "ra/int_t.H"
  10. #include <limits>
  11. #include <cstdlib>
  12. #include <cmath>
  13. #include <algorithm>
  14. namespace ra {
  15. template <class T=double> constexpr T EPS = std::numeric_limits<T>::epsilon();
  16. constexpr double ALINF = std::numeric_limits<double>::max();
  17. constexpr double PINF = std::numeric_limits<double>::infinity();
  18. constexpr double QNAN = std::numeric_limits<double>::quiet_NaN();
  19. constexpr double PI = 3.14159265358979323846264338327950288419716939937510582;
  20. constexpr double PI2 = PI/2.;
  21. constexpr double EXP1 = 2.71828182845904523536028747135266249775724709369995957;
  22. constexpr double TAU = 2*PI;
  23. constexpr double TTAU = TAU*2;
  24. constexpr double TAU6 = TAU/6;
  25. constexpr double TAU12 = TAU/12;
  26. constexpr double I4PI = 1./TTAU;
  27. constexpr double SQRT2 = sqrt(2.);
  28. constexpr double ISQRT2 = 1/sqrt(2.);
  29. constexpr double SQRTPI = sqrt(PI);
  30. constexpr double LNPI = log(PI);
  31. constexpr double C0 = 2.99792458e8;
  32. constexpr double M0 = (4e-7)*PI;
  33. constexpr double E0 = 1./(M0*C0*C0);
  34. constexpr double ECHAR = 1.602176487e-19;
  35. constexpr double EMASS = 9.10938215e-31;
  36. constexpr double Z0 = 376.730313461;
  37. constexpr double LOG2E = 1.44269504088896340735992468100189213742664595415299;
  38. constexpr double LOGE2 = .693147180559945309417232121458176568075500134360255;
  39. constexpr double GOLDEN = 1.61803398874989484820458683436563811772030917980576; // (1+√5)/2.
  40. }
  41. // abs(int) is in ::, so why should I qualify abs(double)?
  42. // besides just as max() and min() are found for ra:: types w/o qualifying (through ADL) they should also be found for the POD types.
  43. using std::abs;
  44. using std::max;
  45. using std::min;
  46. using std::fma;
  47. using std::sqrt;
  48. using std::swap;
  49. using std::isfinite;
  50. using std::isinf;
  51. using std::pow;
  52. using std::exp;
  53. template <class T> inline constexpr T clamp(T const & a, T const & lo, T const & hi)
  54. {
  55. return max(lo, (min(hi, a)));
  56. }
  57. #define IS_REAL(T) (std::numeric_limits<T>::is_integer || std::is_floating_point<T>::value)
  58. // As an array op; special definitions for rank 0.
  59. template <class T> inline constexpr std::enable_if_t<IS_REAL(T), T> amax(T const x) { return x; }
  60. template <class T> inline constexpr std::enable_if_t<IS_REAL(T), T> amin(T const x) { return x; }
  61. // TODO see is_scalar in wedge.H>
  62. template <class T> inline constexpr std::enable_if_t<IS_REAL(T), T> sqr(T const x) { return x*x; }
  63. #undef IS_REAL
  64. #define FOR_FLOAT(T) \
  65. inline constexpr T real_part(T const x) { return x; } \
  66. inline constexpr T imag_part(T const x) { return 0.; } \
  67. inline constexpr T conj(T const x) { return x; } \
  68. inline constexpr T mul_conj(T const x, T const y) { return x*y; } \
  69. inline constexpr T fma_conj(T const a, T const b, T const c) { return a*b + c; } \
  70. inline constexpr T sqrm(T const x) { return x*x; } \
  71. inline constexpr T sqrm(T const x, T const y) { return sqrm(x-y); } \
  72. inline constexpr T norm2(T const x) { return std::abs(x); } \
  73. inline constexpr T norm2(T const x, T const y) { return std::abs(x-y); } \
  74. inline constexpr T abs(T const x, T const y) { return std::abs(x-y); } \
  75. inline constexpr T dot(T const x, T const y) { return x*y; } \
  76. inline constexpr T rel_error(T const a, T const b) { return (a==0. && b==0.) ? 0. : 2.*abs(a, b)/(abs(a)+abs(b)); }
  77. FOR_EACH(FOR_FLOAT, float, double)
  78. #undef FOR_FLOAT
  79. namespace ra {
  80. inline constexpr double rad2deg(double const r) { return r*(180./ra::PI); }
  81. inline constexpr double deg2rad(double const d) { return d*(ra::PI/180.); }
  82. } // namespace ra