Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_ScalarTraits.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 // NOTE: Before adding specializations of ScalarTraits, make sure that they do
43 // not duplicate specializations already present in PyTrilinos (see
44 // packages/PyTrilinos/src/Teuchos_Traits.i)
45 
46 // NOTE: halfPrecision and doublePrecision are not currently implemented for
47 // ARPREC, GMP or the ordinal types (e.g., int, char)
48 
49 #ifndef _TEUCHOS_SCALARTRAITS_HPP_
50 #define _TEUCHOS_SCALARTRAITS_HPP_
51 
56 #include "Teuchos_ConfigDefs.hpp"
57 
58 #ifdef HAVE_TEUCHOS_ARPREC
59 #include <arprec/mp_real.h>
60 #endif
61 
62 #ifdef HAVE_TEUCHOS_QD
63 #include <qd/qd_real.h>
64 #include <qd/dd_real.h>
65 #endif
66 
67 #ifdef HAVE_TEUCHOS_GNU_MP
68 #include <gmp.h>
69 #include <gmpxx.h>
70 #endif
71 
72 
74 
75 
76 namespace Teuchos {
77 
78 
79 #ifndef DOXYGEN_SHOULD_SKIP_THIS
80 
81 
82 TEUCHOSCORE_LIB_DLL_EXPORT
83 void throwScalarTraitsNanInfError( const std::string &errMsg );
84 
85 
86 template<class Scalar>
87 bool generic_real_isnaninf(const Scalar &x)
88 {
89  typedef std::numeric_limits<Scalar> NL;
90  // IEEE says this should fail for NaN (not all compilers do not obey IEEE)
91  const Scalar tol = 1.0; // Any (bounded) number should do!
92  if (!(x <= tol) && !(x > tol)) return true;
93  // Use fact that Inf*0 = NaN (again, all compilers do not obey IEEE)
94  Scalar z = static_cast<Scalar>(0.0) * x;
95  if (!(z <= tol) && !(z > tol)) return true;
96  // As a last result use comparisons
97  if (x == NL::infinity() || x == -NL::infinity()) return true;
98  // We give up and assume the number is finite
99  return false;
100 }
101 
102 
103 #define TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR( VALUE, MSG ) \
104  if (isnaninf(VALUE)) { \
105  std::ostringstream omsg; \
106  omsg << MSG; \
107  Teuchos::throwScalarTraitsNanInfError(omsg.str()); \
108  }
109 
110 
111 template<>
112 struct ScalarTraits<char>
113 {
114  typedef char magnitudeType;
115  typedef char halfPrecision;
116  typedef char doublePrecision;
117  static const bool isComplex = false;
118  static const bool isOrdinal = true;
119  static const bool isComparable = true;
120  static const bool hasMachineParameters = false;
121  // Not defined: eps(), sfmin(), base(), prec(), t(), rnd(), emin(), rmin(), emax(), rmax()
122  static inline magnitudeType magnitude(char a) { return static_cast<char>(std::fabs(static_cast<double>(a))); }
123  static inline char zero() { return 0; }
124  static inline char one() { return 1; }
125  static inline char conjugate(char x) { return x; }
126  static inline char real(char x) { return x; }
127  static inline char imag(char) { return 0; }
128  static inline bool isnaninf(char ) { return false; }
129  static inline void seedrandom(unsigned int s) {
130  std::srand(s);
131 #ifdef __APPLE__
132  // throw away first random number to address bug 3655
133  // http://software.sandia.gov/bugzilla/show_bug.cgi?id=3655
134  random();
135 #endif
136  }
137  //static inline char random() { return (-1 + 2*rand()); } // RAB: This version should be used to be consistent with others
138  static inline char random() { return std::rand(); } // RAB: This version should be used for an unsigned char, not char
139  static inline std::string name() { return "char"; }
140  static inline char squareroot(char x) { return (char) std::sqrt((double) x); }
141  static inline char pow(char x, char y) { return (char) std::pow((double)x,(double)y); }
142  static inline char log(char x) { return static_cast<char> (std::log (static_cast<double> (x))); }
143  static inline char log10(char x) { return static_cast<char> (std::log10 (static_cast<double> (x))); }
144 };
145 
146 
147 template<>
148 struct ScalarTraits<short int>
149 {
150  typedef short int magnitudeType;
151  typedef short int halfPrecision;
152  typedef short int doublePrecision;
153  static const bool isComplex = false;
154  static const bool isOrdinal = true;
155  static const bool isComparable = true;
156  static const bool hasMachineParameters = false;
157  // Not defined: eps(), sfmin(), base(), prec(), t(), rnd(), emin(), rmin(), emax(), rmax()
158  static inline magnitudeType magnitude(short int a) { return static_cast<short int>(std::fabs(static_cast<double>(a))); }
159  static inline short int zero() { return 0; }
160  static inline short int one() { return 1; }
161  static inline short int conjugate(short int x) { return x; }
162  static inline short int real(short int x) { return x; }
163  static inline short int imag(short int) { return 0; }
164  static inline bool isnaninf(short int) { return false; }
165  static inline void seedrandom(unsigned int s) {
166  std::srand(s);
167 #ifdef __APPLE__
168  // throw away first random number to address bug 3655
169  // http://software.sandia.gov/bugzilla/show_bug.cgi?id=3655
170  random();
171 #endif
172  }
173  //static inline int random() { return (-1 + 2*rand()); } // RAB: This version should be used to be consistent with others
174  static inline short int random() { return std::rand(); } // RAB: This version should be used for an unsigned int, not int
175  static inline std::string name() { return "short int"; }
176  static inline short int squareroot(short int x) { return (short int) std::sqrt((double) x); }
177  static inline short int pow(short int x, short int y) { return (short int) std::pow((double)x,(double)y); }
178  static inline short int log(short int x) { return static_cast<short int> (std::log (static_cast<double> (x))); }
179  static inline short int log10(short int x) { return static_cast<short int> (std::log10 (static_cast<double> (x))); }
180 };
181 
182 template<>
183 struct ScalarTraits<unsigned short int>
184 {
185  typedef unsigned short int magnitudeType;
186  typedef unsigned short int halfPrecision;
187  typedef unsigned short int doublePrecision;
188  static const bool isComplex = false;
189  static const bool isOrdinal = true;
190  static const bool isComparable = true;
191  static const bool hasMachineParameters = false;
192  // Not defined: eps(), sfmin(), base(), prec(), t(), rnd(), emin(), rmin(), emax(), rmax()
193  static inline magnitudeType magnitude(unsigned short int a) { return static_cast<unsigned short int>(std::fabs(static_cast<double>(a))); }
194  static inline unsigned short int zero() { return 0; }
195  static inline unsigned short int one() { return 1; }
196  static inline unsigned short int conjugate(unsigned short int x) { return x; }
197  static inline unsigned short int real(unsigned short int x) { return x; }
198  static inline unsigned short int imag(unsigned short int) { return 0; }
199  static inline bool isnaninf(unsigned short int) { return false; }
200  static inline void seedrandom(unsigned int s) {
201  std::srand(s);
202 #ifdef __APPLE__
203  // throw away first random number to address bug 3655
204  // http://software.sandia.gov/bugzilla/show_bug.cgi?id=3655
205  random();
206 #endif
207  }
208  //static inline int random() { return (-1 + 2*rand()); } // RAB: This version should be used to be consistent with others
209  static inline unsigned short int random() { return std::rand(); } // RAB: This version should be used for an unsigned int, not int
210  static inline std::string name() { return "unsigned short int"; }
211  static inline unsigned short int squareroot(unsigned short int x) { return (unsigned short int) std::sqrt((double) x); }
212  static inline unsigned short int pow(unsigned short int x, unsigned short int y) { return (unsigned short int) std::pow((double)x,(double)y); }
213  static inline unsigned short int log(unsigned short int x) { return static_cast<unsigned short int> (std::log (static_cast<double> (x))); }
214  static inline unsigned short int log10(unsigned short int x) { return static_cast<unsigned short int> (std::log10 (static_cast<double> (x))); }
215 };
216 
217 
218 template<>
219 struct ScalarTraits<int>
220 {
221  typedef int magnitudeType;
222  typedef int halfPrecision;
223  typedef int doublePrecision;
224  static const bool isComplex = false;
225  static const bool isOrdinal = true;
226  static const bool isComparable = true;
227  static const bool hasMachineParameters = false;
228  // Not defined: eps(), sfmin(), base(), prec(), t(), rnd(), emin(), rmin(), emax(), rmax()
229  static inline magnitudeType magnitude(int a) { return static_cast<int>(std::fabs(static_cast<double>(a))); }
230  static inline int zero() { return 0; }
231  static inline int one() { return 1; }
232  static inline int conjugate(int x) { return x; }
233  static inline int real(int x) { return x; }
234  static inline int imag(int) { return 0; }
235  static inline bool isnaninf(int) { return false; }
236  static inline void seedrandom(unsigned int s) {
237  std::srand(s);
238 #ifdef __APPLE__
239  // throw away first random number to address bug 3655
240  // http://software.sandia.gov/bugzilla/show_bug.cgi?id=3655
241  random();
242 #endif
243  }
244  //static inline int random() { return (-1 + 2*rand()); } // RAB: This version should be used to be consistent with others
245  static inline int random() { return std::rand(); } // RAB: This version should be used for an unsigned int, not int
246  static inline std::string name() { return "int"; }
247  static inline int squareroot(int x) { return (int) std::sqrt((double) x); }
248  static inline int pow(int x, int y) { return (int) std::pow((double)x,(double)y); }
249  static inline int log(int x) { return static_cast<int> (std::log (static_cast<double> (x))); }
250  static inline int log10(int x) { return static_cast<int> (std::log10 (static_cast<double> (x))); }
251 };
252 
253 
254 template<>
255 struct ScalarTraits<unsigned int>
256 {
257  typedef unsigned int magnitudeType;
258  typedef unsigned int halfPrecision;
259  typedef unsigned int doublePrecision;
260  static const bool isComplex = false;
261  static const bool isOrdinal = true;
262  static const bool isComparable = true;
263  static const bool hasMachineParameters = false;
264  // Not defined: eps(), sfmin(), base(), prec(), t(), rnd(), emin(), rmin(), emax(), rmax()
265  static inline magnitudeType magnitude(unsigned int a) { return static_cast<unsigned int>(std::fabs(static_cast<double>(a))); }
266  static inline unsigned int zero() { return 0; }
267  static inline unsigned int one() { return 1; }
268  static inline unsigned int conjugate(unsigned int x) { return x; }
269  static inline unsigned int real(unsigned int x) { return x; }
270  static inline unsigned int imag(unsigned int) { return 0; }
271  static inline bool isnaninf(unsigned int) { return false; }
272  static inline void seedrandom(unsigned int s) {
273  std::srand(s);
274 #ifdef __APPLE__
275  // throw away first random number to address bug 3655
276  // http://software.sandia.gov/bugzilla/show_bug.cgi?id=3655
277  random();
278 #endif
279  }
280  //static inline int random() { return (-1 + 2*rand()); } // RAB: This version should be used to be consistent with others
281  static inline unsigned int random() { return std::rand(); } // RAB: This version should be used for an unsigned int, not int
282  static inline std::string name() { return "unsigned int"; }
283  static inline unsigned int squareroot(unsigned int x) { return (unsigned int) std::sqrt((double) x); }
284  static inline unsigned int pow(unsigned int x, unsigned int y) { return (unsigned int) std::pow((double)x,(double)y); }
285  static inline unsigned int log(unsigned int x) { return static_cast<unsigned int> (std::log (static_cast<double> (x))); }
286  static inline unsigned int log10(unsigned int x) { return static_cast<unsigned int> (std::log10 (static_cast<double> (x))); }
287 };
288 
289 
290 template<>
291 struct ScalarTraits<long int>
292 {
293  typedef long int magnitudeType;
294  typedef long int halfPrecision;
295  typedef long int doublePrecision;
296  static const bool isComplex = false;
297  static const bool isOrdinal = true;
298  static const bool isComparable = true;
299  static const bool hasMachineParameters = false;
300  // Not defined: eps(), sfmin(), base(), prec(), t(), rnd(), emin(), rmin(), emax(), rmax()
301  static inline magnitudeType magnitude(long int a) { return static_cast<long int>(std::fabs(static_cast<double>(a))); }
302  static inline long int zero() { return 0; }
303  static inline long int one() { return 1; }
304  static inline long int conjugate(long int x) { return x; }
305  static inline long int real(long int x) { return x; }
306  static inline long int imag(long int) { return 0; }
307  static inline bool isnaninf(long int) { return false; }
308  static inline void seedrandom(unsigned int s) {
309  std::srand(s);
310 #ifdef __APPLE__
311  // throw away first random number to address bug 3655
312  // http://software.sandia.gov/bugzilla/show_bug.cgi?id=3655
313  random();
314 #endif
315  }
316  //static inline int random() { return (-1 + 2*rand()); } // RAB: This version should be used to be consistent with others
317  static inline long int random() { return std::rand(); } // RAB: This version should be used for an unsigned int, not int
318  static inline std::string name() { return "long int"; }
319  static inline long int squareroot(long int x) { return (long int) std::sqrt((double) x); }
320  static inline long int pow(long int x, long int y) { return (long int) std::pow((double)x,(double)y); }
321  // Note: Depending on the number of bits in long int, the cast from
322  // long int to double may not be exact.
323  static inline long int log(long int x) { return static_cast<long int> (std::log (static_cast<double> (x))); }
324  static inline long int log10(long int x) { return static_cast<long int> (std::log10 (static_cast<double> (x))); }
325 };
326 
327 
328 template<>
329 struct ScalarTraits<long unsigned int>
330 {
331  typedef long unsigned int magnitudeType;
332  typedef long unsigned int halfPrecision;
333  typedef long unsigned int doublePrecision;
334  static const bool isComplex = false;
335  static const bool isOrdinal = true;
336  static const bool isComparable = true;
337  static const bool hasMachineParameters = false;
338  // Not defined: eps(), sfmin(), base(), prec(), t(), rnd(), emin(), rmin(), emax(), rmax()
339  static inline magnitudeType magnitude(long unsigned int a) { return static_cast<long unsigned int>(std::fabs(static_cast<double>(a))); }
340  static inline long unsigned int zero() { return 0; }
341  static inline long unsigned int one() { return 1; }
342  static inline long unsigned int conjugate(long unsigned int x) { return x; }
343  static inline long unsigned int real(long unsigned int x) { return x; }
344  static inline long unsigned int imag(long unsigned int) { return 0; }
345  static inline bool isnaninf(long unsigned int) { return false; }
346  static inline void seedrandom(unsigned int s) {
347  std::srand(s);
348 #ifdef __APPLE__
349  // throw away first random number to address bug 3655
350  // http://software.sandia.gov/bugzilla/show_bug.cgi?id=3655
351  random();
352 #endif
353  }
354  //static inline int random() { return (-1 + 2*rand()); } // RAB: This version should be used to be consistent with others
355  static inline long unsigned int random() { return std::rand(); } // RAB: This version should be used for an unsigned int, not int
356  static inline std::string name() { return "long unsigned int"; }
357  static inline long unsigned int squareroot(long unsigned int x) { return (long unsigned int) std::sqrt((double) x); }
358  static inline long unsigned int pow(long unsigned int x, long unsigned int y) { return (long unsigned int) std::pow((double)x,(double)y); }
359  // Note: Depending on the number of bits in long unsigned int, the
360  // cast from long unsigned int to double may not be exact.
361  static inline long unsigned int log(long unsigned int x) { return static_cast<long unsigned int> (std::log (static_cast<double> (x))); }
362  static inline long unsigned int log10(long unsigned int x) { return static_cast<long unsigned int> (std::log10 (static_cast<double> (x))); }
363 };
364 
365 
366 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
367 template<>
368 struct ScalarTraits<long long int>
369 {
370  typedef long long int magnitudeType;
371  typedef long long int halfPrecision;
372  typedef long long int doublePrecision;
373  static const bool isComplex = false;
374  static const bool isOrdinal = true;
375  static const bool isComparable = true;
376  static const bool hasMachineParameters = false;
377  // Not defined: eps(), sfmin(), base(), prec(), t(), rnd(), emin(), rmin(), emax(), rmax()
378  static inline magnitudeType magnitude(long long int a) { return static_cast<long long int>(std::fabs(static_cast<double>(a))); }
379  static inline long long int zero() { return 0; }
380  static inline long long int one() { return 1; }
381  static inline long long int conjugate(long long int x) { return x; }
382  static inline long long int real(long long int x) { return x; }
383  static inline long long int imag(long long int) { return 0; }
384  static inline bool isnaninf(long long int) { return false; }
385  static inline void seedrandom(unsigned int s) {
386  std::srand(s);
387 #ifdef __APPLE__
388  // throw away first random number to address bug 3655
389  // http://software.sandia.gov/bugzilla/show_bug.cgi?id=3655
390  random();
391 #endif
392  }
393  //static inline int random() { return (-1 + 2*rand()); } // RAB: This version should be used to be consistent with others
394  static inline long long int random() { return std::rand(); } // RAB: This version should be used for an unsigned int, not int
395  static inline std::string name() { return "long long int"; }
396  static inline long long int squareroot(long long int x) { return (long long int) std::sqrt((double) x); }
397  static inline long long int pow(long long int x, long long int y) { return (long long int) std::pow((double)x,(double)y); }
398  // Note: Depending on the number of bits in long long int, the cast
399  // from long long int to double may not be exact.
400  static inline long long int log(long long int x) { return static_cast<long long int> (std::log (static_cast<double> (x))); }
401  static inline long long int log10(long long int x) { return static_cast<long long int> (std::log10 (static_cast<double> (x))); }
402 };
403 
404 template<>
405 struct ScalarTraits<unsigned long long int>
406 {
407  typedef unsigned long long int magnitudeType;
408  typedef unsigned long long int halfPrecision;
409  typedef unsigned long long int doublePrecision;
410  static const bool isComplex = false;
411  static const bool isOrdinal = true;
412  static const bool isComparable = true;
413  static const bool hasMachineParameters = false;
414  // Not defined: eps(), sfmin(), base(), prec(), t(), rnd(), emin(), rmin(), emax(), rmax()
415  static inline magnitudeType magnitude(unsigned long long int a) { return static_cast<unsigned long long int>(std::fabs(static_cast<double>(a))); }
416  static inline unsigned long long int zero() { return 0; }
417  static inline unsigned long long int one() { return 1; }
418  static inline unsigned long long int conjugate(unsigned long long int x) { return x; }
419  static inline unsigned long long int real(unsigned long long int x) { return x; }
420  static inline unsigned long long int imag(unsigned long long int) { return 0; }
421  static inline bool isnaninf(unsigned long long int) { return false; }
422  static inline void seedrandom(unsigned int s) {
423  std::srand(s);
424 #ifdef __APPLE__
425  // throw away first random number to address bug 3655
426  // http://software.sandia.gov/bugzilla/show_bug.cgi?id=3655
427  random();
428 #endif
429  }
430  //static inline int random() { return (-1 + 2*rand()); } // RAB: This version should be used to be consistent with others
431  static inline unsigned long long int random() { return std::rand(); } // RAB: This version should be used for an unsigned int, not int
432  static inline std::string name() { return "unsigned long long int"; }
433  static inline unsigned long long int squareroot(unsigned long long int x) { return (unsigned long long int) std::sqrt((double) x); }
434  static inline unsigned long long int pow(unsigned long long int x, unsigned long long int y) { return (unsigned long long int) std::pow((double)x,(double)y); }
435  // Note: Depending on the number of bits in unsigned long long int,
436  // the cast from unsigned long long int to double may not be exact.
437  static inline unsigned long long int log(unsigned long long int x) { return static_cast<unsigned long long int> (std::log (static_cast<double> (x))); }
438  static inline unsigned long long int log10(unsigned long long int x) { return static_cast<unsigned long long int> (std::log10 (static_cast<double> (x))); }
439 };
440 #endif // HAVE_TEUCHOS_LONG_LONG_INT
441 
442 
443 #ifdef HAVE_TEUCHOS___INT64
444 
445 template<>
446 struct ScalarTraits<__int64>
447 {
448  typedef __int64 magnitudeType;
449  typedef __int64 halfPrecision;
450  typedef __int64 doublePrecision;
451  static const bool isComplex = false;
452  static const bool isOrdinal = true;
453  static const bool isComparable = true;
454  static const bool hasMachineParameters = false;
455  // Not defined: eps(), sfmin(), base(), prec(), t(), rnd(), emin(), rmin(), emax(), rmax()
456  static inline magnitudeType magnitude(__int64 a) { return static_cast<__int64>(std::fabs(static_cast<double>(a))); }
457  static inline __int64 zero() { return 0; }
458  static inline __int64 one() { return 1; }
459  static inline __int64 conjugate(__int64 x) { return x; }
460  static inline __int64 real(__int64 x) { return x; }
461  static inline __int64 imag(__int64) { return 0; }
462  static inline void seedrandom(unsigned int s) {
463  std::srand(s);
464 #ifdef __APPLE__
465  // throw away first random number to address bug 3655
466  // http://software.sandia.gov/bugzilla/show_bug.cgi?id=3655
467  random();
468 #endif
469  }
470  //static inline int random() { return (-1 + 2*rand()); } // RAB: This version should be used to be consistent with others
471  static inline __int64 random() { return std::rand(); } // RAB: This version should be used for an unsigned int, not int
472  static inline std::string name() { return "__int64"; }
473  static inline __int64 squareroot(__int64 x) { return (__int64) std::sqrt((double) x); }
474  static inline __int64 pow(__int64 x, __int64 y) { return (__int64) std::pow((double)x,(double)y); }
475  // Note: Depending on the number of bits in __int64, the cast
476  // from __int64 to double may not be exact.
477  static inline __int64 log(__int64 x) { return static_cast<__int64> (std::log (static_cast<double> (x))); }
478  static inline __int64 log10(__int64 x) { return static_cast<__int64> (std::log10 (static_cast<double> (x))); }
479 };
480 
481 template<>
482 struct ScalarTraits<unsigned __int64>
483 {
484  typedef unsigned __int64 magnitudeType;
485  typedef unsigned __int64 halfPrecision;
486  typedef unsigned __int64 doublePrecision;
487  static const bool isComplex = false;
488  static const bool isOrdinal = true;
489  static const bool isComparable = true;
490  static const bool hasMachineParameters = false;
491  // Not defined: eps(), sfmin(), base(), prec(), t(), rnd(), emin(), rmin(), emax(), rmax()
492  static inline magnitudeType magnitude(unsigned __int64 a) { return static_cast<unsigned __int64>(std::fabs(static_cast<double>(a))); }
493  static inline unsigned __int64 zero() { return 0; }
494  static inline unsigned __int64 one() { return 1; }
495  static inline unsigned __int64 conjugate(unsigned __int64 x) { return x; }
496  static inline unsigned __int64 real(unsigned __int64 x) { return x; }
497  static inline unsigned __int64 imag(unsigned __int64) { return 0; }
498  static inline void seedrandom(unsigned int s) {
499  std::srand(s);
500 #ifdef __APPLE__
501  // throw away first random number to address bug 3655
502  // http://software.sandia.gov/bugzilla/show_bug.cgi?id=3655
503  random();
504 #endif
505  }
506  //static inline int random() { return (-1 + 2*rand()); } // RAB: This version should be used to be consistent with others
507  static inline unsigned __int64 random() { return std::rand(); } // RAB: This version should be used for an unsigned int, not int
508  static inline std::string name() { return "unsigned __int64"; }
509  static inline unsigned __int64 squareroot(unsigned __int64 x) { return (unsigned __int64) std::sqrt((double) x); }
510  static inline unsigned __int64 pow(unsigned __int64 x, unsigned __int64 y) { return (unsigned __int64) std::pow((double)x,(double)y); }
511  // Note: Depending on the number of bits in unsigned __int64,
512  // the cast from unsigned __int64 to double may not be exact.
513  static inline unsigned __int64 log(unsigned __int64 x) { return static_cast<unsigned __int64> (std::log (static_cast<double> (x))); }
514  static inline unsigned __int64 log10(unsigned __int64 x) { return static_cast<unsigned __int64> (std::log10 (static_cast<double> (x))); }
515 };
516 
517 #endif // HAVE_TEUCHOS___INT64
518 
519 
520 #ifndef __sun
521 extern TEUCHOSCORE_LIB_DLL_EXPORT const float flt_nan;
522 #endif
523 
524 
525 template<>
526 struct ScalarTraits<float>
527 {
528  typedef float magnitudeType;
529  typedef float halfPrecision; // should become IEEE754-2008 binary16 or fp16 later, perhaps specified at configure according to architectural support
530  typedef double doublePrecision;
531  static const bool isComplex = false;
532  static const bool isOrdinal = false;
533  static const bool isComparable = true;
534  static const bool hasMachineParameters = true;
535  static inline float eps() {
536  return std::numeric_limits<float>::epsilon();
537  }
538  static inline float sfmin() {
539  return std::numeric_limits<float>::min();
540  }
541  static inline float base() {
542  return static_cast<float>(std::numeric_limits<float>::radix);
543  }
544  static inline float prec() {
545  return eps()*base();
546  }
547  static inline float t() {
548  return static_cast<float>(std::numeric_limits<float>::digits);
549  }
550  static inline float rnd() {
551  return ( std::numeric_limits<float>::round_style == std::round_to_nearest ? one() : zero() );
552  }
553  static inline float emin() {
554  return static_cast<float>(std::numeric_limits<float>::min_exponent);
555  }
556  static inline float rmin() {
557  return std::numeric_limits<float>::min();
558  }
559  static inline float emax() {
560  return static_cast<float>(std::numeric_limits<float>::max_exponent);
561  }
562  static inline float rmax() {
563  return std::numeric_limits<float>::max();
564  }
565  static inline magnitudeType magnitude(float a)
566  {
567 #ifdef TEUCHOS_DEBUG
568  TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
569  a, "Error, the input value to magnitude(...) a = " << a << " can not be NaN!" );
570 #endif
571  return std::fabs(a);
572  }
573  static inline float zero() { return(0.0f); }
574  static inline float one() { return(1.0f); }
575  static inline float conjugate(float x) { return(x); }
576  static inline float real(float x) { return x; }
577  static inline float imag(float) { return zero(); }
578  static inline float nan() {
579 #ifdef __sun
580  return 0.0f/std::sin(0.0f);
581 #else
582  return flt_nan;
583 #endif
584  }
585  static inline bool isnaninf(float x) {
586  return generic_real_isnaninf<float>(x);
587  }
588  static inline void seedrandom(unsigned int s) {
589  std::srand(s);
590 #ifdef __APPLE__
591  // throw away first random number to address bug 3655
592  // http://software.sandia.gov/bugzilla/show_bug.cgi?id=3655
593  random();
594 #endif
595  }
596  static inline float random() { float rnd = (float) std::rand() / RAND_MAX; return (-1.0f + 2.0f * rnd); }
597  static inline std::string name() { return "float"; }
598  static inline float squareroot(float x)
599  {
600 #ifdef TEUCHOS_DEBUG
601  TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
602  x, "Error, the input value to squareroot(...) x = " << x << " can not be NaN!" );
603 #endif
604  errno = 0;
605  const float rtn = std::sqrt(x);
606  if (errno)
607  return nan();
608  return rtn;
609  }
610  static inline float pow(float x, float y) { return std::pow(x,y); }
611  static inline float log(float x) { return std::log(x); }
612  static inline float log10(float x) { return std::log10(x); }
613 };
614 
615 
616 #ifndef __sun
617 extern TEUCHOSCORE_LIB_DLL_EXPORT const double dbl_nan;
618 #endif
619 
620 
621 template<>
622 struct ScalarTraits<double>
623 {
624  typedef double magnitudeType;
625  typedef float halfPrecision;
626  /* there are different options as to how to double "double"
627  - QD's DD (if available)
628  - ARPREC
629  - GNU MP
630  - a true hardware quad
631 
632  in the shortterm, this should be specified at configure time. I have inserted a configure-time option (--enable-teuchos-double-to-dd)
633  which uses QD's DD when available. This must be used alongside --enable-teuchos-qd.
634  */
635 #if defined(HAVE_TEUCHOS_DOUBLE_TO_QD)
636  typedef dd_real doublePrecision;
637 #elif defined(HAVE_TEUCHOS_DOUBLE_TO_ARPREC)
638  typedef mp_real doublePrecision;
639 #else
640  typedef double doublePrecision; // don't double "double" in this case
641 #endif
642  static const bool isComplex = false;
643  static const bool isOrdinal = false;
644  static const bool isComparable = true;
645  static const bool hasMachineParameters = true;
646  static inline double eps() {
647  return std::numeric_limits<double>::epsilon();
648  }
649  static inline double sfmin() {
650  return std::numeric_limits<double>::min();
651  }
652  static inline double base() {
653  return std::numeric_limits<double>::radix;
654  }
655  static inline double prec() {
656  return eps()*base();
657  }
658  static inline double t() {
659  return std::numeric_limits<double>::digits;
660  }
661  static inline double rnd() {
662  return ( std::numeric_limits<double>::round_style == std::round_to_nearest ? double(1.0) : double(0.0) );
663  }
664  static inline double emin() {
665  return std::numeric_limits<double>::min_exponent;
666  }
667  static inline double rmin() {
668  return std::numeric_limits<double>::min();
669  }
670  static inline double emax() {
671  return std::numeric_limits<double>::max_exponent;
672  }
673  static inline double rmax() {
674  return std::numeric_limits<double>::max();
675  }
676  static inline magnitudeType magnitude(double a)
677  {
678 #ifdef TEUCHOS_DEBUG
679  TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
680  a, "Error, the input value to magnitude(...) a = " << a << " can not be NaN!" );
681 #endif
682  return std::fabs(a);
683  }
684  static inline double zero() { return 0.0; }
685  static inline double one() { return 1.0; }
686  static inline double conjugate(double x) { return(x); }
687  static inline double real(double x) { return(x); }
688  static inline double imag(double) { return(0); }
689  static inline double nan() {
690 #ifdef __sun
691  return 0.0/std::sin(0.0);
692 #else
693  return dbl_nan;
694 #endif
695  }
696  static inline bool isnaninf(double x) {
697  return generic_real_isnaninf<double>(x);
698  }
699  static inline void seedrandom(unsigned int s) {
700  std::srand(s);
701 #ifdef __APPLE__
702  // throw away first random number to address bug 3655
703  // http://software.sandia.gov/bugzilla/show_bug.cgi?id=3655
704  random();
705 #endif
706  }
707  static inline double random() { double rnd = (double) std::rand() / RAND_MAX; return (double)(-1.0 + 2.0 * rnd); }
708  static inline std::string name() { return "double"; }
709  static inline double squareroot(double x)
710  {
711 #ifdef TEUCHOS_DEBUG
712  TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
713  x, "Error, the input value to squareroot(...) x = " << x << " can not be NaN!" );
714 #endif
715  errno = 0;
716  const double rtn = std::sqrt(x);
717  if (errno)
718  return nan();
719  return rtn;
720  }
721  static inline double pow(double x, double y) { return std::pow(x,y); }
722  static inline double log(double x) { return std::log(x); }
723  static inline double log10(double x) { return std::log10(x); }
724 };
725 
726 
727 #ifdef HAVE_TEUCHOS_QD
728 
729 
730 bool operator&&(const dd_real &a, const dd_real &b);
731 bool operator&&(const qd_real &a, const qd_real &b);
732 
733 
734 template<>
735 struct ScalarTraits<dd_real>
736 {
737  typedef dd_real magnitudeType;
738  typedef double halfPrecision;
739  typedef qd_real doublePrecision;
740  static const bool isComplex = false;
741  static const bool isOrdinal = false;
742  static const bool isComparable = true;
743  static const bool hasMachineParameters = true;
744  static inline dd_real eps() { return std::numeric_limits<dd_real>::epsilon(); }
745  static inline dd_real sfmin() { return std::numeric_limits<dd_real>::min(); }
746  static inline dd_real base() { return std::numeric_limits<dd_real>::radix; }
747  static inline dd_real prec() { return eps()*base(); }
748  static inline dd_real t() { return std::numeric_limits<dd_real>::digits; }
749  static inline dd_real rnd() { return ( std::numeric_limits<dd_real>::round_style == std::round_to_nearest ? dd_real(1.0) : dd_real(0.0) ); }
750  static inline dd_real emin() { return std::numeric_limits<dd_real>::min_exponent; }
751  static inline dd_real rmin() { return std::numeric_limits<dd_real>::min(); }
752  static inline dd_real emax() { return std::numeric_limits<dd_real>::max_exponent; }
753  static inline dd_real rmax() { return std::numeric_limits<dd_real>::max(); }
754  static inline magnitudeType magnitude(dd_real a)
755  {
756 #ifdef TEUCHOS_DEBUG
757  TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
758  a, "Error, the input value to magnitude(...) a = " << a << " can not be NaN!" );
759 #endif
760  return ::abs(a);
761  }
762  static inline dd_real zero() { return dd_real(0.0); }
763  static inline dd_real one() { return dd_real(1.0); }
764  static inline dd_real conjugate(dd_real x) { return(x); }
765  static inline dd_real real(dd_real x) { return x ; }
766  static inline dd_real imag(dd_real) { return zero(); }
767  static inline dd_real nan() { return dd_real::_nan; }
768  static inline bool isnaninf(dd_real x) { return isnan(x) || isinf(x); }
769  static inline void seedrandom(unsigned int s) {
770  // ddrand() uses std::rand(), so the std::srand() is our seed
771  std::srand(s);
772 #ifdef __APPLE__
773  // throw away first random number to address bug 3655
774  // http://software.sandia.gov/bugzilla/show_bug.cgi?id=3655
775  random();
776 #endif
777  }
778  static inline dd_real random() { return ddrand(); }
779  static inline std::string name() { return "dd_real"; }
780  static inline dd_real squareroot(dd_real x)
781  {
782 #ifdef TEUCHOS_DEBUG
783  TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
784  x, "Error, the input value to squareroot(...) x = " << x << " can not be NaN!" );
785 #endif
786  return ::sqrt(x);
787  }
788  static inline dd_real pow(dd_real x, dd_real y) { return ::pow(x,y); }
789  // dd_real puts its transcendental functions in the global namespace.
790  static inline dd_real log(dd_real x) { return ::log(x); }
791  static inline dd_real log10(dd_real x) { return ::log10(x); }
792 };
793 
794 
795 template<>
796 struct ScalarTraits<qd_real>
797 {
798  typedef qd_real magnitudeType;
799  typedef dd_real halfPrecision;
800  typedef qd_real doublePrecision;
801  static const bool isComplex = false;
802  static const bool isOrdinal = false;
803  static const bool isComparable = true;
804  static const bool hasMachineParameters = true;
805  static inline qd_real eps() { return std::numeric_limits<qd_real>::epsilon(); }
806  static inline qd_real sfmin() { return std::numeric_limits<qd_real>::min(); }
807  static inline qd_real base() { return std::numeric_limits<qd_real>::radix; }
808  static inline qd_real prec() { return eps()*base(); }
809  static inline qd_real t() { return std::numeric_limits<qd_real>::digits; }
810  static inline qd_real rnd() { return ( std::numeric_limits<qd_real>::round_style == std::round_to_nearest ? qd_real(1.0) : qd_real(0.0) ); }
811  static inline qd_real emin() { return std::numeric_limits<qd_real>::min_exponent; }
812  static inline qd_real rmin() { return std::numeric_limits<qd_real>::min(); }
813  static inline qd_real emax() { return std::numeric_limits<qd_real>::max_exponent; }
814  static inline qd_real rmax() { return std::numeric_limits<qd_real>::max(); }
815  static inline magnitudeType magnitude(qd_real a)
816  {
817 #ifdef TEUCHOS_DEBUG
818  TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
819  a, "Error, the input value to magnitude(...) a = " << a << " can not be NaN!" );
820 #endif
821  return ::abs(a);
822  }
823  static inline qd_real zero() { return qd_real(0.0); }
824  static inline qd_real one() { return qd_real(1.0); }
825  static inline qd_real conjugate(qd_real x) { return(x); }
826  static inline qd_real real(qd_real x) { return x ; }
827  static inline qd_real imag(qd_real) { return zero(); }
828  static inline qd_real nan() { return qd_real::_nan; }
829  static inline bool isnaninf(qd_real x) { return isnan(x) || isinf(x); }
830  static inline void seedrandom(unsigned int s) {
831  // qdrand() uses std::rand(), so the std::srand() is our seed
832  std::srand(s);
833 #ifdef __APPLE__
834  // throw away first random number to address bug 3655
835  // http://software.sandia.gov/bugzilla/show_bug.cgi?id=3655
836  random();
837 #endif
838  }
839  static inline qd_real random() { return qdrand(); }
840  static inline std::string name() { return "qd_real"; }
841  static inline qd_real squareroot(qd_real x)
842  {
843 #ifdef TEUCHOS_DEBUG
844  TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
845  x, "Error, the input value to squareroot(...) x = " << x << " can not be NaN!" );
846 #endif
847  return ::sqrt(x);
848  }
849  static inline qd_real pow(qd_real x, qd_real y) { return ::pow(x,y); }
850  // qd_real puts its transcendental functions in the global namespace.
851  static inline qd_real log(qd_real x) { return ::log(x); }
852  static inline qd_real log10(qd_real x) { return ::log10(x); }
853 };
854 
855 
856 #endif // HAVE_TEUCHOS_QD
857 
858 
859 #ifdef HAVE_TEUCHOS_GNU_MP
860 
861 
862 extern gmp_randclass gmp_rng;
863 
864 
865 /* Regarding halfPrecision, doublePrecision and mpf_class:
866  Because the precision of an mpf_class float is not determined by the data type,
867  there is no way to fill the typedefs for this object.
868 
869  Instead, we could create new data classes (e.g., Teuchos::MPF128, Teuchos::MPF256) for
870  commonly used levels of precision, and fill out ScalarTraits for these. This would allow us
871  to typedef the promotions and demotions in the appropriate way. These classes would serve to
872  wrap an mpf_class object, calling the constructor for the appropriate precision, exposing the
873  arithmetic routines but hiding the precision-altering routines.
874 
875  Alternatively (perhaps, preferably), would could create a single class templated on the precision (e.g., Teuchos::MPF<N>).
876  Then we have a single (partially-specialized) implementation of ScalarTraits. This class, as above, must expose all of the
877  operations expected of a scalar type; however, most of these can be trivially stolen from the gmpcxx.h header file
878 
879  CGB/RAB, 01/05/2009
880 */
881 template<>
882 struct ScalarTraits<mpf_class>
883 {
884  typedef mpf_class magnitudeType;
885  typedef mpf_class halfPrecision;
886  typedef mpf_class doublePrecision;
887  static const bool isComplex = false;
888  static const bool hasMachineParameters = false;
889  // Not defined: eps(), sfmin(), base(), prec(), t(), rnd(), emin(), rmin(), emax(), rmax()
890  static magnitudeType magnitude(mpf_class a) { return std::abs(a); }
891  static inline mpf_class zero() { mpf_class zero = 0.0; return zero; }
892  static inline mpf_class one() { mpf_class one = 1.0; return one; }
893  static inline mpf_class conjugate(mpf_class x) { return x; }
894  static inline mpf_class real(mpf_class x) { return(x); }
895  static inline mpf_class imag(mpf_class x) { return(0); }
896  static inline bool isnaninf(mpf_class x) { return false; } // mpf_class currently can't handle nan or inf!
897  static inline void seedrandom(unsigned int s) {
898  unsigned long int seedVal = static_cast<unsigned long int>(s);
899  gmp_rng.seed( seedVal );
900  }
901  static inline mpf_class random() {
902  return gmp_rng.get_f();
903  }
904  static inline std::string name() { return "mpf_class"; }
905  static inline mpf_class squareroot(mpf_class x) { return std::sqrt(x); }
906  static inline mpf_class pow(mpf_class x, mpf_class y) { return pow(x,y); }
907  // Todo: RAB: 2004/05/28: Add nan() and isnaninf() functions when needed!
908 };
909 
910 #endif // HAVE_TEUCHOS_GNU_MP
911 
912 #ifdef HAVE_TEUCHOS_ARPREC
913 
914 /* See discussion above for mpf_class, regarding halfPrecision and doublePrecision. Something similar will need to be done
915  for ARPREC. */
916 template<>
917 struct ScalarTraits<mp_real>
918 {
919  typedef mp_real magnitudeType;
920  typedef double halfPrecision;
921  typedef mp_real doublePrecision;
922  static const bool isComplex = false;
923  static const bool isComparable = true;
924  static const bool isOrdinal = false;
925  static const bool hasMachineParameters = false;
926  // Not defined: eps(), sfmin(), base(), prec(), t(), rnd(), emin(), rmin(), emax(), rmax()
927  static magnitudeType magnitude(mp_real a) { return abs(a); }
928  static inline mp_real zero() { mp_real zero = 0.0; return zero; }
929  static inline mp_real one() { mp_real one = 1.0; return one; }
930  static inline mp_real conjugate(mp_real x) { return x; }
931  static inline mp_real real(mp_real x) { return(x); }
932  static inline mp_real imag(mp_real x) { return zero(); }
933  static inline bool isnaninf(mp_real x) { return false; } // ToDo: Change this?
934  static inline void seedrandom(unsigned int s) {
935  long int seedVal = static_cast<long int>(s);
936  srand48(seedVal);
937  }
938  static inline mp_real random() { return mp_rand(); }
939  static inline std::string name() { return "mp_real"; }
940  static inline mp_real squareroot(mp_real x) { return sqrt(x); }
941  static inline mp_real pow(mp_real x, mp_real y) { return pow(x,y); }
942  // Todo: RAB: 2004/05/28: Add nan() and isnaninf() functions when needed!
943 };
944 
945 
946 #endif // HAVE_TEUCHOS_ARPREC
947 
948 
949 #ifdef HAVE_TEUCHOS_COMPLEX
950 
951 
952 // Partial specialization for std::complex numbers templated on real type T
953 template<class T>
954 struct ScalarTraits<
955  std::complex<T>
956 >
957 {
958  typedef std::complex<T> ComplexT;
959  typedef std::complex<typename ScalarTraits<T>::halfPrecision> halfPrecision;
960  typedef std::complex<typename ScalarTraits<T>::doublePrecision> doublePrecision;
961  typedef typename ScalarTraits<T>::magnitudeType magnitudeType;
962  static const bool isComplex = true;
963  static const bool isOrdinal = ScalarTraits<T>::isOrdinal;
964  static const bool isComparable = false;
965  static const bool hasMachineParameters = true;
966  static inline magnitudeType eps() { return ScalarTraits<magnitudeType>::eps(); }
967  static inline magnitudeType sfmin() { return ScalarTraits<magnitudeType>::sfmin(); }
968  static inline magnitudeType base() { return ScalarTraits<magnitudeType>::base(); }
969  static inline magnitudeType prec() { return ScalarTraits<magnitudeType>::prec(); }
970  static inline magnitudeType t() { return ScalarTraits<magnitudeType>::t(); }
971  static inline magnitudeType rnd() { return ScalarTraits<magnitudeType>::rnd(); }
972  static inline magnitudeType emin() { return ScalarTraits<magnitudeType>::emin(); }
973  static inline magnitudeType rmin() { return ScalarTraits<magnitudeType>::rmin(); }
974  static inline magnitudeType emax() { return ScalarTraits<magnitudeType>::emax(); }
975  static inline magnitudeType rmax() { return ScalarTraits<magnitudeType>::rmax(); }
976  static magnitudeType magnitude(ComplexT a)
977  {
978 #ifdef TEUCHOS_DEBUG
979  TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
980  a, "Error, the input value to magnitude(...) a = " << a << " can not be NaN!" );
981 #endif
982  return std::abs(a);
983  }
984  static inline ComplexT zero() { return ComplexT(ScalarTraits<magnitudeType>::zero(),ScalarTraits<magnitudeType>::zero()); }
985  static inline ComplexT one() { return ComplexT(ScalarTraits<magnitudeType>::one(),ScalarTraits<magnitudeType>::zero()); }
986  static inline ComplexT conjugate(ComplexT a){ return ComplexT(a.real(),-a.imag()); }
987  static inline magnitudeType real(ComplexT a) { return a.real(); }
988  static inline magnitudeType imag(ComplexT a) { return a.imag(); }
989  static inline ComplexT nan() { return ComplexT(ScalarTraits<magnitudeType>::nan(),ScalarTraits<magnitudeType>::nan()); }
990  static inline bool isnaninf(ComplexT x) { return ScalarTraits<magnitudeType>::isnaninf(x.real()) || ScalarTraits<magnitudeType>::isnaninf(x.imag()); }
991  static inline void seedrandom(unsigned int s) { ScalarTraits<magnitudeType>::seedrandom(s); }
992  static inline ComplexT random()
993  {
994  const T rnd1 = ScalarTraits<magnitudeType>::random();
995  const T rnd2 = ScalarTraits<magnitudeType>::random();
996  return ComplexT(rnd1,rnd2);
997  }
998  static inline std::string name() { return std::string("std::complex<")+std::string(ScalarTraits<magnitudeType>::name())+std::string(">"); }
999  // This will only return one of the square roots of x, the other can be obtained by taking its conjugate
1000  static inline ComplexT squareroot(ComplexT x)
1001  {
1002 #ifdef TEUCHOS_DEBUG
1003  TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
1004  x, "Error, the input value to squareroot(...) x = " << x << " can not be NaN!" );
1005 #endif
1006  typedef ScalarTraits<magnitudeType> STMT;
1007  const T r = x.real(), i = x.imag(), zero = STMT::zero(), two = 2.0;
1008  const T a = STMT::squareroot((r*r)+(i*i));
1009  const T nr = STMT::squareroot((a+r)/two);
1010  const T ni = ( i == zero ? zero : STMT::squareroot((a-r)/two) );
1011  return ComplexT(nr,ni);
1012  }
1013  // 2010/03/19: rabartl: Above, I had to add the check for i == zero
1014  // to avoid a returned NaN on the Intel 10.1 compiler. For some
1015  // reason, having these two squareroot calls in a row produce a NaN
1016  // in an optimized build with this compiler. Amazingly, when I put
1017  // in print statements (i.e. std::cout << ...) the NaN went away and
1018  // the second squareroot((a-r)/two) returned zero correctly. I
1019  // guess that calling the output routine flushed the registers or
1020  // something and restarted the squareroot rountine for this compiler
1021  // or something similar. Actually, due to roundoff, it is possible that a-r
1022  // might be slightly less than zero (i.e. -1e-16) and that would cause
1023  // a possbile NaN return. THe above if test is the right thing to do
1024  // I think and is very cheap.
1025  static inline ComplexT pow(ComplexT x, ComplexT y) { return pow(x,y); }
1026 };
1027 
1028 
1029 #endif // HAVE_TEUCHOS_COMPLEX
1030 
1031 
1032 #endif // DOXYGEN_SHOULD_SKIP_THIS
1033 
1034 
1035 } // Teuchos namespace
1036 
1037 
1038 #endif // _TEUCHOS_SCALARTRAITS_HPP_
T magnitudeType
Mandatory typedef for result of magnitude.
static magnitudeType eps()
Returns relative machine precision.
static T squareroot(T x)
Returns a number of magnitudeType that is the square root of this scalar type x.
static const bool isComparable
Determines if scalar type supports relational operators such as &lt;, &gt;, &lt;=, &gt;=.
static magnitudeType real(T a)
Returns the real part of the scalar type a.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
Declaration and default implementation for basic traits for the scalar field type.
static T pow(T x, T y)
Returns the result of raising one scalar x to the power y.
static magnitudeType emax()
Returns the largest exponent before overflow.
static magnitudeType base()
Returns the base of the machine.
static const bool hasMachineParameters
Determines if scalar type have machine-specific parameters (i.e. eps(), sfmin(), base(), prec(), t(), rnd(), emin(), rmin(), emax(), rmax() are supported).
static std::string name()
Returns the name of this scalar type.
static magnitudeType rmax()
Overflow theshold - (base^emax)*(1-eps)
static magnitudeType sfmin()
Returns safe minimum (sfmin), such that 1/sfmin does not overflow.
static T conjugate(T a)
Returns the conjugate of the scalar type a.
static const bool isOrdinal
Determines if scalar type is an ordinal type.
static magnitudeType prec()
Returns eps*base.
static magnitudeType t()
Returns the number of (base) digits in the mantissa.
static magnitudeType rmin()
Returns the underflow threshold - base^(emin-1)
T doublePrecision
Typedef for double precision.
static void seedrandom(unsigned int s)
Seed the random number generator returned by random().
static magnitudeType imag(T a)
Returns the imaginary part of the scalar type a.
static bool isnaninf(const T &x)
Returns true if x is NaN or Inf.
static magnitudeType magnitude(T a)
Returns the magnitudeType of the scalar type a.
static T nan()
Returns a number that represents NaN.
static T zero()
Returns representation of zero for this scalar type.
static T random()
Returns a random number (between -one() and +one()) of this scalar type.
T halfPrecision
Typedef for half precision.
static const bool isComplex
Determines if scalar type is std::complex.
static magnitudeType emin()
Returns the minimum exponent before (gradual) underflow.
static magnitudeType rnd()
Returns 1.0 when rounding occurs in addition, 0.0 otherwise.
static T one()
Returns representation of one for this scalar type.