49 #ifndef _TEUCHOS_SCALARTRAITS_HPP_ 50 #define _TEUCHOS_SCALARTRAITS_HPP_ 58 #ifdef HAVE_TEUCHOS_ARPREC 59 #include <arprec/mp_real.h> 62 #ifdef HAVE_TEUCHOS_QD 63 #include <qd/qd_real.h> 64 #include <qd/dd_real.h> 67 #ifdef HAVE_TEUCHOS_GNU_MP 79 #ifndef DOXYGEN_SHOULD_SKIP_THIS 83 void throwScalarTraitsNanInfError(
const std::string &errMsg );
86 template<
class Scalar>
87 bool generic_real_isnaninf(
const Scalar &x)
89 typedef std::numeric_limits<Scalar> NL;
91 const Scalar tol = 1.0;
92 if (!(x <= tol) && !(x > tol))
return true;
94 Scalar z =
static_cast<Scalar
>(0.0) * x;
95 if (!(z <= tol) && !(z > tol))
return true;
97 if (x == NL::infinity() || x == -NL::infinity())
return true;
103 #define TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR( VALUE, MSG ) \ 104 if (isnaninf(VALUE)) { \ 105 std::ostringstream omsg; \ 107 Teuchos::throwScalarTraitsNanInfError(omsg.str()); \ 112 struct ScalarTraits<char>
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) {
138 static inline char random() {
return std::rand(); }
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))); }
148 struct ScalarTraits<short int>
153 static const bool isComplex =
false;
154 static const bool isOrdinal =
true;
155 static const bool isComparable =
true;
156 static const bool hasMachineParameters =
false;
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) {
174 static inline short int random() {
return std::rand(); }
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))); }
183 struct ScalarTraits<unsigned short int>
188 static const bool isComplex =
false;
189 static const bool isOrdinal =
true;
190 static const bool isComparable =
true;
191 static const bool hasMachineParameters =
false;
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) {
209 static inline unsigned short int random() {
return std::rand(); }
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))); }
219 struct ScalarTraits<int>
224 static const bool isComplex =
false;
225 static const bool isOrdinal =
true;
226 static const bool isComparable =
true;
227 static const bool hasMachineParameters =
false;
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) {
245 static inline int random() {
return std::rand(); }
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))); }
255 struct ScalarTraits<unsigned int>
260 static const bool isComplex =
false;
261 static const bool isOrdinal =
true;
262 static const bool isComparable =
true;
263 static const bool hasMachineParameters =
false;
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) {
281 static inline unsigned int random() {
return std::rand(); }
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))); }
291 struct ScalarTraits<long int>
296 static const bool isComplex =
false;
297 static const bool isOrdinal =
true;
298 static const bool isComparable =
true;
299 static const bool hasMachineParameters =
false;
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) {
317 static inline long int random() {
return std::rand(); }
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); }
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))); }
329 struct ScalarTraits<long unsigned int>
334 static const bool isComplex =
false;
335 static const bool isOrdinal =
true;
336 static const bool isComparable =
true;
337 static const bool hasMachineParameters =
false;
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) {
355 static inline long unsigned int random() {
return std::rand(); }
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); }
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))); }
366 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 368 struct ScalarTraits<long long int>
373 static const bool isComplex =
false;
374 static const bool isOrdinal =
true;
375 static const bool isComparable =
true;
376 static const bool hasMachineParameters =
false;
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) {
394 static inline long long int random() {
return std::rand(); }
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); }
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))); }
405 struct ScalarTraits<unsigned long long int>
410 static const bool isComplex =
false;
411 static const bool isOrdinal =
true;
412 static const bool isComparable =
true;
413 static const bool hasMachineParameters =
false;
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) {
431 static inline unsigned long long int random() {
return std::rand(); }
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); }
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))); }
440 #endif // HAVE_TEUCHOS_LONG_LONG_INT 443 #ifdef HAVE_TEUCHOS___INT64 446 struct ScalarTraits<__int64>
451 static const bool isComplex =
false;
452 static const bool isOrdinal =
true;
453 static const bool isComparable =
true;
454 static const bool hasMachineParameters =
false;
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) {
471 static inline __int64
random() {
return std::rand(); }
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); }
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))); }
482 struct ScalarTraits<unsigned __int64>
487 static const bool isComplex =
false;
488 static const bool isOrdinal =
true;
489 static const bool isComparable =
true;
490 static const bool hasMachineParameters =
false;
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) {
507 static inline unsigned __int64
random() {
return std::rand(); }
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); }
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))); }
517 #endif // HAVE_TEUCHOS___INT64 526 struct ScalarTraits<float>
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();
538 static inline float sfmin() {
539 return std::numeric_limits<float>::min();
541 static inline float base() {
542 return static_cast<float>(std::numeric_limits<float>::radix);
544 static inline float prec() {
547 static inline float t() {
548 return static_cast<float>(std::numeric_limits<float>::digits);
550 static inline float rnd() {
551 return ( std::numeric_limits<float>::round_style == std::round_to_nearest ?
one() :
zero() );
553 static inline float emin() {
554 return static_cast<float>(std::numeric_limits<float>::min_exponent);
556 static inline float rmin() {
557 return std::numeric_limits<float>::min();
559 static inline float emax() {
560 return static_cast<float>(std::numeric_limits<float>::max_exponent);
562 static inline float rmax() {
563 return std::numeric_limits<float>::max();
565 static inline magnitudeType
magnitude(
float a)
568 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
569 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
573 static inline float zero() {
return(0.0
f); }
574 static inline float one() {
return(1.0
f); }
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() {
580 return 0.0f/std::sin(0.0
f);
585 static inline bool isnaninf(
float x) {
586 return generic_real_isnaninf<float>(x);
588 static inline void seedrandom(
unsigned int s) {
596 static inline float random() {
float rnd = (float) std::rand() / RAND_MAX;
return (-1.0
f + 2.0
f * rnd); }
597 static inline std::string
name() {
return "float"; }
601 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
602 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
605 const float rtn = std::sqrt(x);
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); }
622 struct ScalarTraits<double>
635 #if defined(HAVE_TEUCHOS_DOUBLE_TO_QD) 637 #elif defined(HAVE_TEUCHOS_DOUBLE_TO_ARPREC) 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();
649 static inline double sfmin() {
650 return std::numeric_limits<double>::min();
652 static inline double base() {
653 return std::numeric_limits<double>::radix;
655 static inline double prec() {
658 static inline double t() {
659 return std::numeric_limits<double>::digits;
661 static inline double rnd() {
662 return ( std::numeric_limits<double>::round_style == std::round_to_nearest ?
double(1.0) :
double(0.0) );
664 static inline double emin() {
665 return std::numeric_limits<double>::min_exponent;
667 static inline double rmin() {
668 return std::numeric_limits<double>::min();
670 static inline double emax() {
671 return std::numeric_limits<double>::max_exponent;
673 static inline double rmax() {
674 return std::numeric_limits<double>::max();
676 static inline magnitudeType
magnitude(
double a)
679 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
680 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
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() {
691 return 0.0/std::sin(0.0);
696 static inline bool isnaninf(
double x) {
697 return generic_real_isnaninf<double>(x);
699 static inline void seedrandom(
unsigned int s) {
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"; }
712 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
713 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
716 const double rtn = std::sqrt(x);
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); }
727 #ifdef HAVE_TEUCHOS_QD 730 bool operator&&(
const dd_real &a,
const dd_real &b);
731 bool operator&&(
const qd_real &a,
const qd_real &b);
735 struct ScalarTraits<dd_real>
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)
757 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
758 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
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) {
778 static inline dd_real
random() {
return ddrand(); }
779 static inline std::string
name() {
return "dd_real"; }
783 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
784 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
788 static inline dd_real
pow(dd_real x, dd_real y) { return ::pow(x,y); }
790 static inline dd_real log(dd_real x) { return ::log(x); }
791 static inline dd_real log10(dd_real x) { return ::log10(x); }
796 struct ScalarTraits<qd_real>
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)
818 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
819 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
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) {
839 static inline qd_real
random() {
return qdrand(); }
840 static inline std::string
name() {
return "qd_real"; }
844 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
845 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
849 static inline qd_real
pow(qd_real x, qd_real y) { return ::pow(x,y); }
851 static inline qd_real log(qd_real x) { return ::log(x); }
852 static inline qd_real log10(qd_real x) { return ::log10(x); }
856 #endif // HAVE_TEUCHOS_QD 859 #ifdef HAVE_TEUCHOS_GNU_MP 862 extern gmp_randclass gmp_rng;
882 struct ScalarTraits<mpf_class>
887 static const bool isComplex =
false;
888 static const bool hasMachineParameters =
false;
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; }
897 static inline void seedrandom(
unsigned int s) {
898 unsigned long int seedVal =
static_cast<unsigned long int>(s);
899 gmp_rng.seed( seedVal );
901 static inline mpf_class
random() {
902 return gmp_rng.get_f();
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); }
910 #endif // HAVE_TEUCHOS_GNU_MP 912 #ifdef HAVE_TEUCHOS_ARPREC 917 struct ScalarTraits<mp_real>
922 static const bool isComplex =
false;
923 static const bool isComparable =
true;
924 static const bool isOrdinal =
false;
925 static const bool hasMachineParameters =
false;
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; }
934 static inline void seedrandom(
unsigned int s) {
935 long int seedVal =
static_cast<long int>(s);
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); }
946 #endif // HAVE_TEUCHOS_ARPREC 949 #ifdef HAVE_TEUCHOS_COMPLEX 958 typedef std::complex<T> ComplexT;
959 typedef std::complex<typename ScalarTraits<T>::halfPrecision>
halfPrecision;
960 typedef std::complex<typename ScalarTraits<T>::doublePrecision>
doublePrecision;
962 static const bool isComplex =
true;
964 static const bool isComparable =
false;
965 static const bool hasMachineParameters =
true;
976 static magnitudeType
magnitude(ComplexT a)
979 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
980 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
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(); }
992 static inline ComplexT
random()
996 return ComplexT(rnd1,rnd2);
1000 static inline ComplexT
squareroot(ComplexT x)
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!" );
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);
1025 static inline ComplexT
pow(ComplexT x, ComplexT y) {
return pow(x,y); }
1029 #endif // HAVE_TEUCHOS_COMPLEX 1032 #endif // DOXYGEN_SHOULD_SKIP_THIS 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 <, >, <=, >=.
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.
#define TEUCHOSCORE_LIB_DLL_EXPORT
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.