ImathMath.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Copyright Contributors to the OpenEXR Project.
  4. //
  5. //
  6. // Obsolete functions provided for compatibility, deprecated in favor
  7. // of std:: functions.
  8. //
  9. #ifndef INCLUDED_IMATHMATH_H
  10. #define INCLUDED_IMATHMATH_H
  11. #include "ImathNamespace.h"
  12. #include "ImathPlatform.h"
  13. #include <cmath>
  14. #include <limits>
  15. IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
  16. //----------------------------------------------------------------------------
  17. //
  18. // The deprecated Math<T> methods were intended to allow templated access to
  19. // math functions so that they would automatically choose either the double
  20. // (e.g. sin()) or float (e.g., sinf()) version.
  21. //
  22. // Beginning wth C++11, this is unnecessary, as std:: versions of all these
  23. // functions are available and are templated by type.
  24. //
  25. // We keep these old definitions for backward compatibility but encourage
  26. // users to prefer the std:: versions. Some day we may remove these
  27. // deprecated versions.
  28. //
  29. //----------------------------------------------------------------------------
  30. /// @cond Doxygen_Suppress
  31. template <class T> struct Math
  32. {
  33. IMATH_DEPRECATED("use std::math functions")
  34. IMATH_HOSTDEVICE
  35. static T acos (T x) { return std::acos (x); }
  36. IMATH_DEPRECATED("use std::math functions")
  37. IMATH_HOSTDEVICE
  38. static T asin (T x) { return std::asin (x); }
  39. IMATH_DEPRECATED("use std::math functions")
  40. IMATH_HOSTDEVICE
  41. static T atan (T x) { return std::atan (x); }
  42. IMATH_DEPRECATED("use std::math functions")
  43. IMATH_HOSTDEVICE
  44. static T atan2 (T x, T y) { return std::atan2 (x, y); }
  45. IMATH_DEPRECATED("use std::math functions")
  46. IMATH_HOSTDEVICE
  47. static T cos (T x) { return std::cos (x); }
  48. IMATH_DEPRECATED("use std::math functions")
  49. IMATH_HOSTDEVICE
  50. static T sin (T x) { return std::sin (x); }
  51. IMATH_DEPRECATED("use std::math functions")
  52. IMATH_HOSTDEVICE
  53. static T tan (T x) { return std::tan (x); }
  54. IMATH_DEPRECATED("use std::math functions")
  55. IMATH_HOSTDEVICE
  56. static T cosh (T x) { return std::cosh (x); }
  57. IMATH_DEPRECATED("use std::math functions")
  58. IMATH_HOSTDEVICE
  59. static T sinh (T x) { return std::sinh (x); }
  60. IMATH_DEPRECATED("use std::math functions")
  61. IMATH_HOSTDEVICE
  62. static T tanh (T x) { return std::tanh (x); }
  63. IMATH_DEPRECATED("use std::math functions")
  64. IMATH_HOSTDEVICE
  65. static T exp (T x) { return std::exp (x); }
  66. IMATH_DEPRECATED("use std::math functions")
  67. IMATH_HOSTDEVICE
  68. static T log (T x) { return std::log (x); }
  69. IMATH_DEPRECATED("use std::math functions")
  70. IMATH_HOSTDEVICE
  71. static T log10 (T x) { return std::log10 (x); }
  72. IMATH_DEPRECATED("use std::math functions")
  73. IMATH_HOSTDEVICE
  74. static T modf (T x, T* iptr)
  75. {
  76. T ival;
  77. T rval (std::modf (T (x), &ival));
  78. *iptr = ival;
  79. return rval;
  80. }
  81. IMATH_DEPRECATED("use std::math functions")
  82. IMATH_HOSTDEVICE
  83. static T pow (T x, T y) { return std::pow (x, y); }
  84. IMATH_DEPRECATED("use std::math functions")
  85. IMATH_HOSTDEVICE
  86. static T sqrt (T x) { return std::sqrt (x); }
  87. IMATH_DEPRECATED("use std::math functions")
  88. IMATH_HOSTDEVICE
  89. static T ceil (T x) { return std::ceil (x); }
  90. IMATH_DEPRECATED("use std::math functions")
  91. IMATH_HOSTDEVICE
  92. static T fabs (T x) { return std::fabs (x); }
  93. IMATH_DEPRECATED("use std::math functions")
  94. IMATH_HOSTDEVICE
  95. static T floor (T x) { return std::floor (x); }
  96. IMATH_DEPRECATED("use std::math functions")
  97. IMATH_HOSTDEVICE
  98. static T fmod (T x, T y) { return std::fmod (x, y); }
  99. IMATH_DEPRECATED("use std::math functions")
  100. IMATH_HOSTDEVICE
  101. static T hypot (T x, T y) { return std::hypot (x, y); }
  102. };
  103. /// @endcond
  104. /// Don Hatch's version of sin(x)/x, which is accurate for very small x.
  105. /// Returns 1 for x == 0.
  106. template <class T>
  107. IMATH_HOSTDEVICE inline T
  108. sinx_over_x (T x)
  109. {
  110. if (x * x < std::numeric_limits<T>::epsilon())
  111. return T (1);
  112. else
  113. return std::sin (x) / x;
  114. }
  115. /// Compare two numbers and test if they are "approximately equal":
  116. ///
  117. /// @return Ttrue if x1 is the same as x2 with an absolute error of
  118. /// no more than e:
  119. ///
  120. /// abs (x1 - x2) <= e
  121. template <class T>
  122. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
  123. equalWithAbsError (T x1, T x2, T e) IMATH_NOEXCEPT
  124. {
  125. return ((x1 > x2) ? x1 - x2 : x2 - x1) <= e;
  126. }
  127. /// Compare two numbers and test if they are "approximately equal":
  128. ///
  129. /// @return True if x1 is the same as x2 with an relative error of
  130. /// no more than e,
  131. ///
  132. /// abs (x1 - x2) <= e * x1
  133. template <class T>
  134. IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
  135. equalWithRelError (T x1, T x2, T e) IMATH_NOEXCEPT
  136. {
  137. return ((x1 > x2) ? x1 - x2 : x2 - x1) <= e * ((x1 > 0) ? x1 : -x1);
  138. }
  139. IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
  140. #endif // INCLUDED_IMATHMATH_H