Debian Codegraph @

src/Imath/ImathMath.h

raw
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")
40 IMATH_HOSTDEVICE
41 static T acos (T x) { return std::acos (x); }
42
43 IMATH_DEPRECATED("use std::math functions")
44 IMATH_HOSTDEVICE
45 static T asin (T x) { return std::asin (x); }
46
47 IMATH_DEPRECATED("use std::math functions")
48 IMATH_HOSTDEVICE
49 static T atan (T x) { return std::atan (x); }
50
51 IMATH_DEPRECATED("use std::math functions")
52 IMATH_HOSTDEVICE
53 static T atan2 (T x, T y) { return std::atan2 (x, y); }
54
55 IMATH_DEPRECATED("use std::math functions")
56 IMATH_HOSTDEVICE
57 static T cos (T x) { return std::cos (x); }
58
59 IMATH_DEPRECATED("use std::math functions")
60 IMATH_HOSTDEVICE
61 static T sin (T x) { return std::sin (x); }
62
63 IMATH_DEPRECATED("use std::math functions")
64 IMATH_HOSTDEVICE
65 static T tan (T x) { return std::tan (x); }
66
67 IMATH_DEPRECATED("use std::math functions")
68 IMATH_HOSTDEVICE
69 static T cosh (T x) { return std::cosh (x); }
70
71 IMATH_DEPRECATED("use std::math functions")
72 IMATH_HOSTDEVICE
73 static T sinh (T x) { return std::sinh (x); }
74
75 IMATH_DEPRECATED("use std::math functions")
76 IMATH_HOSTDEVICE
77 static T tanh (T x) { return std::tanh (x); }
78
79 IMATH_DEPRECATED("use std::math functions")
80 IMATH_HOSTDEVICE
81 static T exp (T x) { return std::exp (x); }
82
83 IMATH_DEPRECATED("use std::math functions")
84 IMATH_HOSTDEVICE
85 static T log (T x) { return std::log (x); }
86
87 IMATH_DEPRECATED("use std::math functions")
88 IMATH_HOSTDEVICE
89 static T log10 (T x) { return std::log10 (x); }
90
91 IMATH_DEPRECATED("use std::math functions")
92 IMATH_HOSTDEVICE
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")
102 IMATH_HOSTDEVICE
103 static T pow (T x, T y) { return std::pow (x, y); }
104
105 IMATH_DEPRECATED("use std::math functions")
106 IMATH_HOSTDEVICE
107 static T sqrt (T x) { return std::sqrt (x); }
108
109 IMATH_DEPRECATED("use std::math functions")
110 IMATH_HOSTDEVICE
111 static T ceil (T x) { return std::ceil (x); }
112
113 IMATH_DEPRECATED("use std::math functions")
114 IMATH_HOSTDEVICE
115 static T fabs (T x) { return std::fabs (x); }
116
117 IMATH_DEPRECATED("use std::math functions")
118 IMATH_HOSTDEVICE
119 static T floor (T x) { return std::floor (x); }
120
121 IMATH_DEPRECATED("use std::math functions")
122 IMATH_HOSTDEVICE
123 static T fmod (T x, T y) { return std::fmod (x, y); }
124
125 IMATH_DEPRECATED("use std::math functions")
126 IMATH_HOSTDEVICE
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
136 sinx_over_x (T x)
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
152 equalWithAbsError (T x1, T x2, T e) IMATH_NOEXCEPT
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
165 equalWithRelError (T x1, T x2, T e) IMATH_NOEXCEPT
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