/src/FreeImage/Source/OpenEXR/Imath/ImathMath.h

https://bitbucket.org/cabalistic/ogredeps/ · C++ Header · 208 lines · 89 code · 22 blank · 97 comment · 1 complexity · 4cefb3318b10acdae9ca963459d36d8e MD5 · raw file

  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. #ifndef INCLUDED_IMATHMATH_H
  35. #define INCLUDED_IMATHMATH_H
  36. //----------------------------------------------------------------------------
  37. //
  38. // ImathMath.h
  39. //
  40. // This file contains template functions which call the double-
  41. // precision math functions defined in math.h (sin(), sqrt(),
  42. // exp() etc.), with specializations that call the faster
  43. // single-precision versions (sinf(), sqrtf(), expf() etc.)
  44. // when appropriate.
  45. //
  46. // Example:
  47. //
  48. // double x = Math<double>::sqrt (3); // calls ::sqrt(double);
  49. // float y = Math<float>::sqrt (3); // calls ::sqrtf(float);
  50. //
  51. // When would I want to use this?
  52. //
  53. // You may be writing a template which needs to call some function
  54. // defined in math.h, for example to extract a square root, but you
  55. // don't know whether to call the single- or the double-precision
  56. // version of this function (sqrt() or sqrtf()):
  57. //
  58. // template <class T>
  59. // T
  60. // glorp (T x)
  61. // {
  62. // return sqrt (x + 1); // should call ::sqrtf(float)
  63. // } // if x is a float, but we
  64. // // don't know if it is
  65. //
  66. // Using the templates in this file, you can make sure that
  67. // the appropriate version of the math function is called:
  68. //
  69. // template <class T>
  70. // T
  71. // glorp (T x, T y)
  72. // {
  73. // return Math<T>::sqrt (x + 1); // calls ::sqrtf(float) if x
  74. // } // is a float, ::sqrt(double)
  75. // // otherwise
  76. //
  77. //----------------------------------------------------------------------------
  78. #include "ImathPlatform.h"
  79. #include "ImathLimits.h"
  80. #include <math.h>
  81. namespace Imath {
  82. template <class T>
  83. struct Math
  84. {
  85. static T acos (T x) {return ::acos (double(x));}
  86. static T asin (T x) {return ::asin (double(x));}
  87. static T atan (T x) {return ::atan (double(x));}
  88. static T atan2 (T x, T y) {return ::atan2 (double(x), double(y));}
  89. static T cos (T x) {return ::cos (double(x));}
  90. static T sin (T x) {return ::sin (double(x));}
  91. static T tan (T x) {return ::tan (double(x));}
  92. static T cosh (T x) {return ::cosh (double(x));}
  93. static T sinh (T x) {return ::sinh (double(x));}
  94. static T tanh (T x) {return ::tanh (double(x));}
  95. static T exp (T x) {return ::exp (double(x));}
  96. static T log (T x) {return ::log (double(x));}
  97. static T log10 (T x) {return ::log10 (double(x));}
  98. static T modf (T x, T *iptr)
  99. {
  100. double ival;
  101. T rval( ::modf (double(x),&ival));
  102. *iptr = ival;
  103. return rval;
  104. }
  105. static T pow (T x, T y) {return ::pow (double(x), double(y));}
  106. static T sqrt (T x) {return ::sqrt (double(x));}
  107. static T ceil (T x) {return ::ceil (double(x));}
  108. static T fabs (T x) {return ::fabs (double(x));}
  109. static T floor (T x) {return ::floor (double(x));}
  110. static T fmod (T x, T y) {return ::fmod (double(x), double(y));}
  111. static T hypot (T x, T y) {return ::hypot (double(x), double(y));}
  112. };
  113. template <>
  114. struct Math<float>
  115. {
  116. static float acos (float x) {return ::acosf (x);}
  117. static float asin (float x) {return ::asinf (x);}
  118. static float atan (float x) {return ::atanf (x);}
  119. static float atan2 (float x, float y) {return ::atan2f (x, y);}
  120. static float cos (float x) {return ::cosf (x);}
  121. static float sin (float x) {return ::sinf (x);}
  122. static float tan (float x) {return ::tanf (x);}
  123. static float cosh (float x) {return ::coshf (x);}
  124. static float sinh (float x) {return ::sinhf (x);}
  125. static float tanh (float x) {return ::tanhf (x);}
  126. static float exp (float x) {return ::expf (x);}
  127. static float log (float x) {return ::logf (x);}
  128. static float log10 (float x) {return ::log10f (x);}
  129. static float modf (float x, float *y) {return ::modff (x, y);}
  130. static float pow (float x, float y) {return ::powf (x, y);}
  131. static float sqrt (float x) {return ::sqrtf (x);}
  132. static float ceil (float x) {return ::ceilf (x);}
  133. static float fabs (float x) {return ::fabsf (x);}
  134. static float floor (float x) {return ::floorf (x);}
  135. static float fmod (float x, float y) {return ::fmodf (x, y);}
  136. #if !defined(_MSC_VER)
  137. static float hypot (float x, float y) {return ::hypotf (x, y);}
  138. #else
  139. static float hypot (float x, float y) {return ::sqrtf(x*x + y*y);}
  140. #endif
  141. };
  142. //--------------------------------------------------------------------------
  143. // Don Hatch's version of sin(x)/x, which is accurate for very small x.
  144. // Returns 1 for x == 0.
  145. //--------------------------------------------------------------------------
  146. template <class T>
  147. inline T
  148. sinx_over_x (T x)
  149. {
  150. if (x * x < limits<T>::epsilon())
  151. return T (1);
  152. else
  153. return Math<T>::sin (x) / x;
  154. }
  155. //--------------------------------------------------------------------------
  156. // Compare two numbers and test if they are "approximately equal":
  157. //
  158. // equalWithAbsError (x1, x2, e)
  159. //
  160. // Returns true if x1 is the same as x2 with an absolute error of
  161. // no more than e,
  162. //
  163. // abs (x1 - x2) <= e
  164. //
  165. // equalWithRelError (x1, x2, e)
  166. //
  167. // Returns true if x1 is the same as x2 with an relative error of
  168. // no more than e,
  169. //
  170. // abs (x1 - x2) <= e * x1
  171. //
  172. //--------------------------------------------------------------------------
  173. template <class T>
  174. inline bool
  175. equalWithAbsError (T x1, T x2, T e)
  176. {
  177. return ((x1 > x2)? x1 - x2: x2 - x1) <= e;
  178. }
  179. template <class T>
  180. inline bool
  181. equalWithRelError (T x1, T x2, T e)
  182. {
  183. return ((x1 > x2)? x1 - x2: x2 - x1) <= e * ((x1 > 0)? x1: -x1);
  184. }
  185. } // namespace Imath
  186. #endif