/blitz/blitz/numinquire.h

https://github.com/qingguang/sdpd-blitz · C Header · 306 lines · 208 code · 53 blank · 45 comment · 0 complexity · e6e328834e1e6e03e6bef5ed32af9985 MD5 · raw file

  1. /***************************************************************************
  2. * blitz/numinquire.h Numeric inquiry functions
  3. *
  4. * $Id: numinquire.h,v 1.6 2005/11/01 02:42:23 julianc Exp $
  5. *
  6. * Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * Suggestions: blitz-dev@oonumerics.org
  19. * Bugs: blitz-bugs@oonumerics.org
  20. *
  21. * For more information, please see the Blitz++ Home Page:
  22. * http://oonumerics.org/blitz/
  23. *
  24. ***************************************************************************/
  25. /*
  26. * These numeric inquiry functions are provided as an alternative
  27. * to the somewhat klunky numeric_limits<T>::yadda_yadda syntax.
  28. * Where a similar Fortran 90 function exists, the same name has
  29. * been used.
  30. *
  31. * The argument in all cases is a dummy of the appropriate type
  32. * (double, int, etc.)
  33. *
  34. * These functions assume that numeric_limits<T> has been specialized
  35. * for the appropriate case. If not, the results are not useful.
  36. */
  37. #ifndef BZ_NUMINQUIRE_H
  38. #define BZ_NUMINQUIRE_H
  39. #ifndef BZ_BLITZ_H
  40. #include <blitz/blitz.h>
  41. #endif
  42. #ifndef BZ_HAVE_NUMERIC_LIMITS
  43. #include <blitz/limits-hack.h>
  44. #else
  45. #include <limits>
  46. #endif
  47. #ifndef BZ_RANGE_H
  48. #include <blitz/range.h>
  49. #endif
  50. BZ_NAMESPACE(blitz)
  51. /*
  52. * This traits class provides zero and one values for numeric
  53. * types. This was previously a template function with specializations,
  54. * but the specializations were causing multiply-defined symbols
  55. * at link time. TV 980226
  56. */
  57. template<typename T_numtype>
  58. struct _bz_OneZeroTraits {
  59. static inline T_numtype zero() { return 0; }
  60. static inline T_numtype one() { return 1; }
  61. };
  62. #ifdef BZ_HAVE_COMPLEX
  63. template<>
  64. struct _bz_OneZeroTraits<complex<float> > {
  65. static inline complex<float> zero() { return complex<float>(0.0f,0.0f); }
  66. static inline complex<float> one() { return complex<float>(1.0f,0.0f); }
  67. };
  68. template<>
  69. struct _bz_OneZeroTraits<complex<double> > {
  70. static inline complex<double> zero() { return complex<double>(0.0,0.0); }
  71. static inline complex<double> one() { return complex<double>(1.0,0.0); }
  72. };
  73. template<>
  74. struct _bz_OneZeroTraits<complex<long double> > {
  75. static inline complex<long double> zero()
  76. { return complex<long double>(0.0,0.0); }
  77. static inline complex<long double> one()
  78. { return complex<long double>(1.0,0.0); }
  79. };
  80. #endif // BZ_HAVE_COMPLEX
  81. template<typename T>
  82. inline T zero(T)
  83. {
  84. return _bz_OneZeroTraits<T>::zero();
  85. }
  86. template<typename T>
  87. inline T one(T)
  88. {
  89. return _bz_OneZeroTraits<T>::one();
  90. }
  91. template<typename T>
  92. inline int digits(T)
  93. {
  94. return numeric_limits<T>::digits;
  95. }
  96. template<typename T>
  97. inline int digits10(T)
  98. {
  99. return numeric_limits<T>::digits10;
  100. }
  101. template<typename T>
  102. inline T epsilon(T) BZ_THROW
  103. {
  104. return numeric_limits<T>::epsilon();
  105. }
  106. // neghuge() by Theodore Papadopoulo, to fix a problem with
  107. // max() reductions.
  108. template<typename T>
  109. inline T neghuge(T) BZ_THROW
  110. {
  111. return numeric_limits<T>::is_integer ? (numeric_limits<T>::min)()
  112. : - (numeric_limits<T>::max)();
  113. }
  114. template<typename T>
  115. inline T huge(T) BZ_THROW
  116. {
  117. return (numeric_limits<T>::max)();
  118. }
  119. template<typename T>
  120. inline T tiny(T) BZ_THROW
  121. {
  122. return (numeric_limits<T>::min)();
  123. }
  124. template<typename T>
  125. inline int max_exponent(T)
  126. {
  127. return numeric_limits<T>::max_exponent;
  128. }
  129. template<typename T>
  130. inline int min_exponent(T)
  131. {
  132. return numeric_limits<T>::min_exponent;
  133. }
  134. template<typename T>
  135. inline int min_exponent10(T)
  136. {
  137. return numeric_limits<T>::min_exponent10;
  138. }
  139. template<typename T>
  140. inline int max_exponent10(T)
  141. {
  142. return numeric_limits<T>::max_exponent10;
  143. }
  144. template<typename T>
  145. inline int precision(T)
  146. {
  147. return numeric_limits<T>::digits10;
  148. }
  149. template<typename T>
  150. inline int radix(T)
  151. {
  152. return numeric_limits<T>::radix;
  153. }
  154. template<typename T>
  155. inline Range range(T)
  156. {
  157. return Range(numeric_limits<T>::min_exponent10,
  158. numeric_limits<T>::max_exponent10);
  159. }
  160. template<typename T>
  161. inline bool is_signed(T) {
  162. return numeric_limits<T>::is_signed;
  163. }
  164. template<typename T>
  165. inline bool is_integer(T) {
  166. return numeric_limits<T>::is_integer;
  167. }
  168. template<typename T>
  169. inline bool is_exact(T) {
  170. return numeric_limits<T>::is_exact;
  171. }
  172. template<typename T>
  173. inline T round_error(T) BZ_THROW
  174. {
  175. return numeric_limits<T>::round_error();
  176. }
  177. template<typename T>
  178. inline bool has_infinity(T) {
  179. return numeric_limits<T>::has_infinity;
  180. }
  181. template<typename T>
  182. inline bool has_quiet_NaN(T) {
  183. return numeric_limits<T>::has_quiet_NaN;
  184. }
  185. template<typename T>
  186. inline bool has_signaling_NaN(T) {
  187. return numeric_limits<T>::has_signaling_NaN;
  188. }
  189. // Provided for non-US english users
  190. template<typename T>
  191. inline bool has_signalling_NaN(T) {
  192. return numeric_limits<T>::has_signaling_NaN;
  193. }
  194. template<typename T>
  195. inline bool has_denorm(T) {
  196. return numeric_limits<T>::has_denorm;
  197. }
  198. template<typename T>
  199. inline bool has_denorm_loss(T) {
  200. return numeric_limits<T>::has_denorm_loss;
  201. }
  202. template<typename T>
  203. inline T infinity(T) BZ_THROW
  204. {
  205. return numeric_limits<T>::infinity();
  206. }
  207. template<typename T>
  208. inline T quiet_NaN(T) BZ_THROW
  209. {
  210. return numeric_limits<T>::quiet_NaN();
  211. }
  212. template<typename T>
  213. inline T signaling_NaN(T) BZ_THROW
  214. {
  215. return numeric_limits<T>::signaling_NaN();
  216. }
  217. template<typename T>
  218. inline T signalling_NaN(T) BZ_THROW
  219. {
  220. return numeric_limits<T>::signaling_NaN();
  221. }
  222. template<typename T>
  223. inline T denorm_min(T) BZ_THROW
  224. {
  225. return numeric_limits<T>::denorm_min();
  226. }
  227. template<typename T>
  228. inline bool is_iec559(T) {
  229. return numeric_limits<T>::is_iec559;
  230. }
  231. template<typename T>
  232. inline bool is_bounded(T) {
  233. return numeric_limits<T>::is_bounded;
  234. }
  235. template<typename T>
  236. inline bool is_modulo(T) {
  237. return numeric_limits<T>::is_modulo;
  238. }
  239. template<typename T>
  240. inline bool traps(T) {
  241. return numeric_limits<T>::traps;
  242. }
  243. template<typename T>
  244. inline bool tinyness_before(T) {
  245. return numeric_limits<T>::tinyness_before;
  246. }
  247. template<typename T>
  248. inline BZ_STD_SCOPE(float_round_style) round_style(T)
  249. {
  250. return numeric_limits<T>::round_style;
  251. }
  252. BZ_NAMESPACE_END
  253. #endif // BZ_NUMINQUIRE_H