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>
156 typedef char magnitudeType;
157 typedef char halfPrecision;
158 typedef char doublePrecision;
159 static const bool isComplex =
false;
160 static const bool isOrdinal =
true;
161 static const bool isComparable =
true;
162 static const bool hasMachineParameters =
false;
164 static inline magnitudeType magnitude(
char a) {
return static_cast<char>(std::fabs(static_cast<double>(a))); }
165 static inline char zero() {
return 0; }
166 static inline char one() {
return 1; }
167 static inline char conjugate(
char x) {
return x; }
168 static inline char real(
char x) {
return x; }
169 static inline char imag(
char) {
return 0; }
170 static inline bool isnaninf(
char ) {
return false; }
171 static inline void seedrandom(
unsigned int s) {
180 static inline char random() {
return std::rand(); }
181 static inline std::string name() {
return "char"; }
182 static inline char squareroot(
char x) {
return (
char) std::sqrt((
double) x); }
183 static inline char pow(
char x,
char y) {
return (
char) std::pow((
double)x,(
double)y); }
184 static inline char log(
char x) {
return static_cast<char> (std::log (static_cast<double> (x))); }
185 static inline char log10(
char x) {
return static_cast<char> (std::log10 (static_cast<double> (x))); }
190 struct ScalarTraits<short int>
192 typedef short int magnitudeType;
193 typedef short int halfPrecision;
194 typedef short int doublePrecision;
195 static const bool isComplex =
false;
196 static const bool isOrdinal =
true;
197 static const bool isComparable =
true;
198 static const bool hasMachineParameters =
false;
200 static inline magnitudeType magnitude(
short int a) {
return static_cast<short int>(std::fabs(static_cast<double>(a))); }
201 static inline short int zero() {
return 0; }
202 static inline short int one() {
return 1; }
203 static inline short int conjugate(
short int x) {
return x; }
204 static inline short int real(
short int x) {
return x; }
205 static inline short int imag(
short int) {
return 0; }
206 static inline bool isnaninf(
short int) {
return false; }
207 static inline void seedrandom(
unsigned int s) {
216 static inline short int random() {
return std::rand(); }
217 static inline std::string name() {
return "short int"; }
218 static inline short int squareroot(
short int x) {
return (
short int) std::sqrt((
double) x); }
219 static inline short int pow(
short int x,
short int y) {
return (
short int) std::pow((
double)x,(
double)y); }
220 static inline short int log(
short int x) {
return static_cast<short int> (std::log (static_cast<double> (x))); }
221 static inline short int log10(
short int x) {
return static_cast<short int> (std::log10 (static_cast<double> (x))); }
225 struct ScalarTraits<unsigned short int>
227 typedef unsigned short int magnitudeType;
228 typedef unsigned short int halfPrecision;
229 typedef unsigned short int doublePrecision;
230 static const bool isComplex =
false;
231 static const bool isOrdinal =
true;
232 static const bool isComparable =
true;
233 static const bool hasMachineParameters =
false;
235 static inline magnitudeType magnitude(
unsigned short int a) {
return static_cast<unsigned short int>(std::fabs(static_cast<double>(a))); }
236 static inline unsigned short int zero() {
return 0; }
237 static inline unsigned short int one() {
return 1; }
238 static inline unsigned short int conjugate(
unsigned short int x) {
return x; }
239 static inline unsigned short int real(
unsigned short int x) {
return x; }
240 static inline unsigned short int imag(
unsigned short int) {
return 0; }
241 static inline bool isnaninf(
unsigned short int) {
return false; }
242 static inline void seedrandom(
unsigned int s) {
251 static inline unsigned short int random() {
return std::rand(); }
252 static inline std::string name() {
return "unsigned short int"; }
253 static inline unsigned short int squareroot(
unsigned short int x) {
return (
unsigned short int) std::sqrt((
double) x); }
254 static inline unsigned short int pow(
unsigned short int x,
unsigned short int y) {
return (
unsigned short int) std::pow((
double)x,(
double)y); }
255 static inline unsigned short int log(
unsigned short int x) {
return static_cast<unsigned short int> (std::log (static_cast<double> (x))); }
256 static inline unsigned short int log10(
unsigned short int x) {
return static_cast<unsigned short int> (std::log10 (static_cast<double> (x))); }
261 struct ScalarTraits<int>
263 typedef int magnitudeType;
264 typedef int halfPrecision;
265 typedef int doublePrecision;
266 static const bool isComplex =
false;
267 static const bool isOrdinal =
true;
268 static const bool isComparable =
true;
269 static const bool hasMachineParameters =
false;
271 static inline magnitudeType magnitude(
int a) {
return static_cast<int>(std::fabs(static_cast<double>(a))); }
272 static inline int zero() {
return 0; }
273 static inline int one() {
return 1; }
274 static inline int conjugate(
int x) {
return x; }
275 static inline int real(
int x) {
return x; }
276 static inline int imag(
int) {
return 0; }
277 static inline bool isnaninf(
int) {
return false; }
278 static inline void seedrandom(
unsigned int s) {
287 static inline int random() {
return std::rand(); }
288 static inline std::string name() {
return "int"; }
289 static inline int squareroot(
int x) {
return (
int) std::sqrt((
double) x); }
290 static inline int pow(
int x,
int y) {
return (
int) std::pow((
double)x,(
double)y); }
291 static inline int log(
int x) {
return static_cast<int> (std::log (static_cast<double> (x))); }
292 static inline int log10(
int x) {
return static_cast<int> (std::log10 (static_cast<double> (x))); }
297 struct ScalarTraits<unsigned int>
299 typedef unsigned int magnitudeType;
300 typedef unsigned int halfPrecision;
301 typedef unsigned int doublePrecision;
302 static const bool isComplex =
false;
303 static const bool isOrdinal =
true;
304 static const bool isComparable =
true;
305 static const bool hasMachineParameters =
false;
307 static inline magnitudeType magnitude(
unsigned int a) {
return static_cast<unsigned int>(std::fabs(static_cast<double>(a))); }
308 static inline unsigned int zero() {
return 0; }
309 static inline unsigned int one() {
return 1; }
310 static inline unsigned int conjugate(
unsigned int x) {
return x; }
311 static inline unsigned int real(
unsigned int x) {
return x; }
312 static inline unsigned int imag(
unsigned int) {
return 0; }
313 static inline bool isnaninf(
unsigned int) {
return false; }
314 static inline void seedrandom(
unsigned int s) {
323 static inline unsigned int random() {
return std::rand(); }
324 static inline std::string name() {
return "unsigned int"; }
325 static inline unsigned int squareroot(
unsigned int x) {
return (
unsigned int) std::sqrt((
double) x); }
326 static inline unsigned int pow(
unsigned int x,
unsigned int y) {
return (
unsigned int) std::pow((
double)x,(
double)y); }
327 static inline unsigned int log(
unsigned int x) {
return static_cast<unsigned int> (std::log (static_cast<double> (x))); }
328 static inline unsigned int log10(
unsigned int x) {
return static_cast<unsigned int> (std::log10 (static_cast<double> (x))); }
333 struct ScalarTraits<long int>
335 typedef long int magnitudeType;
336 typedef long int halfPrecision;
337 typedef long int doublePrecision;
338 static const bool isComplex =
false;
339 static const bool isOrdinal =
true;
340 static const bool isComparable =
true;
341 static const bool hasMachineParameters =
false;
343 static inline magnitudeType magnitude(
long int a) {
return static_cast<long int>(std::fabs(static_cast<double>(a))); }
344 static inline long int zero() {
return 0; }
345 static inline long int one() {
return 1; }
346 static inline long int conjugate(
long int x) {
return x; }
347 static inline long int real(
long int x) {
return x; }
348 static inline long int imag(
long int) {
return 0; }
349 static inline bool isnaninf(
long int) {
return false; }
350 static inline void seedrandom(
unsigned int s) {
359 static inline long int random() {
return std::rand(); }
360 static inline std::string name() {
return "long int"; }
361 static inline long int squareroot(
long int x) {
return (
long int) std::sqrt((
double) x); }
362 static inline long int pow(
long int x,
long int y) {
return (
long int) std::pow((
double)x,(
double)y); }
365 static inline long int log(
long int x) {
return static_cast<long int> (std::log (static_cast<double> (x))); }
366 static inline long int log10(
long int x) {
return static_cast<long int> (std::log10 (static_cast<double> (x))); }
371 struct ScalarTraits<long unsigned int>
373 typedef long unsigned int magnitudeType;
374 typedef long unsigned int halfPrecision;
375 typedef long unsigned int doublePrecision;
376 static const bool isComplex =
false;
377 static const bool isOrdinal =
true;
378 static const bool isComparable =
true;
379 static const bool hasMachineParameters =
false;
381 static inline magnitudeType magnitude(
long unsigned int a) {
return static_cast<long unsigned int>(std::fabs(static_cast<double>(a))); }
382 static inline long unsigned int zero() {
return 0; }
383 static inline long unsigned int one() {
return 1; }
384 static inline long unsigned int conjugate(
long unsigned int x) {
return x; }
385 static inline long unsigned int real(
long unsigned int x) {
return x; }
386 static inline long unsigned int imag(
long unsigned int) {
return 0; }
387 static inline bool isnaninf(
long unsigned int) {
return false; }
388 static inline void seedrandom(
unsigned int s) {
397 static inline long unsigned int random() {
return std::rand(); }
398 static inline std::string name() {
return "long unsigned int"; }
399 static inline long unsigned int squareroot(
long unsigned int x) {
return (
long unsigned int) std::sqrt((
double) x); }
400 static inline long unsigned int pow(
long unsigned int x,
long unsigned int y) {
return (
long unsigned int) std::pow((
double)x,(
double)y); }
403 static inline long unsigned int log(
long unsigned int x) {
return static_cast<long unsigned int> (std::log (static_cast<double> (x))); }
404 static inline long unsigned int log10(
long unsigned int x) {
return static_cast<long unsigned int> (std::log10 (static_cast<double> (x))); }
408 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 410 struct ScalarTraits<long long int>
412 typedef long long int magnitudeType;
413 typedef long long int halfPrecision;
414 typedef long long int doublePrecision;
415 static const bool isComplex =
false;
416 static const bool isOrdinal =
true;
417 static const bool isComparable =
true;
418 static const bool hasMachineParameters =
false;
420 static inline magnitudeType magnitude(
long long int a) {
return static_cast<long long int>(std::fabs(static_cast<double>(a))); }
421 static inline long long int zero() {
return 0; }
422 static inline long long int one() {
return 1; }
423 static inline long long int conjugate(
long long int x) {
return x; }
424 static inline long long int real(
long long int x) {
return x; }
425 static inline long long int imag(
long long int) {
return 0; }
426 static inline bool isnaninf(
long long int) {
return false; }
427 static inline void seedrandom(
unsigned int s) {
436 static inline long long int random() {
return std::rand(); }
437 static inline std::string name() {
return "long long int"; }
438 static inline long long int squareroot(
long long int x) {
return (
long long int) std::sqrt((
double) x); }
439 static inline long long int pow(
long long int x,
long long int y) {
return (
long long int) std::pow((
double)x,(
double)y); }
442 static inline long long int log(
long long int x) {
return static_cast<long long int> (std::log (static_cast<double> (x))); }
443 static inline long long int log10(
long long int x) {
return static_cast<long long int> (std::log10 (static_cast<double> (x))); }
447 struct ScalarTraits<unsigned long long int>
449 typedef unsigned long long int magnitudeType;
450 typedef unsigned long long int halfPrecision;
451 typedef unsigned long long int doublePrecision;
452 static const bool isComplex =
false;
453 static const bool isOrdinal =
true;
454 static const bool isComparable =
true;
455 static const bool hasMachineParameters =
false;
457 static inline magnitudeType magnitude(
unsigned long long int a) {
return static_cast<unsigned long long int>(std::fabs(static_cast<double>(a))); }
458 static inline unsigned long long int zero() {
return 0; }
459 static inline unsigned long long int one() {
return 1; }
460 static inline unsigned long long int conjugate(
unsigned long long int x) {
return x; }
461 static inline unsigned long long int real(
unsigned long long int x) {
return x; }
462 static inline unsigned long long int imag(
unsigned long long int) {
return 0; }
463 static inline bool isnaninf(
unsigned long long int) {
return false; }
464 static inline void seedrandom(
unsigned int s) {
473 static inline unsigned long long int random() {
return std::rand(); }
474 static inline std::string name() {
return "unsigned long long int"; }
475 static inline unsigned long long int squareroot(
unsigned long long int x) {
return (
unsigned long long int) std::sqrt((
double) x); }
476 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); }
479 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))); }
480 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))); }
482 #endif // HAVE_TEUCHOS_LONG_LONG_INT 485 #ifdef HAVE_TEUCHOS___INT64 488 struct ScalarTraits<__int64>
490 typedef __int64 magnitudeType;
491 typedef __int64 halfPrecision;
492 typedef __int64 doublePrecision;
493 static const bool isComplex =
false;
494 static const bool isOrdinal =
true;
495 static const bool isComparable =
true;
496 static const bool hasMachineParameters =
false;
498 static inline magnitudeType magnitude(__int64 a) {
return static_cast<__int64
>(std::fabs(static_cast<double>(a))); }
499 static inline __int64 zero() {
return 0; }
500 static inline __int64 one() {
return 1; }
501 static inline __int64 conjugate(__int64 x) {
return x; }
502 static inline __int64 real(__int64 x) {
return x; }
503 static inline __int64 imag(__int64) {
return 0; }
504 static inline void seedrandom(
unsigned int s) {
513 static inline __int64 random() {
return std::rand(); }
514 static inline std::string name() {
return "__int64"; }
515 static inline __int64 squareroot(__int64 x) {
return (__int64) std::sqrt((
double) x); }
516 static inline __int64 pow(__int64 x, __int64 y) {
return (__int64) std::pow((
double)x,(
double)y); }
519 static inline __int64 log(__int64 x) {
return static_cast<__int64
> (std::log (static_cast<double> (x))); }
520 static inline __int64 log10(__int64 x) {
return static_cast<__int64
> (std::log10 (static_cast<double> (x))); }
524 struct ScalarTraits<unsigned __int64>
526 typedef unsigned __int64 magnitudeType;
527 typedef unsigned __int64 halfPrecision;
528 typedef unsigned __int64 doublePrecision;
529 static const bool isComplex =
false;
530 static const bool isOrdinal =
true;
531 static const bool isComparable =
true;
532 static const bool hasMachineParameters =
false;
534 static inline magnitudeType magnitude(
unsigned __int64 a) {
return static_cast<unsigned __int64
>(std::fabs(static_cast<double>(a))); }
535 static inline unsigned __int64 zero() {
return 0; }
536 static inline unsigned __int64 one() {
return 1; }
537 static inline unsigned __int64 conjugate(
unsigned __int64 x) {
return x; }
538 static inline unsigned __int64 real(
unsigned __int64 x) {
return x; }
539 static inline unsigned __int64 imag(
unsigned __int64) {
return 0; }
540 static inline void seedrandom(
unsigned int s) {
549 static inline unsigned __int64 random() {
return std::rand(); }
550 static inline std::string name() {
return "unsigned __int64"; }
551 static inline unsigned __int64 squareroot(
unsigned __int64 x) {
return (
unsigned __int64) std::sqrt((
double) x); }
552 static inline unsigned __int64 pow(
unsigned __int64 x,
unsigned __int64 y) {
return (
unsigned __int64) std::pow((
double)x,(
double)y); }
555 static inline unsigned __int64 log(
unsigned __int64 x) {
return static_cast<unsigned __int64
> (std::log (static_cast<double> (x))); }
556 static inline unsigned __int64 log10(
unsigned __int64 x) {
return static_cast<unsigned __int64
> (std::log10 (static_cast<double> (x))); }
559 #endif // HAVE_TEUCHOS___INT64 563 extern TEUCHOSCORE_LIB_DLL_EXPORT
const float flt_nan;
568 struct ScalarTraits<float>
570 typedef float magnitudeType;
571 typedef float halfPrecision;
572 typedef double doublePrecision;
573 static const bool isComplex =
false;
574 static const bool isOrdinal =
false;
575 static const bool isComparable =
true;
576 static const bool hasMachineParameters =
true;
577 static inline float eps() {
578 return std::numeric_limits<float>::epsilon();
580 static inline float sfmin() {
581 return std::numeric_limits<float>::min();
583 static inline float base() {
584 return static_cast<float>(std::numeric_limits<float>::radix);
586 static inline float prec() {
589 static inline float t() {
590 return static_cast<float>(std::numeric_limits<float>::digits);
592 static inline float rnd() {
593 return ( std::numeric_limits<float>::round_style == std::round_to_nearest ? one() : zero() );
595 static inline float emin() {
596 return static_cast<float>(std::numeric_limits<float>::min_exponent);
598 static inline float rmin() {
599 return std::numeric_limits<float>::min();
601 static inline float emax() {
602 return static_cast<float>(std::numeric_limits<float>::max_exponent);
604 static inline float rmax() {
605 return std::numeric_limits<float>::max();
607 static inline magnitudeType magnitude(
float a)
610 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
611 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
615 static inline float zero() {
return(0.0f); }
616 static inline float one() {
return(1.0f); }
617 static inline float conjugate(
float x) {
return(x); }
618 static inline float real(
float x) {
return x; }
619 static inline float imag(
float) {
return zero(); }
620 static inline float nan() {
622 return 0.0f/std::sin(0.0f);
627 static inline bool isnaninf(
float x) {
628 return generic_real_isnaninf<float>(x);
630 static inline void seedrandom(
unsigned int s) {
638 static inline float random() {
float rnd = (float) std::rand() / RAND_MAX;
return (-1.0f + 2.0f * rnd); }
639 static inline std::string name() {
return "float"; }
640 static inline float squareroot(
float x)
643 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
644 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
647 const float rtn = std::sqrt(x);
652 static inline float pow(
float x,
float y) {
return std::pow(x,y); }
653 static inline float pi() {
return 3.14159265358979323846f; }
654 static inline float log(
float x) {
return std::log(x); }
655 static inline float log10(
float x) {
return std::log10(x); }
660 extern TEUCHOSCORE_LIB_DLL_EXPORT
const double dbl_nan;
665 struct ScalarTraits<double>
667 typedef double magnitudeType;
668 typedef float halfPrecision;
678 #if defined(HAVE_TEUCHOS_DOUBLE_TO_QD) 679 typedef dd_real doublePrecision;
680 #elif defined(HAVE_TEUCHOS_DOUBLE_TO_ARPREC) 681 typedef mp_real doublePrecision;
683 typedef double doublePrecision;
685 static const bool isComplex =
false;
686 static const bool isOrdinal =
false;
687 static const bool isComparable =
true;
688 static const bool hasMachineParameters =
true;
689 static inline double eps() {
690 return std::numeric_limits<double>::epsilon();
692 static inline double sfmin() {
693 return std::numeric_limits<double>::min();
695 static inline double base() {
696 return std::numeric_limits<double>::radix;
698 static inline double prec() {
701 static inline double t() {
702 return std::numeric_limits<double>::digits;
704 static inline double rnd() {
705 return ( std::numeric_limits<double>::round_style == std::round_to_nearest ?
double(1.0) :
double(0.0) );
707 static inline double emin() {
708 return std::numeric_limits<double>::min_exponent;
710 static inline double rmin() {
711 return std::numeric_limits<double>::min();
713 static inline double emax() {
714 return std::numeric_limits<double>::max_exponent;
716 static inline double rmax() {
717 return std::numeric_limits<double>::max();
719 static inline magnitudeType magnitude(
double a)
722 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
723 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
727 static inline double zero() {
return 0.0; }
728 static inline double one() {
return 1.0; }
729 static inline double conjugate(
double x) {
return(x); }
730 static inline double real(
double x) {
return(x); }
731 static inline double imag(
double) {
return(0); }
732 static inline double nan() {
734 return 0.0/std::sin(0.0);
739 static inline bool isnaninf(
double x) {
740 return generic_real_isnaninf<double>(x);
742 static inline void seedrandom(
unsigned int s) {
750 static inline double random() {
double rnd = (double) std::rand() / RAND_MAX;
return (
double)(-1.0 + 2.0 * rnd); }
751 static inline std::string name() {
return "double"; }
752 static inline double squareroot(
double x)
755 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
756 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
759 const double rtn = std::sqrt(x);
764 static inline double pow(
double x,
double y) {
return std::pow(x,y); }
765 static inline double pi() {
return 3.14159265358979323846; }
766 static inline double log(
double x) {
return std::log(x); }
767 static inline double log10(
double x) {
return std::log10(x); }
771 #ifdef HAVE_TEUCHOSCORE_QUADMATH 774 struct ScalarTraits<__float128> {
775 typedef __float128 magnitudeType;
779 typedef __float128 doublePrecision;
780 typedef double halfPrecision;
782 static const bool isComplex =
false;
783 static const bool isOrdinal =
false;
784 static const bool isComparable =
true;
785 static const bool hasMachineParameters =
true;
787 static __float128 eps () {
788 return FLT128_EPSILON;
790 static __float128 sfmin () {
793 static __float128 base () {
796 static __float128 prec () {
797 return eps () * base ();
799 static __float128 t () {
800 return FLT128_MANT_DIG;
802 static __float128 rnd () {
805 static __float128 emin () {
806 return FLT128_MIN_EXP;
808 static __float128 rmin () {
811 static __float128 emax () {
812 return FLT128_MAX_EXP;
814 static __float128 rmax () {
817 static magnitudeType magnitude (
const __float128& x) {
820 static __float128 zero () {
823 static __float128 one () {
826 static __float128 conjugate (
const __float128& x) {
829 static __float128 real (
const __float128& x) {
832 static __float128 imag (
const __float128& ) {
835 static __float128 nan () {
836 return strtoflt128 (
"NAN()", NULL);
838 static bool isnaninf (
const __float128& x) {
839 return isinfq (x) || isnanq (x);
841 static inline void seedrandom (
unsigned int s) {
849 static __float128 random () {
852 const __float128 scalingFactor =
853 static_cast<__float128
> (std::numeric_limits<double>::min ()) /
854 static_cast<__float128> (2.0);
855 const __float128 higherOrderTerm =
856 static_cast<__float128
> (ScalarTraits<double>::random ());
857 const __float128 lowerOrderTerm =
858 static_cast<__float128
> (ScalarTraits<double>::random ()) *
860 return higherOrderTerm + lowerOrderTerm;
862 static std::string name () {
865 static __float128 squareroot (
const __float128& x) {
868 static __float128 pow (
const __float128& x,
const __float128& y) {
871 static __float128 pi() {
return 3.14159265358979323846; }
872 static __float128 log (
const __float128& x) {
875 static __float128 log10 (
const __float128& x) {
879 #endif // HAVE_TEUCHOSCORE_QUADMATH 883 #ifdef HAVE_TEUCHOS_QD 885 bool operator&&(
const dd_real &a,
const dd_real &b);
886 bool operator&&(
const qd_real &a,
const qd_real &b);
889 struct ScalarTraits<dd_real>
891 typedef dd_real magnitudeType;
892 typedef double halfPrecision;
893 typedef qd_real doublePrecision;
894 static const bool isComplex =
false;
895 static const bool isOrdinal =
false;
896 static const bool isComparable =
true;
897 static const bool hasMachineParameters =
true;
898 static inline dd_real eps() {
return std::numeric_limits<dd_real>::epsilon(); }
899 static inline dd_real sfmin() {
return std::numeric_limits<dd_real>::min(); }
900 static inline dd_real base() {
return std::numeric_limits<dd_real>::radix; }
901 static inline dd_real prec() {
return eps()*base(); }
902 static inline dd_real t() {
return std::numeric_limits<dd_real>::digits; }
903 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) ); }
904 static inline dd_real emin() {
return std::numeric_limits<dd_real>::min_exponent; }
905 static inline dd_real rmin() {
return std::numeric_limits<dd_real>::min(); }
906 static inline dd_real emax() {
return std::numeric_limits<dd_real>::max_exponent; }
907 static inline dd_real rmax() {
return std::numeric_limits<dd_real>::max(); }
908 static inline magnitudeType magnitude(dd_real a)
911 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
912 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
916 static inline dd_real zero() {
return dd_real(0.0); }
917 static inline dd_real one() {
return dd_real(1.0); }
918 static inline dd_real conjugate(dd_real x) {
return(x); }
919 static inline dd_real real(dd_real x) {
return x ; }
920 static inline dd_real imag(dd_real) {
return zero(); }
921 static inline dd_real nan() {
return dd_real::_nan; }
922 static inline bool isnaninf(dd_real x) {
return isnan(x) || isinf(x); }
923 static inline void seedrandom(
unsigned int s) {
932 static inline dd_real random() {
return ddrand(); }
933 static inline std::string name() {
return "dd_real"; }
934 static inline dd_real squareroot(dd_real x)
937 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
938 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
942 static inline dd_real pow(dd_real x, dd_real y) { return ::pow(x,y); }
943 static inline dd_real pi() {
return 3.14159265358979323846; }
945 static inline dd_real log(dd_real x) { return ::log(x); }
946 static inline dd_real log10(dd_real x) { return ::log10(x); }
951 struct ScalarTraits<qd_real>
953 typedef qd_real magnitudeType;
954 typedef dd_real halfPrecision;
955 typedef qd_real doublePrecision;
956 static const bool isComplex =
false;
957 static const bool isOrdinal =
false;
958 static const bool isComparable =
true;
959 static const bool hasMachineParameters =
true;
960 static inline qd_real eps() {
return std::numeric_limits<qd_real>::epsilon(); }
961 static inline qd_real sfmin() {
return std::numeric_limits<qd_real>::min(); }
962 static inline qd_real base() {
return std::numeric_limits<qd_real>::radix; }
963 static inline qd_real prec() {
return eps()*base(); }
964 static inline qd_real t() {
return std::numeric_limits<qd_real>::digits; }
965 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) ); }
966 static inline qd_real emin() {
return std::numeric_limits<qd_real>::min_exponent; }
967 static inline qd_real rmin() {
return std::numeric_limits<qd_real>::min(); }
968 static inline qd_real emax() {
return std::numeric_limits<qd_real>::max_exponent; }
969 static inline qd_real rmax() {
return std::numeric_limits<qd_real>::max(); }
970 static inline magnitudeType magnitude(qd_real a)
973 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
974 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
978 static inline qd_real zero() {
return qd_real(0.0); }
979 static inline qd_real one() {
return qd_real(1.0); }
980 static inline qd_real conjugate(qd_real x) {
return(x); }
981 static inline qd_real real(qd_real x) {
return x ; }
982 static inline qd_real imag(qd_real) {
return zero(); }
983 static inline qd_real nan() {
return qd_real::_nan; }
984 static inline bool isnaninf(qd_real x) {
return isnan(x) || isinf(x); }
985 static inline void seedrandom(
unsigned int s) {
994 static inline qd_real random() {
return qdrand(); }
995 static inline std::string name() {
return "qd_real"; }
996 static inline qd_real squareroot(qd_real x)
999 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
1000 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
1004 static inline qd_real pow(qd_real x, qd_real y) { return ::pow(x,y); }
1005 static inline qd_real pi() {
return 3.14159265358979323846; }
1007 static inline qd_real log(qd_real x) { return ::log(x); }
1008 static inline qd_real log10(qd_real x) { return ::log10(x); }
1012 #endif // HAVE_TEUCHOS_QD 1015 #ifdef HAVE_TEUCHOS_GNU_MP 1018 extern gmp_randclass gmp_rng;
1038 struct ScalarTraits<mpf_class>
1040 typedef mpf_class magnitudeType;
1041 typedef mpf_class halfPrecision;
1042 typedef mpf_class doublePrecision;
1043 static const bool isComplex =
false;
1044 static const bool hasMachineParameters =
false;
1046 static magnitudeType magnitude(mpf_class a) {
return std::abs(a); }
1047 static inline mpf_class zero() { mpf_class zero = 0.0;
return zero; }
1048 static inline mpf_class one() { mpf_class one = 1.0;
return one; }
1049 static inline mpf_class conjugate(mpf_class x) {
return x; }
1050 static inline mpf_class real(mpf_class x) {
return(x); }
1051 static inline mpf_class imag(mpf_class x) {
return(0); }
1052 static inline bool isnaninf(mpf_class x) {
return false; }
1053 static inline void seedrandom(
unsigned int s) {
1054 unsigned long int seedVal =
static_cast<unsigned long int>(s);
1055 gmp_rng.seed( seedVal );
1057 static inline mpf_class random() {
1058 return gmp_rng.get_f();
1060 static inline std::string name() {
return "mpf_class"; }
1061 static inline mpf_class squareroot(mpf_class x) {
return std::sqrt(x); }
1062 static inline mpf_class pow(mpf_class x, mpf_class y) {
return pow(x,y); }
1066 #endif // HAVE_TEUCHOS_GNU_MP 1068 #ifdef HAVE_TEUCHOS_ARPREC 1073 struct ScalarTraits<mp_real>
1075 typedef mp_real magnitudeType;
1076 typedef double halfPrecision;
1077 typedef mp_real doublePrecision;
1078 static const bool isComplex =
false;
1079 static const bool isComparable =
true;
1080 static const bool isOrdinal =
false;
1081 static const bool hasMachineParameters =
false;
1083 static magnitudeType magnitude(mp_real a) {
return abs(a); }
1084 static inline mp_real zero() { mp_real zero = 0.0;
return zero; }
1085 static inline mp_real one() { mp_real one = 1.0;
return one; }
1086 static inline mp_real conjugate(mp_real x) {
return x; }
1087 static inline mp_real real(mp_real x) {
return(x); }
1088 static inline mp_real imag(mp_real x) {
return zero(); }
1089 static inline bool isnaninf(mp_real x) {
return false; }
1090 static inline void seedrandom(
unsigned int s) {
1091 long int seedVal =
static_cast<long int>(s);
1094 static inline mp_real random() {
return mp_rand(); }
1095 static inline std::string name() {
return "mp_real"; }
1096 static inline mp_real squareroot(mp_real x) {
return sqrt(x); }
1097 static inline mp_real pow(mp_real x, mp_real y) {
return pow(x,y); }
1098 static inline mp_real pi() {
return 3.14159265358979323846; }
1103 #endif // HAVE_TEUCHOS_ARPREC 1106 #ifdef HAVE_TEUCHOS_COMPLEX 1111 struct ScalarTraits<
1115 typedef std::complex<T> ComplexT;
1116 typedef std::complex<typename ScalarTraits<T>::halfPrecision> halfPrecision;
1117 typedef std::complex<typename ScalarTraits<T>::doublePrecision> doublePrecision;
1118 typedef typename ScalarTraits<T>::magnitudeType magnitudeType;
1119 static const bool isComplex =
true;
1120 static const bool isOrdinal = ScalarTraits<T>::isOrdinal;
1121 static const bool isComparable =
false;
1122 static const bool hasMachineParameters =
true;
1123 static inline magnitudeType eps() {
return ScalarTraits<magnitudeType>::eps(); }
1124 static inline magnitudeType sfmin() {
return ScalarTraits<magnitudeType>::sfmin(); }
1125 static inline magnitudeType base() {
return ScalarTraits<magnitudeType>::base(); }
1126 static inline magnitudeType prec() {
return ScalarTraits<magnitudeType>::prec(); }
1127 static inline magnitudeType t() {
return ScalarTraits<magnitudeType>::t(); }
1128 static inline magnitudeType rnd() {
return ScalarTraits<magnitudeType>::rnd(); }
1129 static inline magnitudeType emin() {
return ScalarTraits<magnitudeType>::emin(); }
1130 static inline magnitudeType rmin() {
return ScalarTraits<magnitudeType>::rmin(); }
1131 static inline magnitudeType emax() {
return ScalarTraits<magnitudeType>::emax(); }
1132 static inline magnitudeType rmax() {
return ScalarTraits<magnitudeType>::rmax(); }
1133 static magnitudeType magnitude(ComplexT a)
1135 #ifdef TEUCHOS_DEBUG 1136 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
1137 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
1141 static inline ComplexT zero() {
return ComplexT(ScalarTraits<magnitudeType>::zero(),ScalarTraits<magnitudeType>::zero()); }
1142 static inline ComplexT one() {
return ComplexT(ScalarTraits<magnitudeType>::one(),ScalarTraits<magnitudeType>::zero()); }
1143 static inline ComplexT conjugate(ComplexT a){
return ComplexT(a.real(),-a.imag()); }
1144 static inline magnitudeType real(ComplexT a) {
return a.real(); }
1145 static inline magnitudeType imag(ComplexT a) {
return a.imag(); }
1146 static inline ComplexT nan() {
return ComplexT(ScalarTraits<magnitudeType>::nan(),ScalarTraits<magnitudeType>::nan()); }
1147 static inline bool isnaninf(ComplexT x) {
return ScalarTraits<magnitudeType>::isnaninf(x.real()) || ScalarTraits<magnitudeType>::isnaninf(x.imag()); }
1148 static inline void seedrandom(
unsigned int s) { ScalarTraits<magnitudeType>::seedrandom(s); }
1149 static inline ComplexT random()
1151 const T rnd1 = ScalarTraits<magnitudeType>::random();
1152 const T rnd2 = ScalarTraits<magnitudeType>::random();
1153 return ComplexT(rnd1,rnd2);
1155 static inline std::string name() {
return std::string(
"std::complex<")+std::string(ScalarTraits<magnitudeType>::name())+std::string(
">"); }
1157 static inline ComplexT squareroot(ComplexT x)
1159 #ifdef TEUCHOS_DEBUG 1160 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
1161 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
1163 typedef ScalarTraits<magnitudeType> STMT;
1164 const T r = x.real(), i = x.imag(), zero = STMT::zero(), two = 2.0;
1165 const T a = STMT::squareroot((r*r)+(i*i));
1166 const T nr = STMT::squareroot((a+r)/two);
1167 const T ni = ( i == zero ? zero : STMT::squareroot((a-r)/two) );
1168 return ComplexT(nr,ni);
1182 static inline ComplexT pow(ComplexT x, ComplexT y) {
return pow(x,y); }
1183 static inline ComplexT pi() {
return ScalarTraits<T>::pi(); }
1186 #endif // HAVE_TEUCHOS_COMPLEX 1187 #endif // DOXYGEN_SHOULD_SKIP_THIS 1191 #endif // _TEUCHOS_SCALARTRAITS_HPP_ 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.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.