HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImathMath.h
Go to the documentation of this file.
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5 
6 //
7 // Obsolete functions provided for compatibility, deprecated in favor
8 // of std:: functions.
9 //
10 
11 #ifndef INCLUDED_IMATHMATH_H
12 #define INCLUDED_IMATHMATH_H
13 
14 #include "ImathNamespace.h"
15 #include "ImathPlatform.h"
16 #include <cmath>
17 #include <limits>
18 
19 IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
20 
21 //----------------------------------------------------------------------------
22 //
23 // The deprecated Math<T> methods were intended to allow templated access to
24 // math functions so that they would automatically choose either the double
25 // (e.g. sin()) or float (e.g., sinf()) version.
26 //
27 // Beginning wth C++11, this is unnecessary, as std:: versions of all these
28 // functions are available and are templated by type.
29 //
30 // We keep these old definitions for backward compatibility but encourage
31 // users to prefer the std:: versions. Some day we may remove these
32 // deprecated versions.
33 //
34 //----------------------------------------------------------------------------
35 
36 /// @cond Doxygen_Suppress
37 template <class T> struct Math
38 {
39  IMATH_DEPRECATED ("use std::math functions")
41  static T acos (T x) { return std::acos (x); }
42 
43  IMATH_DEPRECATED ("use std::math functions")
45  static T asin (T x) { return std::asin (x); }
46 
47  IMATH_DEPRECATED ("use std::math functions")
49  static T atan (T x) { return std::atan (x); }
50 
51  IMATH_DEPRECATED ("use std::math functions")
53  static T atan2 (T x, T y) { return std::atan2 (x, y); }
54 
55  IMATH_DEPRECATED ("use std::math functions")
57  static T cos (T x) { return std::cos (x); }
58 
59  IMATH_DEPRECATED ("use std::math functions")
61  static T sin (T x) { return std::sin (x); }
62 
63  IMATH_DEPRECATED ("use std::math functions")
65  static T tan (T x) { return std::tan (x); }
66 
67  IMATH_DEPRECATED ("use std::math functions")
69  static T cosh (T x) { return std::cosh (x); }
70 
71  IMATH_DEPRECATED ("use std::math functions")
73  static T sinh (T x) { return std::sinh (x); }
74 
75  IMATH_DEPRECATED ("use std::math functions")
77  static T tanh (T x) { return std::tanh (x); }
78 
79  IMATH_DEPRECATED ("use std::math functions")
81  static T exp (T x) { return std::exp (x); }
82 
83  IMATH_DEPRECATED ("use std::math functions")
85  static T log (T x) { return std::log (x); }
86 
87  IMATH_DEPRECATED ("use std::math functions")
89  static T log10 (T x) { return std::log10 (x); }
90 
91  IMATH_DEPRECATED ("use std::math functions")
93  static T modf (T x, T* iptr)
94  {
95  T ival;
96  T rval (std::modf (T (x), &ival));
97  *iptr = ival;
98  return rval;
99  }
100 
101  IMATH_DEPRECATED ("use std::math functions")
103  static T pow (T x, T y) { return std::pow (x, y); }
104 
105  IMATH_DEPRECATED ("use std::math functions")
107  static T sqrt (T x) { return std::sqrt (x); }
108 
109  IMATH_DEPRECATED ("use std::math functions")
111  static T ceil (T x) { return std::ceil (x); }
112 
113  IMATH_DEPRECATED ("use std::math functions")
115  static T fabs (T x) { return std::fabs (x); }
116 
117  IMATH_DEPRECATED ("use std::math functions")
119  static T floor (T x) { return std::floor (x); }
120 
121  IMATH_DEPRECATED ("use std::math functions")
123  static T fmod (T x, T y) { return std::fmod (x, y); }
124 
125  IMATH_DEPRECATED ("use std::math functions")
127  static T hypot (T x, T y) { return std::hypot (x, y); }
128 };
129 /// @endcond
130 
131 /// Don Hatch's version of sin(x)/x, which is accurate for very small x.
132 /// Returns 1 for x == 0.
133 template <class T>
134 IMATH_HOSTDEVICE inline T
136 {
137  if (x * x < std::numeric_limits<T>::epsilon ())
138  return T (1);
139  else
140  return std::sin (x) / x;
141 }
142 
143 /// Compare two numbers and test if they are "approximately equal":
144 ///
145 /// @return Ttrue if x1 is the same as x2 with an absolute error of
146 /// no more than e:
147 ///
148 /// abs (x1 - x2) <= e
149 template <class T>
150 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
152 {
153  return ((x1 > x2) ? x1 - x2 : x2 - x1) <= e;
154 }
155 
156 /// Compare two numbers and test if they are "approximately equal":
157 ///
158 /// @return True if x1 is the same as x2 with an relative error of
159 /// no more than e,
160 ///
161 /// abs (x1 - x2) <= e * x1
162 template <class T>
163 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
165 {
166  return ((x1 > x2) ? x1 - x2 : x2 - x1) <= e * ((x1 > 0) ? x1 : -x1);
167 }
168 
169 IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
170 
171 #endif // INCLUDED_IMATHMATH_H
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool equalWithAbsError(T x1, T x2, T e) IMATH_NOEXCEPT
Definition: ImathMath.h:151
#define IMATH_NOEXCEPT
Definition: ImathConfig.h:78
IMATH_HOSTDEVICE constexpr int floor(T x) IMATH_NOEXCEPT
Definition: ImathFun.h:112
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool equalWithRelError(T x1, T x2, T e) IMATH_NOEXCEPT
Definition: ImathMath.h:164
vfloat4 sqrt(const vfloat4 &a)
Definition: simd.h:7694
GLint y
Definition: glcorearb.h:103
#define IMATH_HOSTDEVICE
Definition: ImathConfig.h:108
GLdouble GLdouble x2
Definition: glad.h:2349
ImageBuf OIIO_API pow(const ImageBuf &A, cspan< float > B, ROI roi={}, int nthreads=0)
GLint GLenum GLint x
Definition: glcorearb.h:409
IMATH_HOSTDEVICE constexpr int ceil(T x) IMATH_NOEXCEPT
Definition: ImathFun.h:119
#define IMATH_DEPRECATED(msg)
Definition: ImathConfig.h:156
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER IMATH_HOSTDEVICE T sinx_over_x(T x)
Definition: ImathMath.h:135
OIIO_FORCEINLINE T log(const T &v)
Definition: simd.h:7905