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 typedef std::numeric_limits<Scalar> STD_NL;
127 const Scalar tol = 1.0;
128 if (!(x <= tol) && !(x > tol))
return true;
130 Scalar z =
static_cast<Scalar
>(0.0) * x;
131 if (!(z <= tol) && !(z > tol))
return true;
133 if (x == STD_NL::infinity() || x == -STD_NL::infinity())
return true;
139 #define TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR( VALUE, MSG ) \ 140 if (isnaninf(VALUE)) { \ 141 std::ostringstream omsg; \ 143 Teuchos::throwScalarTraitsNanInfError(omsg.str()); \ 148 struct ScalarTraits<char>
150 typedef char magnitudeType;
151 typedef char halfPrecision;
152 typedef char doublePrecision;
153 static const bool isComplex =
false;
154 static const bool isOrdinal =
true;
155 static const bool isComparable =
true;
156 static const bool hasMachineParameters =
false;
158 static inline magnitudeType magnitude(
char a) {
return static_cast<char>(std::fabs(static_cast<double>(a))); }
159 static inline char zero() {
return 0; }
160 static inline char one() {
return 1; }
161 static inline char conjugate(
char x) {
return x; }
162 static inline char real(
char x) {
return x; }
163 static inline char imag(
char) {
return 0; }
164 static inline bool isnaninf(
char ) {
return false; }
165 static inline void seedrandom(
unsigned int s) {
174 static inline char random() {
return std::rand(); }
175 static inline std::string name() {
return "char"; }
176 static inline char squareroot(
char x) {
return (
char) std::sqrt((
double) x); }
177 static inline char pow(
char x,
char y) {
return (
char) std::pow((
double)x,(
double)y); }
178 static inline char log(
char x) {
return static_cast<char> (std::log (static_cast<double> (x))); }
179 static inline char log10(
char x) {
return static_cast<char> (std::log10 (static_cast<double> (x))); }
184 struct ScalarTraits<short int>
186 typedef short int magnitudeType;
187 typedef short int halfPrecision;
188 typedef short int doublePrecision;
189 static const bool isComplex =
false;
190 static const bool isOrdinal =
true;
191 static const bool isComparable =
true;
192 static const bool hasMachineParameters =
false;
194 static inline magnitudeType magnitude(
short int a) {
return static_cast<short int>(std::fabs(static_cast<double>(a))); }
195 static inline short int zero() {
return 0; }
196 static inline short int one() {
return 1; }
197 static inline short int conjugate(
short int x) {
return x; }
198 static inline short int real(
short int x) {
return x; }
199 static inline short int imag(
short int) {
return 0; }
200 static inline bool isnaninf(
short int) {
return false; }
201 static inline void seedrandom(
unsigned int s) {
210 static inline short int random() {
return std::rand(); }
211 static inline std::string name() {
return "short int"; }
212 static inline short int squareroot(
short int x) {
return (
short int) std::sqrt((
double) x); }
213 static inline short int pow(
short int x,
short int y) {
return (
short int) std::pow((
double)x,(
double)y); }
214 static inline short int log(
short int x) {
return static_cast<short int> (std::log (static_cast<double> (x))); }
215 static inline short int log10(
short int x) {
return static_cast<short int> (std::log10 (static_cast<double> (x))); }
219 struct ScalarTraits<unsigned short int>
221 typedef unsigned short int magnitudeType;
222 typedef unsigned short int halfPrecision;
223 typedef unsigned short int doublePrecision;
224 static const bool isComplex =
false;
225 static const bool isOrdinal =
true;
226 static const bool isComparable =
true;
227 static const bool hasMachineParameters =
false;
229 static inline magnitudeType magnitude(
unsigned short int a) {
return static_cast<unsigned short int>(std::fabs(static_cast<double>(a))); }
230 static inline unsigned short int zero() {
return 0; }
231 static inline unsigned short int one() {
return 1; }
232 static inline unsigned short int conjugate(
unsigned short int x) {
return x; }
233 static inline unsigned short int real(
unsigned short int x) {
return x; }
234 static inline unsigned short int imag(
unsigned short int) {
return 0; }
235 static inline bool isnaninf(
unsigned short int) {
return false; }
236 static inline void seedrandom(
unsigned int s) {
245 static inline unsigned short int random() {
return std::rand(); }
246 static inline std::string name() {
return "unsigned short int"; }
247 static inline unsigned short int squareroot(
unsigned short int x) {
return (
unsigned short int) std::sqrt((
double) x); }
248 static inline unsigned short int pow(
unsigned short int x,
unsigned short int y) {
return (
unsigned short int) std::pow((
double)x,(
double)y); }
249 static inline unsigned short int log(
unsigned short int x) {
return static_cast<unsigned short int> (std::log (static_cast<double> (x))); }
250 static inline unsigned short int log10(
unsigned short int x) {
return static_cast<unsigned short int> (std::log10 (static_cast<double> (x))); }
255 struct ScalarTraits<int>
257 typedef int magnitudeType;
258 typedef int halfPrecision;
259 typedef int doublePrecision;
260 static const bool isComplex =
false;
261 static const bool isOrdinal =
true;
262 static const bool isComparable =
true;
263 static const bool hasMachineParameters =
false;
265 static inline magnitudeType magnitude(
int a) {
return static_cast<int>(std::fabs(static_cast<double>(a))); }
266 static inline int zero() {
return 0; }
267 static inline int one() {
return 1; }
268 static inline int conjugate(
int x) {
return x; }
269 static inline int real(
int x) {
return x; }
270 static inline int imag(
int) {
return 0; }
271 static inline bool isnaninf(
int) {
return false; }
272 static inline void seedrandom(
unsigned int s) {
281 static inline int random() {
return std::rand(); }
282 static inline std::string name() {
return "int"; }
283 static inline int squareroot(
int x) {
return (
int) std::sqrt((
double) x); }
284 static inline int pow(
int x,
int y) {
return (
int) std::pow((
double)x,(
double)y); }
285 static inline int log(
int x) {
return static_cast<int> (std::log (static_cast<double> (x))); }
286 static inline int log10(
int x) {
return static_cast<int> (std::log10 (static_cast<double> (x))); }
291 struct ScalarTraits<unsigned int>
293 typedef unsigned int magnitudeType;
294 typedef unsigned int halfPrecision;
295 typedef unsigned int doublePrecision;
296 static const bool isComplex =
false;
297 static const bool isOrdinal =
true;
298 static const bool isComparable =
true;
299 static const bool hasMachineParameters =
false;
301 static inline magnitudeType magnitude(
unsigned int a) {
return static_cast<unsigned int>(std::fabs(static_cast<double>(a))); }
302 static inline unsigned int zero() {
return 0; }
303 static inline unsigned int one() {
return 1; }
304 static inline unsigned int conjugate(
unsigned int x) {
return x; }
305 static inline unsigned int real(
unsigned int x) {
return x; }
306 static inline unsigned int imag(
unsigned int) {
return 0; }
307 static inline bool isnaninf(
unsigned int) {
return false; }
308 static inline void seedrandom(
unsigned int s) {
317 static inline unsigned int random() {
return std::rand(); }
318 static inline std::string name() {
return "unsigned int"; }
319 static inline unsigned int squareroot(
unsigned int x) {
return (
unsigned int) std::sqrt((
double) x); }
320 static inline unsigned int pow(
unsigned int x,
unsigned int y) {
return (
unsigned int) std::pow((
double)x,(
double)y); }
321 static inline unsigned int log(
unsigned int x) {
return static_cast<unsigned int> (std::log (static_cast<double> (x))); }
322 static inline unsigned int log10(
unsigned int x) {
return static_cast<unsigned int> (std::log10 (static_cast<double> (x))); }
327 struct ScalarTraits<long int>
329 typedef long int magnitudeType;
330 typedef long int halfPrecision;
331 typedef long int doublePrecision;
332 static const bool isComplex =
false;
333 static const bool isOrdinal =
true;
334 static const bool isComparable =
true;
335 static const bool hasMachineParameters =
false;
337 static inline magnitudeType magnitude(
long int a) {
return static_cast<long int>(std::fabs(static_cast<double>(a))); }
338 static inline long int zero() {
return 0; }
339 static inline long int one() {
return 1; }
340 static inline long int conjugate(
long int x) {
return x; }
341 static inline long int real(
long int x) {
return x; }
342 static inline long int imag(
long int) {
return 0; }
343 static inline bool isnaninf(
long int) {
return false; }
344 static inline void seedrandom(
unsigned int s) {
353 static inline long int random() {
return std::rand(); }
354 static inline std::string name() {
return "long int"; }
355 static inline long int squareroot(
long int x) {
return (
long int) std::sqrt((
double) x); }
356 static inline long int pow(
long int x,
long int y) {
return (
long int) std::pow((
double)x,(
double)y); }
359 static inline long int log(
long int x) {
return static_cast<long int> (std::log (static_cast<double> (x))); }
360 static inline long int log10(
long int x) {
return static_cast<long int> (std::log10 (static_cast<double> (x))); }
365 struct ScalarTraits<long unsigned int>
367 typedef long unsigned int magnitudeType;
368 typedef long unsigned int halfPrecision;
369 typedef long unsigned int doublePrecision;
370 static const bool isComplex =
false;
371 static const bool isOrdinal =
true;
372 static const bool isComparable =
true;
373 static const bool hasMachineParameters =
false;
375 static inline magnitudeType magnitude(
long unsigned int a) {
return static_cast<long unsigned int>(std::fabs(static_cast<double>(a))); }
376 static inline long unsigned int zero() {
return 0; }
377 static inline long unsigned int one() {
return 1; }
378 static inline long unsigned int conjugate(
long unsigned int x) {
return x; }
379 static inline long unsigned int real(
long unsigned int x) {
return x; }
380 static inline long unsigned int imag(
long unsigned int) {
return 0; }
381 static inline bool isnaninf(
long unsigned int) {
return false; }
382 static inline void seedrandom(
unsigned int s) {
391 static inline long unsigned int random() {
return std::rand(); }
392 static inline std::string name() {
return "long unsigned int"; }
393 static inline long unsigned int squareroot(
long unsigned int x) {
return (
long unsigned int) std::sqrt((
double) x); }
394 static inline long unsigned int pow(
long unsigned int x,
long unsigned int y) {
return (
long unsigned int) std::pow((
double)x,(
double)y); }
397 static inline long unsigned int log(
long unsigned int x) {
return static_cast<long unsigned int> (std::log (static_cast<double> (x))); }
398 static inline long unsigned int log10(
long unsigned int x) {
return static_cast<long unsigned int> (std::log10 (static_cast<double> (x))); }
402 #ifdef HAVE_TEUCHOS_LONG_LONG_INT 404 struct ScalarTraits<long long int>
406 typedef long long int magnitudeType;
407 typedef long long int halfPrecision;
408 typedef long long int doublePrecision;
409 static const bool isComplex =
false;
410 static const bool isOrdinal =
true;
411 static const bool isComparable =
true;
412 static const bool hasMachineParameters =
false;
414 static inline magnitudeType magnitude(
long long int a) {
return static_cast<long long int>(std::fabs(static_cast<double>(a))); }
415 static inline long long int zero() {
return 0; }
416 static inline long long int one() {
return 1; }
417 static inline long long int conjugate(
long long int x) {
return x; }
418 static inline long long int real(
long long int x) {
return x; }
419 static inline long long int imag(
long long int) {
return 0; }
420 static inline bool isnaninf(
long long int) {
return false; }
421 static inline void seedrandom(
unsigned int s) {
430 static inline long long int random() {
return std::rand(); }
431 static inline std::string name() {
return "long long int"; }
432 static inline long long int squareroot(
long long int x) {
return (
long long int) std::sqrt((
double) x); }
433 static inline long long int pow(
long long int x,
long long int y) {
return (
long long int) std::pow((
double)x,(
double)y); }
436 static inline long long int log(
long long int x) {
return static_cast<long long int> (std::log (static_cast<double> (x))); }
437 static inline long long int log10(
long long int x) {
return static_cast<long long int> (std::log10 (static_cast<double> (x))); }
441 struct ScalarTraits<unsigned long long int>
443 typedef unsigned long long int magnitudeType;
444 typedef unsigned long long int halfPrecision;
445 typedef unsigned long long int doublePrecision;
446 static const bool isComplex =
false;
447 static const bool isOrdinal =
true;
448 static const bool isComparable =
true;
449 static const bool hasMachineParameters =
false;
451 static inline magnitudeType magnitude(
unsigned long long int a) {
return static_cast<unsigned long long int>(std::fabs(static_cast<double>(a))); }
452 static inline unsigned long long int zero() {
return 0; }
453 static inline unsigned long long int one() {
return 1; }
454 static inline unsigned long long int conjugate(
unsigned long long int x) {
return x; }
455 static inline unsigned long long int real(
unsigned long long int x) {
return x; }
456 static inline unsigned long long int imag(
unsigned long long int) {
return 0; }
457 static inline bool isnaninf(
unsigned long long int) {
return false; }
458 static inline void seedrandom(
unsigned int s) {
467 static inline unsigned long long int random() {
return std::rand(); }
468 static inline std::string name() {
return "unsigned long long int"; }
469 static inline unsigned long long int squareroot(
unsigned long long int x) {
return (
unsigned long long int) std::sqrt((
double) x); }
470 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); }
473 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))); }
474 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))); }
476 #endif // HAVE_TEUCHOS_LONG_LONG_INT 479 #ifdef HAVE_TEUCHOS___INT64 482 struct ScalarTraits<__int64>
484 typedef __int64 magnitudeType;
485 typedef __int64 halfPrecision;
486 typedef __int64 doublePrecision;
487 static const bool isComplex =
false;
488 static const bool isOrdinal =
true;
489 static const bool isComparable =
true;
490 static const bool hasMachineParameters =
false;
492 static inline magnitudeType magnitude(__int64 a) {
return static_cast<__int64
>(std::fabs(static_cast<double>(a))); }
493 static inline __int64 zero() {
return 0; }
494 static inline __int64 one() {
return 1; }
495 static inline __int64 conjugate(__int64 x) {
return x; }
496 static inline __int64 real(__int64 x) {
return x; }
497 static inline __int64 imag(__int64) {
return 0; }
498 static inline void seedrandom(
unsigned int s) {
507 static inline __int64 random() {
return std::rand(); }
508 static inline std::string name() {
return "__int64"; }
509 static inline __int64 squareroot(__int64 x) {
return (__int64) std::sqrt((
double) x); }
510 static inline __int64 pow(__int64 x, __int64 y) {
return (__int64) std::pow((
double)x,(
double)y); }
513 static inline __int64 log(__int64 x) {
return static_cast<__int64
> (std::log (static_cast<double> (x))); }
514 static inline __int64 log10(__int64 x) {
return static_cast<__int64
> (std::log10 (static_cast<double> (x))); }
518 struct ScalarTraits<unsigned __int64>
520 typedef unsigned __int64 magnitudeType;
521 typedef unsigned __int64 halfPrecision;
522 typedef unsigned __int64 doublePrecision;
523 static const bool isComplex =
false;
524 static const bool isOrdinal =
true;
525 static const bool isComparable =
true;
526 static const bool hasMachineParameters =
false;
528 static inline magnitudeType magnitude(
unsigned __int64 a) {
return static_cast<unsigned __int64
>(std::fabs(static_cast<double>(a))); }
529 static inline unsigned __int64 zero() {
return 0; }
530 static inline unsigned __int64 one() {
return 1; }
531 static inline unsigned __int64 conjugate(
unsigned __int64 x) {
return x; }
532 static inline unsigned __int64 real(
unsigned __int64 x) {
return x; }
533 static inline unsigned __int64 imag(
unsigned __int64) {
return 0; }
534 static inline void seedrandom(
unsigned int s) {
543 static inline unsigned __int64 random() {
return std::rand(); }
544 static inline std::string name() {
return "unsigned __int64"; }
545 static inline unsigned __int64 squareroot(
unsigned __int64 x) {
return (
unsigned __int64) std::sqrt((
double) x); }
546 static inline unsigned __int64 pow(
unsigned __int64 x,
unsigned __int64 y) {
return (
unsigned __int64) std::pow((
double)x,(
double)y); }
549 static inline unsigned __int64 log(
unsigned __int64 x) {
return static_cast<unsigned __int64
> (std::log (static_cast<double> (x))); }
550 static inline unsigned __int64 log10(
unsigned __int64 x) {
return static_cast<unsigned __int64
> (std::log10 (static_cast<double> (x))); }
553 #endif // HAVE_TEUCHOS___INT64 557 extern TEUCHOSCORE_LIB_DLL_EXPORT
const float flt_nan;
562 struct ScalarTraits<float>
564 typedef float magnitudeType;
565 typedef float halfPrecision;
566 typedef double doublePrecision;
567 static const bool isComplex =
false;
568 static const bool isOrdinal =
false;
569 static const bool isComparable =
true;
570 static const bool hasMachineParameters =
true;
571 static inline float eps() {
572 return std::numeric_limits<float>::epsilon();
574 static inline float sfmin() {
575 return std::numeric_limits<float>::min();
577 static inline float base() {
578 return static_cast<float>(std::numeric_limits<float>::radix);
580 static inline float prec() {
583 static inline float t() {
584 return static_cast<float>(std::numeric_limits<float>::digits);
586 static inline float rnd() {
587 return ( std::numeric_limits<float>::round_style == std::round_to_nearest ? one() : zero() );
589 static inline float emin() {
590 return static_cast<float>(std::numeric_limits<float>::min_exponent);
592 static inline float rmin() {
593 return std::numeric_limits<float>::min();
595 static inline float emax() {
596 return static_cast<float>(std::numeric_limits<float>::max_exponent);
598 static inline float rmax() {
599 return std::numeric_limits<float>::max();
601 static inline magnitudeType magnitude(
float a)
604 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
605 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
609 static inline float zero() {
return(0.0f); }
610 static inline float one() {
return(1.0f); }
611 static inline float conjugate(
float x) {
return(x); }
612 static inline float real(
float x) {
return x; }
613 static inline float imag(
float) {
return zero(); }
614 static inline float nan() {
616 return 0.0f/std::sin(0.0f);
621 static inline bool isnaninf(
float x) {
622 return generic_real_isnaninf<float>(x);
624 static inline void seedrandom(
unsigned int s) {
632 static inline float random() {
float rnd = (float) std::rand() / RAND_MAX;
return (-1.0f + 2.0f * rnd); }
633 static inline std::string name() {
return "float"; }
634 static inline float squareroot(
float x)
637 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
638 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
641 const float rtn = std::sqrt(x);
646 static inline float pow(
float x,
float y) {
return std::pow(x,y); }
647 static inline float log(
float x) {
return std::log(x); }
648 static inline float log10(
float x) {
return std::log10(x); }
653 extern TEUCHOSCORE_LIB_DLL_EXPORT
const double dbl_nan;
658 struct ScalarTraits<double>
660 typedef double magnitudeType;
661 typedef float halfPrecision;
671 #if defined(HAVE_TEUCHOS_DOUBLE_TO_QD) 672 typedef dd_real doublePrecision;
673 #elif defined(HAVE_TEUCHOS_DOUBLE_TO_ARPREC) 674 typedef mp_real doublePrecision;
676 typedef double doublePrecision;
678 static const bool isComplex =
false;
679 static const bool isOrdinal =
false;
680 static const bool isComparable =
true;
681 static const bool hasMachineParameters =
true;
682 static inline double eps() {
683 return std::numeric_limits<double>::epsilon();
685 static inline double sfmin() {
686 return std::numeric_limits<double>::min();
688 static inline double base() {
689 return std::numeric_limits<double>::radix;
691 static inline double prec() {
694 static inline double t() {
695 return std::numeric_limits<double>::digits;
697 static inline double rnd() {
698 return ( std::numeric_limits<double>::round_style == std::round_to_nearest ?
double(1.0) :
double(0.0) );
700 static inline double emin() {
701 return std::numeric_limits<double>::min_exponent;
703 static inline double rmin() {
704 return std::numeric_limits<double>::min();
706 static inline double emax() {
707 return std::numeric_limits<double>::max_exponent;
709 static inline double rmax() {
710 return std::numeric_limits<double>::max();
712 static inline magnitudeType magnitude(
double a)
715 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
716 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
720 static inline double zero() {
return 0.0; }
721 static inline double one() {
return 1.0; }
722 static inline double conjugate(
double x) {
return(x); }
723 static inline double real(
double x) {
return(x); }
724 static inline double imag(
double) {
return(0); }
725 static inline double nan() {
727 return 0.0/std::sin(0.0);
732 static inline bool isnaninf(
double x) {
733 return generic_real_isnaninf<double>(x);
735 static inline void seedrandom(
unsigned int s) {
743 static inline double random() {
double rnd = (double) std::rand() / RAND_MAX;
return (
double)(-1.0 + 2.0 * rnd); }
744 static inline std::string name() {
return "double"; }
745 static inline double squareroot(
double x)
748 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
749 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
752 const double rtn = std::sqrt(x);
757 static inline double pow(
double x,
double y) {
return std::pow(x,y); }
758 static inline double log(
double x) {
return std::log(x); }
759 static inline double log10(
double x) {
return std::log10(x); }
763 #ifdef HAVE_TEUCHOSCORE_QUADMATH 766 struct ScalarTraits<__float128> {
767 typedef __float128 magnitudeType;
771 typedef __float128 doublePrecision;
772 typedef double halfPrecision;
774 static const bool isComplex =
false;
775 static const bool isOrdinal =
false;
776 static const bool isComparable =
true;
777 static const bool hasMachineParameters =
true;
779 static __float128 eps () {
780 return FLT128_EPSILON;
782 static __float128 sfmin () {
785 static __float128 base () {
788 static __float128 prec () {
789 return eps () * base ();
791 static __float128 t () {
792 return FLT128_MANT_DIG;
794 static __float128 rnd () {
797 static __float128 emin () {
798 return FLT128_MIN_EXP;
800 static __float128 rmin () {
803 static __float128 emax () {
804 return FLT128_MAX_EXP;
806 static __float128 rmax () {
809 static magnitudeType magnitude (
const __float128& x) {
812 static __float128 zero () {
815 static __float128 one () {
818 static __float128 conjugate (
const __float128& x) {
821 static __float128 real (
const __float128& x) {
824 static __float128 imag (
const __float128& ) {
827 static __float128 nan () {
828 return strtoflt128 (
"NAN()", NULL);
830 static bool isnaninf (
const __float128& x) {
831 return isinfq (x) || isnanq (x);
833 static inline void seedrandom (
unsigned int s) {
841 static __float128 random () {
844 const __float128 scalingFactor =
845 static_cast<__float128
> (std::numeric_limits<double>::min ()) /
846 static_cast<__float128> (2.0);
847 const __float128 higherOrderTerm =
848 static_cast<__float128
> (ScalarTraits<double>::random ());
849 const __float128 lowerOrderTerm =
850 static_cast<__float128
> (ScalarTraits<double>::random ()) *
852 return higherOrderTerm + lowerOrderTerm;
854 static std::string name () {
857 static __float128 squareroot (
const __float128& x) {
860 static __float128 pow (
const __float128& x,
const __float128& y) {
863 static __float128 log (
const __float128& x) {
866 static __float128 log10 (
const __float128& x) {
870 #endif // HAVE_TEUCHOSCORE_QUADMATH 874 #ifdef HAVE_TEUCHOS_QD 876 bool operator&&(
const dd_real &a,
const dd_real &b);
877 bool operator&&(
const qd_real &a,
const qd_real &b);
880 struct ScalarTraits<dd_real>
882 typedef dd_real magnitudeType;
883 typedef double halfPrecision;
884 typedef qd_real doublePrecision;
885 static const bool isComplex =
false;
886 static const bool isOrdinal =
false;
887 static const bool isComparable =
true;
888 static const bool hasMachineParameters =
true;
889 static inline dd_real eps() {
return std::numeric_limits<dd_real>::epsilon(); }
890 static inline dd_real sfmin() {
return std::numeric_limits<dd_real>::min(); }
891 static inline dd_real base() {
return std::numeric_limits<dd_real>::radix; }
892 static inline dd_real prec() {
return eps()*base(); }
893 static inline dd_real t() {
return std::numeric_limits<dd_real>::digits; }
894 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) ); }
895 static inline dd_real emin() {
return std::numeric_limits<dd_real>::min_exponent; }
896 static inline dd_real rmin() {
return std::numeric_limits<dd_real>::min(); }
897 static inline dd_real emax() {
return std::numeric_limits<dd_real>::max_exponent; }
898 static inline dd_real rmax() {
return std::numeric_limits<dd_real>::max(); }
899 static inline magnitudeType magnitude(dd_real a)
902 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
903 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
907 static inline dd_real zero() {
return dd_real(0.0); }
908 static inline dd_real one() {
return dd_real(1.0); }
909 static inline dd_real conjugate(dd_real x) {
return(x); }
910 static inline dd_real real(dd_real x) {
return x ; }
911 static inline dd_real imag(dd_real) {
return zero(); }
912 static inline dd_real nan() {
return dd_real::_nan; }
913 static inline bool isnaninf(dd_real x) {
return isnan(x) || isinf(x); }
914 static inline void seedrandom(
unsigned int s) {
923 static inline dd_real random() {
return ddrand(); }
924 static inline std::string name() {
return "dd_real"; }
925 static inline dd_real squareroot(dd_real x)
928 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
929 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
933 static inline dd_real pow(dd_real x, dd_real y) { return ::pow(x,y); }
935 static inline dd_real log(dd_real x) { return ::log(x); }
936 static inline dd_real log10(dd_real x) { return ::log10(x); }
941 struct ScalarTraits<qd_real>
943 typedef qd_real magnitudeType;
944 typedef dd_real halfPrecision;
945 typedef qd_real doublePrecision;
946 static const bool isComplex =
false;
947 static const bool isOrdinal =
false;
948 static const bool isComparable =
true;
949 static const bool hasMachineParameters =
true;
950 static inline qd_real eps() {
return std::numeric_limits<qd_real>::epsilon(); }
951 static inline qd_real sfmin() {
return std::numeric_limits<qd_real>::min(); }
952 static inline qd_real base() {
return std::numeric_limits<qd_real>::radix; }
953 static inline qd_real prec() {
return eps()*base(); }
954 static inline qd_real t() {
return std::numeric_limits<qd_real>::digits; }
955 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) ); }
956 static inline qd_real emin() {
return std::numeric_limits<qd_real>::min_exponent; }
957 static inline qd_real rmin() {
return std::numeric_limits<qd_real>::min(); }
958 static inline qd_real emax() {
return std::numeric_limits<qd_real>::max_exponent; }
959 static inline qd_real rmax() {
return std::numeric_limits<qd_real>::max(); }
960 static inline magnitudeType magnitude(qd_real a)
963 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
964 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
968 static inline qd_real zero() {
return qd_real(0.0); }
969 static inline qd_real one() {
return qd_real(1.0); }
970 static inline qd_real conjugate(qd_real x) {
return(x); }
971 static inline qd_real real(qd_real x) {
return x ; }
972 static inline qd_real imag(qd_real) {
return zero(); }
973 static inline qd_real nan() {
return qd_real::_nan; }
974 static inline bool isnaninf(qd_real x) {
return isnan(x) || isinf(x); }
975 static inline void seedrandom(
unsigned int s) {
984 static inline qd_real random() {
return qdrand(); }
985 static inline std::string name() {
return "qd_real"; }
986 static inline qd_real squareroot(qd_real x)
989 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
990 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
994 static inline qd_real pow(qd_real x, qd_real y) { return ::pow(x,y); }
996 static inline qd_real log(qd_real x) { return ::log(x); }
997 static inline qd_real log10(qd_real x) { return ::log10(x); }
1001 #endif // HAVE_TEUCHOS_QD 1004 #ifdef HAVE_TEUCHOS_GNU_MP 1007 extern gmp_randclass gmp_rng;
1027 struct ScalarTraits<mpf_class>
1029 typedef mpf_class magnitudeType;
1030 typedef mpf_class halfPrecision;
1031 typedef mpf_class doublePrecision;
1032 static const bool isComplex =
false;
1033 static const bool hasMachineParameters =
false;
1035 static magnitudeType magnitude(mpf_class a) {
return std::abs(a); }
1036 static inline mpf_class zero() { mpf_class zero = 0.0;
return zero; }
1037 static inline mpf_class one() { mpf_class one = 1.0;
return one; }
1038 static inline mpf_class conjugate(mpf_class x) {
return x; }
1039 static inline mpf_class real(mpf_class x) {
return(x); }
1040 static inline mpf_class imag(mpf_class x) {
return(0); }
1041 static inline bool isnaninf(mpf_class x) {
return false; }
1042 static inline void seedrandom(
unsigned int s) {
1043 unsigned long int seedVal =
static_cast<unsigned long int>(s);
1044 gmp_rng.seed( seedVal );
1046 static inline mpf_class random() {
1047 return gmp_rng.get_f();
1049 static inline std::string name() {
return "mpf_class"; }
1050 static inline mpf_class squareroot(mpf_class x) {
return std::sqrt(x); }
1051 static inline mpf_class pow(mpf_class x, mpf_class y) {
return pow(x,y); }
1055 #endif // HAVE_TEUCHOS_GNU_MP 1057 #ifdef HAVE_TEUCHOS_ARPREC 1062 struct ScalarTraits<mp_real>
1064 typedef mp_real magnitudeType;
1065 typedef double halfPrecision;
1066 typedef mp_real doublePrecision;
1067 static const bool isComplex =
false;
1068 static const bool isComparable =
true;
1069 static const bool isOrdinal =
false;
1070 static const bool hasMachineParameters =
false;
1072 static magnitudeType magnitude(mp_real a) {
return abs(a); }
1073 static inline mp_real zero() { mp_real zero = 0.0;
return zero; }
1074 static inline mp_real one() { mp_real one = 1.0;
return one; }
1075 static inline mp_real conjugate(mp_real x) {
return x; }
1076 static inline mp_real real(mp_real x) {
return(x); }
1077 static inline mp_real imag(mp_real x) {
return zero(); }
1078 static inline bool isnaninf(mp_real x) {
return false; }
1079 static inline void seedrandom(
unsigned int s) {
1080 long int seedVal =
static_cast<long int>(s);
1083 static inline mp_real random() {
return mp_rand(); }
1084 static inline std::string name() {
return "mp_real"; }
1085 static inline mp_real squareroot(mp_real x) {
return sqrt(x); }
1086 static inline mp_real pow(mp_real x, mp_real y) {
return pow(x,y); }
1091 #endif // HAVE_TEUCHOS_ARPREC 1094 #ifdef HAVE_TEUCHOS_COMPLEX 1099 struct ScalarTraits<
1103 typedef std::complex<T> ComplexT;
1104 typedef std::complex<typename ScalarTraits<T>::halfPrecision> halfPrecision;
1105 typedef std::complex<typename ScalarTraits<T>::doublePrecision> doublePrecision;
1106 typedef typename ScalarTraits<T>::magnitudeType magnitudeType;
1107 static const bool isComplex =
true;
1108 static const bool isOrdinal = ScalarTraits<T>::isOrdinal;
1109 static const bool isComparable =
false;
1110 static const bool hasMachineParameters =
true;
1111 static inline magnitudeType eps() {
return ScalarTraits<magnitudeType>::eps(); }
1112 static inline magnitudeType sfmin() {
return ScalarTraits<magnitudeType>::sfmin(); }
1113 static inline magnitudeType base() {
return ScalarTraits<magnitudeType>::base(); }
1114 static inline magnitudeType prec() {
return ScalarTraits<magnitudeType>::prec(); }
1115 static inline magnitudeType t() {
return ScalarTraits<magnitudeType>::t(); }
1116 static inline magnitudeType rnd() {
return ScalarTraits<magnitudeType>::rnd(); }
1117 static inline magnitudeType emin() {
return ScalarTraits<magnitudeType>::emin(); }
1118 static inline magnitudeType rmin() {
return ScalarTraits<magnitudeType>::rmin(); }
1119 static inline magnitudeType emax() {
return ScalarTraits<magnitudeType>::emax(); }
1120 static inline magnitudeType rmax() {
return ScalarTraits<magnitudeType>::rmax(); }
1121 static magnitudeType magnitude(ComplexT a)
1123 #ifdef TEUCHOS_DEBUG 1124 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
1125 a,
"Error, the input value to magnitude(...) a = " << a <<
" can not be NaN!" );
1129 static inline ComplexT zero() {
return ComplexT(ScalarTraits<magnitudeType>::zero(),ScalarTraits<magnitudeType>::zero()); }
1130 static inline ComplexT one() {
return ComplexT(ScalarTraits<magnitudeType>::one(),ScalarTraits<magnitudeType>::zero()); }
1131 static inline ComplexT conjugate(ComplexT a){
return ComplexT(a.real(),-a.imag()); }
1132 static inline magnitudeType real(ComplexT a) {
return a.real(); }
1133 static inline magnitudeType imag(ComplexT a) {
return a.imag(); }
1134 static inline ComplexT nan() {
return ComplexT(ScalarTraits<magnitudeType>::nan(),ScalarTraits<magnitudeType>::nan()); }
1135 static inline bool isnaninf(ComplexT x) {
return ScalarTraits<magnitudeType>::isnaninf(x.real()) || ScalarTraits<magnitudeType>::isnaninf(x.imag()); }
1136 static inline void seedrandom(
unsigned int s) { ScalarTraits<magnitudeType>::seedrandom(s); }
1137 static inline ComplexT random()
1139 const T rnd1 = ScalarTraits<magnitudeType>::random();
1140 const T rnd2 = ScalarTraits<magnitudeType>::random();
1141 return ComplexT(rnd1,rnd2);
1143 static inline std::string name() {
return std::string(
"std::complex<")+std::string(ScalarTraits<magnitudeType>::name())+std::string(
">"); }
1145 static inline ComplexT squareroot(ComplexT x)
1147 #ifdef TEUCHOS_DEBUG 1148 TEUCHOS_SCALAR_TRAITS_NAN_INF_ERR(
1149 x,
"Error, the input value to squareroot(...) x = " << x <<
" can not be NaN!" );
1151 typedef ScalarTraits<magnitudeType> STMT;
1152 const T r = x.real(), i = x.imag(), zero = STMT::zero(), two = 2.0;
1153 const T a = STMT::squareroot((r*r)+(i*i));
1154 const T nr = STMT::squareroot((a+r)/two);
1155 const T ni = ( i == zero ? zero : STMT::squareroot((a-r)/two) );
1156 return ComplexT(nr,ni);
1170 static inline ComplexT pow(ComplexT x, ComplexT y) {
return pow(x,y); }
1173 #endif // HAVE_TEUCHOS_COMPLEX 1174 #endif // DOXYGEN_SHOULD_SKIP_THIS 1178 #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.