49 #ifndef _TEUCHOS_SCALARTRAITS_HPP_
50 #define _TEUCHOS_SCALARTRAITS_HPP_
58 #ifdef HAVE_TEUCHOS_ARPREC
59 #include <arprec/mp_real.h>
62 #ifdef HAVE_TEUCHOSCORE_QUADMATH
80 operator<< (std::ostream& out,
const __float128& x);
92 operator>> (std::istream& in, __float128& x);
96 #endif // HAVE_TEUCHOSCORE_QUADMATH
98 #ifdef HAVE_TEUCHOS_QD
99 #include <qd/qd_real.h>
100 #include <qd/dd_real.h>
103 #ifdef HAVE_TEUCHOS_GNU_MP
115 #ifndef DOXYGEN_SHOULD_SKIP_THIS
118 TEUCHOSCORE_LIB_DLL_EXPORT
119 void throwScalarTraitsNanInfError(
const std::string &errMsg );
122 template<
class Scalar>
123 bool generic_real_isnaninf(
const Scalar &x)
125 #ifdef HAVE_TEUCHOSCORE_CXX11
126 if (std::isnan(x))
return true;
127 if (std::isinf(x))
return true;
130 typedef std::numeric_limits<Scalar> STD_NL;
132 const Scalar tol = 1.0;
133 if (!(x <= tol) && !(x > tol))
return true;
135 Scalar z =
static_cast<Scalar
>(0.0) * x;
136 if (!(z <= tol) && !(z > tol))
return true;
138 if (x == STD_NL::infinity() || x == -STD_NL::infinity())
return true;
145 #define TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR( VALUE, MSG ) \
146 if (isnaninf(VALUE)) { \
147 std::ostringstream omsg; \
149 Teuchos::throwScalarTraitsNanInfError(omsg.str()); \
154 struct ScalarTraits<char>
165 static inline magnitudeType magnitude(
char a) {
return static_cast<char>(std::fabs(static_cast<double>(a))); }
166 static inline char zero() {
return 0; }
167 static inline char one() {
return 1; }
168 static inline char conjugate(
char x) {
return x; }
169 static inline char real(
char x) {
return x; }
170 static inline char imag(
char) {
return 0; }
171 static inline bool isnaninf(
char ) {
return false; }
172 static inline void seedrandom(
unsigned int s) {
181 static inline char random() {
return std::rand(); }
182 static inline std::string
name() {
return "char"; }
183 static inline char squareroot(
char x) {
return (
char) std::sqrt((
double) x); }
184 static inline char pow(
char x,
char y) {
return (
char) std::pow((
double)x,(
double)y); }
185 static inline char log(
char x) {
return static_cast<char> (std::log (static_cast<double> (x))); }
186 static inline char log10(
char x) {
return static_cast<char> (std::log10 (static_cast<double> (x))); }
191 struct ScalarTraits<short int>
202 static inline magnitudeType magnitude(
short int a) {
return static_cast<short int>(std::fabs(static_cast<double>(a))); }
203 static inline short int zero() {
return 0; }
204 static inline short int one() {
return 1; }
205 static inline short int conjugate(
short int x) {
return x; }
206 static inline short int real(
short int x) {
return x; }
207 static inline short int imag(
short int) {
return 0; }
208 static inline bool isnaninf(
short int) {
return false; }
209 static inline void seedrandom(
unsigned int s) {
218 static inline short int random() {
return std::rand(); }
219 static inline std::string
name() {
return "short int"; }
220 static inline short int squareroot(
short int x) {
return (
short int) std::sqrt((
double) x); }
221 static inline short int pow(
short int x,
short int y) {
return (
short int) std::pow((
double)x,(
double)y); }
222 static inline short int log(
short int x) {
return static_cast<short int> (std::log (static_cast<double> (x))); }
223 static inline short int log10(
short int x) {
return static_cast<short int> (std::log10 (static_cast<double> (x))); }
227 struct ScalarTraits<unsigned short int>
238 static inline magnitudeType magnitude(
unsigned short int a) {
return static_cast<unsigned short int>(std::fabs(static_cast<double>(a))); }
239 static inline unsigned short int zero() {
return 0; }
240 static inline unsigned short int one() {
return 1; }
241 static inline unsigned short int conjugate(
unsigned short int x) {
return x; }
242 static inline unsigned short int real(
unsigned short int x) {
return x; }
243 static inline unsigned short int imag(
unsigned short int) {
return 0; }
244 static inline bool isnaninf(
unsigned short int) {
return false; }
245 static inline void seedrandom(
unsigned int s) {
254 static inline unsigned short int random() {
return std::rand(); }
255 static inline std::string
name() {
return "unsigned short int"; }
256 static inline unsigned short int squareroot(
unsigned short int x) {
return (
unsigned short int) std::sqrt((
double) x); }
257 static inline unsigned short int pow(
unsigned short int x,
unsigned short int y) {
return (
unsigned short int) std::pow((
double)x,(
double)y); }
258 static inline unsigned short int log(
unsigned short int x) {
return static_cast<unsigned short int> (std::log (static_cast<double> (x))); }
259 static inline unsigned short int log10(
unsigned short int x) {
return static_cast<unsigned short int> (std::log10 (static_cast<double> (x))); }
264 struct ScalarTraits<int>
275 static inline magnitudeType magnitude(
int a) {
return static_cast<int>(std::fabs(static_cast<double>(a))); }
276 static inline int zero() {
return 0; }
277 static inline int one() {
return 1; }
278 static inline int conjugate(
int x) {
return x; }
279 static inline int real(
int x) {
return x; }
280 static inline int imag(
int) {
return 0; }
281 static inline bool isnaninf(
int) {
return false; }
282 static inline void seedrandom(
unsigned int s) {
291 static inline int random() {
return std::rand(); }
292 static inline std::string
name() {
return "int"; }
293 static inline int squareroot(
int x) {
return (
int) std::sqrt((
double) x); }
294 static inline int pow(
int x,
int y) {
return (
int) std::pow((
double)x,(
double)y); }
295 static inline int log(
int x) {
return static_cast<int> (std::log (static_cast<double> (x))); }
296 static inline int log10(
int x) {
return static_cast<int> (std::log10 (static_cast<double> (x))); }
301 struct ScalarTraits<unsigned int>
312 static inline magnitudeType magnitude(
unsigned int a) {
return static_cast<unsigned int>(std::fabs(static_cast<double>(a))); }
313 static inline unsigned int zero() {
return 0; }
314 static inline unsigned int one() {
return 1; }
315 static inline unsigned int conjugate(
unsigned int x) {
return x; }
316 static inline unsigned int real(
unsigned int x) {
return x; }
317 static inline unsigned int imag(
unsigned int) {
return 0; }
318 static inline bool isnaninf(
unsigned int) {
return false; }
319 static inline void seedrandom(
unsigned int s) {
328 static inline unsigned int random() {
return std::rand(); }
329 static inline std::string
name() {
return "unsigned int"; }
330 static inline unsigned int squareroot(
unsigned int x) {
return (
unsigned int) std::sqrt((
double) x); }
331 static inline unsigned int pow(
unsigned int x,
unsigned int y) {
return (
unsigned int) std::pow((
double)x,(
double)y); }
332 static inline unsigned int log(
unsigned int x) {
return static_cast<unsigned int> (std::log (static_cast<double> (x))); }
333 static inline unsigned int log10(
unsigned int x) {
return static_cast<unsigned int> (std::log10 (static_cast<double> (x))); }
338 struct ScalarTraits<long int>
349 static inline magnitudeType magnitude(
long int a) {
return static_cast<long int>(std::fabs(static_cast<double>(a))); }
350 static inline long int zero() {
return 0; }
351 static inline long int one() {
return 1; }
352 static inline long int conjugate(
long int x) {
return x; }
353 static inline long int real(
long int x) {
return x; }
354 static inline long int imag(
long int) {
return 0; }
355 static inline bool isnaninf(
long int) {
return false; }
356 static inline void seedrandom(
unsigned int s) {
365 static inline long int random() {
return std::rand(); }
366 static inline std::string
name() {
return "long int"; }
367 static inline long int squareroot(
long int x) {
return (
long int) std::sqrt((
double) x); }
368 static inline long int pow(
long int x,
long int y) {
return (
long int) std::pow((
double)x,(
double)y); }
371 static inline long int log(
long int x) {
return static_cast<long int> (std::log (static_cast<double> (x))); }
372 static inline long int log10(
long int x) {
return static_cast<long int> (std::log10 (static_cast<double> (x))); }
377 struct ScalarTraits<long unsigned int>
388 static inline magnitudeType magnitude(
long unsigned int a) {
return static_cast<long unsigned int>(std::fabs(static_cast<double>(a))); }
389 static inline long unsigned int zero() {
return 0; }
390 static inline long unsigned int one() {
return 1; }
391 static inline long unsigned int conjugate(
long unsigned int x) {
return x; }
392 static inline long unsigned int real(
long unsigned int x) {
return x; }
393 static inline long unsigned int imag(
long unsigned int) {
return 0; }
394 static inline bool isnaninf(
long unsigned int) {
return false; }
395 static inline void seedrandom(
unsigned int s) {
404 static inline long unsigned int random() {
return std::rand(); }
405 static inline std::string
name() {
return "long unsigned int"; }
406 static inline long unsigned int squareroot(
long unsigned int x) {
return (
long unsigned int) std::sqrt((
double) x); }
407 static inline long unsigned int pow(
long unsigned int x,
long unsigned int y) {
return (
long unsigned int) std::pow((
double)x,(
double)y); }
410 static inline long unsigned int log(
long unsigned int x) {
return static_cast<long unsigned int> (std::log (static_cast<double> (x))); }
411 static inline long unsigned int log10(
long unsigned int x) {
return static_cast<long unsigned int> (std::log10 (static_cast<double> (x))); }
416 struct ScalarTraits<long long int>
427 static inline magnitudeType magnitude(
long long int a) {
return static_cast<long long int>(std::fabs(static_cast<double>(a))); }
428 static inline long long int zero() {
return 0; }
429 static inline long long int one() {
return 1; }
430 static inline long long int conjugate(
long long int x) {
return x; }
431 static inline long long int real(
long long int x) {
return x; }
432 static inline long long int imag(
long long int) {
return 0; }
433 static inline bool isnaninf(
long long int) {
return false; }
434 static inline void seedrandom(
unsigned int s) {
443 static inline long long int random() {
return std::rand(); }
444 static inline std::string
name() {
return "long long int"; }
445 static inline long long int squareroot(
long long int x) {
return (
long long int) std::sqrt((
double) x); }
446 static inline long long int pow(
long long int x,
long long int y) {
return (
long long int) std::pow((
double)x,(
double)y); }
449 static inline long long int log(
long long int x) {
return static_cast<long long int> (std::log (static_cast<double> (x))); }
450 static inline long long int log10(
long long int x) {
return static_cast<long long int> (std::log10 (static_cast<double> (x))); }
454 struct ScalarTraits<unsigned long long int>
465 static inline magnitudeType magnitude(
unsigned long long int a) {
return static_cast<unsigned long long int>(std::fabs(static_cast<double>(a))); }
466 static inline unsigned long long int zero() {
return 0; }
467 static inline unsigned long long int one() {
return 1; }
468 static inline unsigned long long int conjugate(
unsigned long long int x) {
return x; }
469 static inline unsigned long long int real(
unsigned long long int x) {
return x; }
470 static inline unsigned long long int imag(
unsigned long long int) {
return 0; }
471 static inline bool isnaninf(
unsigned long long int) {
return false; }
472 static inline void seedrandom(
unsigned int s) {
481 static inline unsigned long long int random() {
return std::rand(); }
482 static inline std::string
name() {
return "unsigned long long int"; }
483 static inline unsigned long long int squareroot(
unsigned long long int x) {
return (
unsigned long long int) std::sqrt((
double) x); }
484 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); }
487 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))); }
488 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))); }
492 #ifdef HAVE_TEUCHOS___INT64
495 struct ScalarTraits<__int64>
506 static inline magnitudeType magnitude(__int64 a) {
return static_cast<__int64
>(std::fabs(static_cast<double>(a))); }
507 static inline __int64
zero() {
return 0; }
508 static inline __int64
one() {
return 1; }
509 static inline __int64
conjugate(__int64 x) {
return x; }
510 static inline __int64
real(__int64 x) {
return x; }
511 static inline __int64
imag(__int64) {
return 0; }
512 static inline void seedrandom(
unsigned int s) {
521 static inline __int64
random() {
return std::rand(); }
522 static inline std::string
name() {
return "__int64"; }
523 static inline __int64
squareroot(__int64 x) {
return (__int64) std::sqrt((
double) x); }
524 static inline __int64
pow(__int64 x, __int64 y) {
return (__int64) std::pow((
double)x,(
double)y); }
527 static inline __int64 log(__int64 x) {
return static_cast<__int64
> (std::log (static_cast<double> (x))); }
528 static inline __int64 log10(__int64 x) {
return static_cast<__int64
> (std::log10 (static_cast<double> (x))); }
532 struct ScalarTraits<unsigned __int64>
543 static inline magnitudeType magnitude(
unsigned __int64 a) {
return static_cast<unsigned __int64
>(std::fabs(static_cast<double>(a))); }
544 static inline unsigned __int64
zero() {
return 0; }
545 static inline unsigned __int64
one() {
return 1; }
546 static inline unsigned __int64
conjugate(
unsigned __int64 x) {
return x; }
547 static inline unsigned __int64
real(
unsigned __int64 x) {
return x; }
548 static inline unsigned __int64
imag(
unsigned __int64) {
return 0; }
549 static inline void seedrandom(
unsigned int s) {
558 static inline unsigned __int64
random() {
return std::rand(); }
559 static inline std::string
name() {
return "unsigned __int64"; }
560 static inline unsigned __int64
squareroot(
unsigned __int64 x) {
return (
unsigned __int64) std::sqrt((
double) x); }
561 static inline unsigned __int64
pow(
unsigned __int64 x,
unsigned __int64 y) {
return (
unsigned __int64) std::pow((
double)x,(
double)y); }
564 static inline unsigned __int64 log(
unsigned __int64 x) {
return static_cast<unsigned __int64
> (std::log (static_cast<double> (x))); }
565 static inline unsigned __int64 log10(
unsigned __int64 x) {
return static_cast<unsigned __int64
> (std::log10 (static_cast<double> (x))); }
568 #endif // HAVE_TEUCHOS___INT64
572 extern TEUCHOSCORE_LIB_DLL_EXPORT
const float flt_nan;
577 struct ScalarTraits<float>
587 static inline float eps() {
588 return std::numeric_limits<float>::epsilon();
590 static inline float sfmin() {
591 return std::numeric_limits<float>::min();
593 static inline float base() {
594 return static_cast<float>(std::numeric_limits<float>::radix);
596 static inline float prec() {
599 static inline float t() {
600 return static_cast<float>(std::numeric_limits<float>::digits);
602 static inline float rnd() {
603 return ( std::numeric_limits<float>::round_style == std::round_to_nearest ?
one() :
zero() );
605 static inline float emin() {
606 return static_cast<float>(std::numeric_limits<float>::min_exponent);
608 static inline float rmin() {
609 return std::numeric_limits<float>::min();
611 static inline float emax() {
612 return static_cast<float>(std::numeric_limits<float>::max_exponent);
614 static inline float rmax() {
615 return std::numeric_limits<float>::max();
620 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
621 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
625 static inline float zero() {
return(0.0f); }
626 static inline float one() {
return(1.0f); }
627 static inline float conjugate(
float x) {
return(x); }
628 static inline float real(
float x) {
return x; }
629 static inline float imag(
float) {
return zero(); }
630 static inline float nan() {
632 return 0.0f/std::sin(0.0f);
637 static inline bool isnaninf(
float x) {
638 return generic_real_isnaninf<float>(x);
640 static inline void seedrandom(
unsigned int s) {
648 static inline float random() {
float rnd = (float) std::rand() / RAND_MAX;
return (-1.0f + 2.0f * rnd); }
649 static inline std::string
name() {
return "float"; }
653 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
654 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
657 const float rtn = std::sqrt(x);
662 static inline float pow(
float x,
float y) {
return std::pow(x,y); }
663 static inline float pi() {
return 3.14159265358979323846f; }
664 static inline float log(
float x) {
return std::log(x); }
665 static inline float log10(
float x) {
return std::log10(x); }
670 extern TEUCHOSCORE_LIB_DLL_EXPORT
const double dbl_nan;
675 struct ScalarTraits<double>
688 #if defined(HAVE_TEUCHOS_DOUBLE_TO_QD)
690 #elif defined(HAVE_TEUCHOS_DOUBLE_TO_ARPREC)
700 static inline double eps() {
701 return std::numeric_limits<double>::epsilon();
703 static inline double sfmin() {
704 return std::numeric_limits<double>::min();
706 static inline double base() {
707 return std::numeric_limits<double>::radix;
709 static inline double prec() {
712 static inline double t() {
713 return std::numeric_limits<double>::digits;
715 static inline double rnd() {
716 return ( std::numeric_limits<double>::round_style == std::round_to_nearest ?
double(1.0) :
double(0.0) );
718 static inline double emin() {
719 return std::numeric_limits<double>::min_exponent;
721 static inline double rmin() {
722 return std::numeric_limits<double>::min();
724 static inline double emax() {
725 return std::numeric_limits<double>::max_exponent;
727 static inline double rmax() {
728 return std::numeric_limits<double>::max();
733 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
734 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
738 static inline double zero() {
return 0.0; }
739 static inline double one() {
return 1.0; }
740 static inline double conjugate(
double x) {
return(x); }
741 static inline double real(
double x) {
return(x); }
742 static inline double imag(
double) {
return(0); }
743 static inline double nan() {
745 return 0.0/std::sin(0.0);
750 static inline bool isnaninf(
double x) {
751 return generic_real_isnaninf<double>(x);
753 static inline void seedrandom(
unsigned int s) {
761 static inline double random() {
double rnd = (double) std::rand() / RAND_MAX;
return (
double)(-1.0 + 2.0 *
rnd); }
762 static inline std::string
name() {
return "double"; }
766 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
767 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
770 const double rtn = std::sqrt(x);
775 static inline double pow(
double x,
double y) {
return std::pow(x,y); }
776 static inline double pi() {
return 3.14159265358979323846; }
777 static inline double log(
double x) {
return std::log(x); }
778 static inline double log10(
double x) {
return std::log10(x); }
782 #ifdef HAVE_TEUCHOSCORE_QUADMATH
785 struct ScalarTraits<__float128> {
799 static __float128
eps () {
800 return FLT128_EPSILON;
802 static __float128
sfmin () {
805 static __float128
base () {
808 static __float128
prec () {
811 static __float128
t () {
812 return FLT128_MANT_DIG;
814 static __float128
rnd () {
817 static __float128
emin () {
818 return FLT128_MIN_EXP;
820 static __float128
rmin () {
823 static __float128
emax () {
824 return FLT128_MAX_EXP;
826 static __float128
rmax () {
832 static __float128
zero () {
835 static __float128
one () {
838 static __float128
conjugate (
const __float128& x) {
841 static __float128
real (
const __float128& x) {
844 static __float128
imag (
const __float128& ) {
847 static __float128
nan () {
848 return strtoflt128 (
"NAN()", NULL);
850 static bool isnaninf (
const __float128& x) {
851 return isinfq (x) || isnanq (x);
853 static inline void seedrandom (
unsigned int s) {
861 static __float128
random () {
864 const __float128 scalingFactor =
865 static_cast<__float128
> (std::numeric_limits<double>::min ()) /
866 static_cast<__float128> (2.0);
867 const __float128 higherOrderTerm =
869 const __float128 lowerOrderTerm =
872 return higherOrderTerm + lowerOrderTerm;
874 static std::string
name () {
877 static __float128
squareroot (
const __float128& x) {
880 static __float128
pow (
const __float128& x,
const __float128& y) {
883 static __float128
pi() {
return 3.14159265358979323846; }
884 static __float128 log (
const __float128& x) {
887 static __float128 log10 (
const __float128& x) {
891 #endif // HAVE_TEUCHOSCORE_QUADMATH
895 #ifdef HAVE_TEUCHOS_QD
897 bool operator&&(
const dd_real &a,
const dd_real &b);
898 bool operator&&(
const qd_real &a,
const qd_real &b);
901 struct ScalarTraits<dd_real>
911 static inline dd_real
eps() {
return std::numeric_limits<dd_real>::epsilon(); }
912 static inline dd_real
sfmin() {
return std::numeric_limits<dd_real>::min(); }
913 static inline dd_real
base() {
return std::numeric_limits<dd_real>::radix; }
914 static inline dd_real
prec() {
return eps()*
base(); }
915 static inline dd_real
t() {
return std::numeric_limits<dd_real>::digits; }
916 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) ); }
917 static inline dd_real
emin() {
return std::numeric_limits<dd_real>::min_exponent; }
918 static inline dd_real
rmin() {
return std::numeric_limits<dd_real>::min(); }
919 static inline dd_real
emax() {
return std::numeric_limits<dd_real>::max_exponent; }
920 static inline dd_real
rmax() {
return std::numeric_limits<dd_real>::max(); }
924 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
925 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
929 static inline dd_real
zero() {
return dd_real(0.0); }
930 static inline dd_real
one() {
return dd_real(1.0); }
931 static inline dd_real
conjugate(dd_real x) {
return(x); }
932 static inline dd_real
real(dd_real x) {
return x ; }
933 static inline dd_real
imag(dd_real) {
return zero(); }
934 static inline dd_real
nan() {
return dd_real::_nan; }
935 static inline bool isnaninf(dd_real x) {
return isnan(x) || isinf(x); }
936 static inline void seedrandom(
unsigned int s) {
945 static inline dd_real
random() {
return ddrand(); }
946 static inline std::string
name() {
return "dd_real"; }
950 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
951 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
955 static inline dd_real
pow(dd_real x, dd_real y) { return ::pow(x,y); }
956 static inline dd_real
pi() {
return 3.14159265358979323846; }
958 static inline dd_real log(dd_real x) { return ::log(x); }
959 static inline dd_real log10(dd_real x) { return ::log10(x); }
964 struct ScalarTraits<qd_real>
974 static inline qd_real
eps() {
return std::numeric_limits<qd_real>::epsilon(); }
975 static inline qd_real
sfmin() {
return std::numeric_limits<qd_real>::min(); }
976 static inline qd_real
base() {
return std::numeric_limits<qd_real>::radix; }
977 static inline qd_real
prec() {
return eps()*
base(); }
978 static inline qd_real
t() {
return std::numeric_limits<qd_real>::digits; }
979 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) ); }
980 static inline qd_real
emin() {
return std::numeric_limits<qd_real>::min_exponent; }
981 static inline qd_real
rmin() {
return std::numeric_limits<qd_real>::min(); }
982 static inline qd_real
emax() {
return std::numeric_limits<qd_real>::max_exponent; }
983 static inline qd_real
rmax() {
return std::numeric_limits<qd_real>::max(); }
987 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
988 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
992 static inline qd_real
zero() {
return qd_real(0.0); }
993 static inline qd_real
one() {
return qd_real(1.0); }
994 static inline qd_real
conjugate(qd_real x) {
return(x); }
995 static inline qd_real
real(qd_real x) {
return x ; }
996 static inline qd_real
imag(qd_real) {
return zero(); }
997 static inline qd_real
nan() {
return qd_real::_nan; }
998 static inline bool isnaninf(qd_real x) {
return isnan(x) || isinf(x); }
999 static inline void seedrandom(
unsigned int s) {
1008 static inline qd_real
random() {
return qdrand(); }
1009 static inline std::string
name() {
return "qd_real"; }
1012 #ifdef TEUCHOS_DEBUG
1013 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
1014 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
1018 static inline qd_real
pow(qd_real x, qd_real y) { return ::pow(x,y); }
1019 static inline qd_real
pi() {
return 3.14159265358979323846; }
1021 static inline qd_real log(qd_real x) { return ::log(x); }
1022 static inline qd_real log10(qd_real x) { return ::log10(x); }
1026 #endif // HAVE_TEUCHOS_QD
1029 #ifdef HAVE_TEUCHOS_GNU_MP
1032 extern gmp_randclass gmp_rng;
1052 struct ScalarTraits<mpf_class>
1062 static inline mpf_class
zero() { mpf_class
zero = 0.0;
return zero; }
1063 static inline mpf_class
one() { mpf_class
one = 1.0;
return one; }
1064 static inline mpf_class
conjugate(mpf_class x) {
return x; }
1065 static inline mpf_class
real(mpf_class x) {
return(x); }
1066 static inline mpf_class
imag(mpf_class x) {
return(0); }
1067 static inline bool isnaninf(mpf_class x) {
return false; }
1068 static inline void seedrandom(
unsigned int s) {
1069 unsigned long int seedVal =
static_cast<unsigned long int>(s);
1070 gmp_rng.seed( seedVal );
1072 static inline mpf_class
random() {
1073 return gmp_rng.get_f();
1075 static inline std::string
name() {
return "mpf_class"; }
1076 static inline mpf_class
squareroot(mpf_class x) {
return std::sqrt(x); }
1077 static inline mpf_class
pow(mpf_class x, mpf_class y) {
return pow(x,y); }
1081 #endif // HAVE_TEUCHOS_GNU_MP
1083 #ifdef HAVE_TEUCHOS_ARPREC
1088 struct ScalarTraits<mp_real>
1100 static inline mp_real
zero() { mp_real
zero = 0.0;
return zero; }
1101 static inline mp_real
one() { mp_real
one = 1.0;
return one; }
1102 static inline mp_real
conjugate(mp_real x) {
return x; }
1103 static inline mp_real
real(mp_real x) {
return(x); }
1104 static inline mp_real
imag(mp_real x) {
return zero(); }
1105 static inline bool isnaninf(mp_real x) {
return false; }
1106 static inline void seedrandom(
unsigned int s) {
1107 long int seedVal =
static_cast<long int>(s);
1110 static inline mp_real
random() {
return mp_rand(); }
1111 static inline std::string
name() {
return "mp_real"; }
1112 static inline mp_real
squareroot(mp_real x) {
return sqrt(x); }
1113 static inline mp_real
pow(mp_real x, mp_real y) {
return pow(x,y); }
1114 static inline mp_real
pi() {
return 3.14159265358979323846; }
1119 #endif // HAVE_TEUCHOS_ARPREC
1122 #ifdef HAVE_TEUCHOS_COMPLEX
1127 struct ScalarTraits<
1131 typedef std::complex<T> ComplexT;
1132 typedef std::complex<typename ScalarTraits<T>::halfPrecision>
halfPrecision;
1133 typedef std::complex<typename ScalarTraits<T>::doublePrecision>
doublePrecision;
1134 typedef typename ScalarTraits<T>::magnitudeType
magnitudeType;
1137 static const bool isOrdinal = ScalarTraits<T>::isOrdinal;
1152 #ifdef TEUCHOS_DEBUG
1153 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
1154 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
1158 static inline ComplexT
zero() {
return ComplexT(ScalarTraits<magnitudeType>::zero(),ScalarTraits<magnitudeType>::zero()); }
1159 static inline ComplexT
one() {
return ComplexT(ScalarTraits<magnitudeType>::one(),ScalarTraits<magnitudeType>::zero()); }
1160 static inline ComplexT
conjugate(ComplexT a){
return ComplexT(a.real(),-a.imag()); }
1163 static inline ComplexT
nan() {
return ComplexT(ScalarTraits<magnitudeType>::nan(),ScalarTraits<magnitudeType>::nan()); }
1166 static inline ComplexT
random()
1170 return ComplexT(rnd1,rnd2);
1172 static inline std::string
name() {
return std::string(
"std::complex<")+std::string(ScalarTraits<magnitudeType>::name())+std::string(
">"); }
1174 static inline ComplexT
squareroot(ComplexT x)
1176 #ifdef TEUCHOS_DEBUG
1177 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
1178 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
1180 typedef ScalarTraits<magnitudeType> STMT;
1181 const T r = x.real(), i = x.imag(),
zero = STMT::zero(), two = 2.0;
1182 const T a = STMT::squareroot((r*r)+(i*i));
1183 const T nr = STMT::squareroot((a+r)/two);
1184 const T ni = ( i ==
zero ?
zero : STMT::squareroot((a-r)/two) );
1185 return ComplexT(nr,ni);
1199 static inline ComplexT
pow(ComplexT x, ComplexT y) {
return pow(x,y); }
1203 #endif // HAVE_TEUCHOS_COMPLEX
1204 #endif // DOXYGEN_SHOULD_SKIP_THIS
1208 #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 pi()
Returns the value of PI.
T coordinateType
Typedef for coordinates.
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.