ImathLimits.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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_IMATHLIMITS_H
  35. #define INCLUDED_IMATHLIMITS_H
  36. //----------------------------------------------------------------
  37. //
  38. // Limitations of the basic C++ numerical data types
  39. //
  40. //----------------------------------------------------------------
  41. #include "ImathNamespace.h"
  42. #include <float.h>
  43. #include <limits.h>
  44. //------------------------------------------
  45. // In Windows, min and max are macros. Yay.
  46. //------------------------------------------
  47. #if defined _WIN32 || defined _WIN64
  48. #ifdef min
  49. #undef min
  50. #endif
  51. #ifdef max
  52. #undef max
  53. #endif
  54. #endif
  55. IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
  56. //-----------------------------------------------------------------
  57. //
  58. // Template class limits<T> returns information about the limits
  59. // of numerical data type T:
  60. //
  61. // min() largest possible negative value of type T
  62. //
  63. // max() largest possible positive value of type T
  64. //
  65. // smallest() smallest possible positive value of type T
  66. // (for float and double: smallest normalized
  67. // positive value)
  68. //
  69. // epsilon() smallest possible e of type T, for which
  70. // 1 + e != 1
  71. //
  72. // isIntegral() returns true if T is an integral type
  73. //
  74. // isSigned() returns true if T is signed
  75. //
  76. // Class limits<T> is useful to implement template classes or
  77. // functions which depend on the limits of a numerical type
  78. // which is not known in advance; for example:
  79. //
  80. // template <class T> max (T x[], int n)
  81. // {
  82. // T m = limits<T>::min();
  83. //
  84. // for (int i = 0; i < n; i++)
  85. // if (m < x[i])
  86. // m = x[i];
  87. //
  88. // return m;
  89. // }
  90. //
  91. // Class limits<T> has been implemented for the following types:
  92. //
  93. // char, signed char, unsigned char
  94. // short, unsigned short
  95. // int, unsigned int
  96. // long, unsigned long
  97. // float
  98. // double
  99. // long double
  100. //
  101. // Class limits<T> has only static member functions, all of which
  102. // are implemented as inlines. No objects of type limits<T> are
  103. // ever created.
  104. //
  105. //-----------------------------------------------------------------
  106. template <class T> struct limits
  107. {
  108. static T min();
  109. static T max();
  110. static T smallest();
  111. static T epsilon();
  112. static bool isIntegral();
  113. static bool isSigned();
  114. };
  115. //---------------
  116. // Implementation
  117. //---------------
  118. template <>
  119. struct limits <char>
  120. {
  121. static char min() {return CHAR_MIN;}
  122. static char max() {return CHAR_MAX;}
  123. static char smallest() {return 1;}
  124. static char epsilon() {return 1;}
  125. static bool isIntegral() {return true;}
  126. static bool isSigned() {return (char) ~0 < 0;}
  127. };
  128. template <>
  129. struct limits <signed char>
  130. {
  131. static signed char min() {return SCHAR_MIN;}
  132. static signed char max() {return SCHAR_MAX;}
  133. static signed char smallest() {return 1;}
  134. static signed char epsilon() {return 1;}
  135. static bool isIntegral() {return true;}
  136. static bool isSigned() {return true;}
  137. };
  138. template <>
  139. struct limits <unsigned char>
  140. {
  141. static unsigned char min() {return 0;}
  142. static unsigned char max() {return UCHAR_MAX;}
  143. static unsigned char smallest() {return 1;}
  144. static unsigned char epsilon() {return 1;}
  145. static bool isIntegral() {return true;}
  146. static bool isSigned() {return false;}
  147. };
  148. template <>
  149. struct limits <short>
  150. {
  151. static short min() {return SHRT_MIN;}
  152. static short max() {return SHRT_MAX;}
  153. static short smallest() {return 1;}
  154. static short epsilon() {return 1;}
  155. static bool isIntegral() {return true;}
  156. static bool isSigned() {return true;}
  157. };
  158. template <>
  159. struct limits <unsigned short>
  160. {
  161. static unsigned short min() {return 0;}
  162. static unsigned short max() {return USHRT_MAX;}
  163. static unsigned short smallest() {return 1;}
  164. static unsigned short epsilon() {return 1;}
  165. static bool isIntegral() {return true;}
  166. static bool isSigned() {return false;}
  167. };
  168. template <>
  169. struct limits <int>
  170. {
  171. static int min() {return INT_MIN;}
  172. static int max() {return INT_MAX;}
  173. static int smallest() {return 1;}
  174. static int epsilon() {return 1;}
  175. static bool isIntegral() {return true;}
  176. static bool isSigned() {return true;}
  177. };
  178. template <>
  179. struct limits <unsigned int>
  180. {
  181. static unsigned int min() {return 0;}
  182. static unsigned int max() {return UINT_MAX;}
  183. static unsigned int smallest() {return 1;}
  184. static unsigned int epsilon() {return 1;}
  185. static bool isIntegral() {return true;}
  186. static bool isSigned() {return false;}
  187. };
  188. template <>
  189. struct limits <long>
  190. {
  191. static long min() {return LONG_MIN;}
  192. static long max() {return LONG_MAX;}
  193. static long smallest() {return 1;}
  194. static long epsilon() {return 1;}
  195. static bool isIntegral() {return true;}
  196. static bool isSigned() {return true;}
  197. };
  198. template <>
  199. struct limits <unsigned long>
  200. {
  201. static unsigned long min() {return 0;}
  202. static unsigned long max() {return ULONG_MAX;}
  203. static unsigned long smallest() {return 1;}
  204. static unsigned long epsilon() {return 1;}
  205. static bool isIntegral() {return true;}
  206. static bool isSigned() {return false;}
  207. };
  208. template <>
  209. struct limits <float>
  210. {
  211. static float min() {return -FLT_MAX;}
  212. static float max() {return FLT_MAX;}
  213. static float smallest() {return FLT_MIN;}
  214. static float epsilon() {return FLT_EPSILON;}
  215. static bool isIntegral() {return false;}
  216. static bool isSigned() {return true;}
  217. };
  218. template <>
  219. struct limits <double>
  220. {
  221. static double min() {return -DBL_MAX;}
  222. static double max() {return DBL_MAX;}
  223. static double smallest() {return DBL_MIN;}
  224. static double epsilon() {return DBL_EPSILON;}
  225. static bool isIntegral() {return false;}
  226. static bool isSigned() {return true;}
  227. };
  228. template <>
  229. struct limits <long double>
  230. {
  231. static long double min() {return -LDBL_MAX;}
  232. static long double max() {return LDBL_MAX;}
  233. static long double smallest() {return LDBL_MIN;}
  234. static long double epsilon() {return LDBL_EPSILON;}
  235. static bool isIntegral() {return false;}
  236. static bool isSigned() {return true;}
  237. };
  238. IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
  239. #endif // INCLUDED_IMATHLIMITS_H