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 
132 /// Don Hatch's version of sin(x)/x, which is accurate for very small x.
133 /// Returns 1 for x == 0.
134 template <class T>
135 IMATH_HOSTDEVICE inline T
137 {
138  if (x * x < std::numeric_limits<T>::epsilon())
139  return T (1);
140  else
141  return std::sin (x) / x;
142 }
143 
144 /// Compare two numbers and test if they are "approximately equal":
145 ///
146 /// @return Ttrue if x1 is the same as x2 with an absolute error of
147 /// no more than e:
148 ///
149 /// abs (x1 - x2) <= e
150 template <class T>
151 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
153 {
154  return ((x1 > x2) ? x1 - x2 : x2 - x1) <= e;
155 }
156 
157 /// Compare two numbers and test if they are "approximately equal":
158 ///
159 /// @return True if x1 is the same as x2 with an relative error of
160 /// no more than e,
161 ///
162 /// abs (x1 - x2) <= e * x1
163 template <class T>
164 IMATH_HOSTDEVICE IMATH_CONSTEXPR14 inline bool
166 {
167  return ((x1 > x2) ? x1 - x2 : x2 - x1) <= e * ((x1 > 0) ? x1 : -x1);
168 }
169 
170 IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
171 
172 #endif // INCLUDED_IMATHMATH_H
IMATH_HOSTDEVICE IMATH_CONSTEXPR14 bool equalWithAbsError(T x1, T x2, T e) IMATH_NOEXCEPT
Definition: ImathMath.h:152
SYS_API double cos(double x)
Definition: SYS_FPUMath.h:69
SYS_API double fmod(double x, double y)
Definition: SYS_FPUMath.h:85
SYS_API double atan2(double y, double x)
Definition: SYS_FPUMath.h:79
#define IMATH_NOEXCEPT
Definition: ImathConfig.h:72
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:165
vfloat4 sqrt(const vfloat4 &a)
Definition: simd.h:7481
GLint y
Definition: glcorearb.h:103
SYS_API double log10(double x)
Definition: SYS_FPUMath.h:89
#define IMATH_HOSTDEVICE
Definition: ImathConfig.h:102
GLdouble GLdouble x2
Definition: glad.h:2349
ImageBuf OIIO_API pow(const ImageBuf &A, cspan< float > B, ROI roi={}, int nthreads=0)
SYS_API double asin(double x)
Definition: SYS_FPUMath.h:81
SYS_API double sinh(double x)
SYS_API double cosh(double x)
SYS_API double acos(double x)
Definition: SYS_FPUMath.h:83
SYS_API double hypot(double x, double y)
GLint GLenum GLint x
Definition: glcorearb.h:409
SYS_API double tanh(double x)
Definition: SYS_FPUMath.h:103
SYS_API double tan(double x)
Definition: SYS_FPUMath.h:75
IMATH_HOSTDEVICE constexpr int ceil(T x) IMATH_NOEXCEPT
Definition: ImathFun.h:119
#define IMATH_DEPRECATED(msg)
Definition: ImathConfig.h:152
SYS_API double atan(double x)
Definition: SYS_FPUMath.h:77
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER IMATH_HOSTDEVICE T sinx_over_x(T x)
Definition: ImathMath.h:136
OIIO_FORCEINLINE T log(const T &v)
Definition: simd.h:7688
SYS_API double sin(double x)
Definition: SYS_FPUMath.h:71