ImathMath.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002-2012, 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 "ImathNamespace.h"
  81. #include <math.h>
  82. IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
  83. template <class T>
  84. struct Math
  85. {
  86. static T acos (T x) {return ::acos (double(x));}
  87. static T asin (T x) {return ::asin (double(x));}
  88. static T atan (T x) {return ::atan (double(x));}
  89. static T atan2 (T x, T y) {return ::atan2 (double(x), double(y));}
  90. static T cos (T x) {return ::cos (double(x));}
  91. static T sin (T x) {return ::sin (double(x));}
  92. static T tan (T x) {return ::tan (double(x));}
  93. static T cosh (T x) {return ::cosh (double(x));}
  94. static T sinh (T x) {return ::sinh (double(x));}
  95. static T tanh (T x) {return ::tanh (double(x));}
  96. static T exp (T x) {return ::exp (double(x));}
  97. static T log (T x) {return ::log (double(x));}
  98. static T log10 (T x) {return ::log10 (double(x));}
  99. static T modf (T x, T *iptr)
  100. {
  101. double ival;
  102. T rval( ::modf (double(x),&ival));
  103. *iptr = ival;
  104. return rval;
  105. }
  106. static T pow (T x, T y) {return ::pow (double(x), double(y));}
  107. static T sqrt (T x) {return ::sqrt (double(x));}
  108. static T ceil (T x) {return ::ceil (double(x));}
  109. static T fabs (T x) {return ::fabs (double(x));}
  110. static T floor (T x) {return ::floor (double(x));}
  111. static T fmod (T x, T y) {return ::fmod (double(x), double(y));}
  112. static T hypot (T x, T y) {return ::hypot (double(x), double(y));}
  113. };
  114. template <>
  115. struct Math<float>
  116. {
  117. static float acos (float x) {return ::acosf (x);}
  118. static float asin (float x) {return ::asinf (x);}
  119. static float atan (float x) {return ::atanf (x);}
  120. static float atan2 (float x, float y) {return ::atan2f (x, y);}
  121. static float cos (float x) {return ::cosf (x);}
  122. static float sin (float x) {return ::sinf (x);}
  123. static float tan (float x) {return ::tanf (x);}
  124. static float cosh (float x) {return ::coshf (x);}
  125. static float sinh (float x) {return ::sinhf (x);}
  126. static float tanh (float x) {return ::tanhf (x);}
  127. static float exp (float x) {return ::expf (x);}
  128. static float log (float x) {return ::logf (x);}
  129. static float log10 (float x) {return ::log10f (x);}
  130. static float modf (float x, float *y) {return ::modff (x, y);}
  131. static float pow (float x, float y) {return ::powf (x, y);}
  132. static float sqrt (float x) {return ::sqrtf (x);}
  133. static float ceil (float x) {return ::ceilf (x);}
  134. static float fabs (float x) {return ::fabsf (x);}
  135. static float floor (float x) {return ::floorf (x);}
  136. static float fmod (float x, float y) {return ::fmodf (x, y);}
  137. #if !defined(_MSC_VER)
  138. static float hypot (float x, float y) {return ::hypotf (x, y);}
  139. #else
  140. static float hypot (float x, float y) {return ::sqrtf(x*x + y*y);}
  141. #endif
  142. };
  143. //--------------------------------------------------------------------------
  144. // Don Hatch's version of sin(x)/x, which is accurate for very small x.
  145. // Returns 1 for x == 0.
  146. //--------------------------------------------------------------------------
  147. template <class T>
  148. inline T
  149. sinx_over_x (T x)
  150. {
  151. if (x * x < limits<T>::epsilon())
  152. return T (1);
  153. else
  154. return Math<T>::sin (x) / x;
  155. }
  156. //--------------------------------------------------------------------------
  157. // Compare two numbers and test if they are "approximately equal":
  158. //
  159. // equalWithAbsError (x1, x2, e)
  160. //
  161. // Returns true if x1 is the same as x2 with an absolute error of
  162. // no more than e,
  163. //
  164. // abs (x1 - x2) <= e
  165. //
  166. // equalWithRelError (x1, x2, e)
  167. //
  168. // Returns true if x1 is the same as x2 with an relative error of
  169. // no more than e,
  170. //
  171. // abs (x1 - x2) <= e * x1
  172. //
  173. //--------------------------------------------------------------------------
  174. template <class T>
  175. inline bool
  176. equalWithAbsError (T x1, T x2, T e)
  177. {
  178. return ((x1 > x2)? x1 - x2: x2 - x1) <= e;
  179. }
  180. template <class T>
  181. inline bool
  182. equalWithRelError (T x1, T x2, T e)
  183. {
  184. return ((x1 > x2)? x1 - x2: x2 - x1) <= e * ((x1 > 0)? x1: -x1);
  185. }
  186. IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
  187. #endif // INCLUDED_IMATHMATH_H