Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_ScalarTraits.cpp
1// @HEADER
2// *****************************************************************************
3// Teuchos: Common Tools Package
4//
5// Copyright 2004 NTESS and the Teuchos contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
11#include "Teuchos_Assert.hpp"
12#include <limits>
13
14// Define this to throw exceptions when any Teuchos::ScalarTraits function
15// encounters a NaN or an Inf.
16//#define TEUCHOS_SCALAR_TRAITS_THROW_NAN_INF_ERR
17
18#ifdef HAVE_TEUCHOSCORE_QUADMATH
19namespace std {
20
21std::ostream&
22operator<< (std::ostream& out, const __float128& x)
23{
24 const size_t bufSize = 128;
25 char buf[128];
26
27 const int numCharPrinted = quadmath_snprintf (buf, bufSize, "%.30Qe", x);
28 if (static_cast<size_t> (numCharPrinted) >= bufSize) {
29 std::ostringstream os;
30 os << "Failed to print __float128 value: buffer has " << bufSize
31 << " characters, but quadmath_snprintf wanted " << numCharPrinted
32 << " characters!";
33 throw std::runtime_error (os.str ());
34 }
35 out << buf;
36 return out;
37}
38
39istream&
40operator>> (std::istream& in, __float128& x)
41{
42 std::string tmpStr;
43 in >> tmpStr;
44 // FIXME (mfh 10 Sep 2015) I don't think this routine does any error
45 // checking.
46 x = strtoflt128 (tmpStr.c_str (), NULL);
47 return in;
48}
49
50} // namespace std
51#endif // HAVE_TEUCHOSCORE_QUADMATH
52
53void Teuchos::throwScalarTraitsNanInfError( const std::string &errMsg )
54{
55 (void)errMsg;
56#ifdef TEUCHOS_SCALAR_TRAITS_THROW_NAN_INF_ERR
57 TEUCHOS_TEST_FOR_EXCEPTION( true, std::runtime_error, errMsg );
58#endif
59}
60
61#ifdef HAVE_TEUCHOS_GNU_MP
62gmp_randclass Teuchos::gmp_rng ( gmp_randinit_default );
63#endif
64
65#ifdef HAVE_TEUCHOS_QD
66bool Teuchos::operator&&(const dd_real &a, const dd_real &b) {
67 return !a.is_zero() && !b.is_zero();
68}
69bool Teuchos::operator&&(const qd_real &a, const qd_real &b) {
70 return !a.is_zero() && !b.is_zero();
71}
72#endif
73
74#ifndef __sun
75// This is an intentional computation of NaN.
76namespace Teuchos {
77 const float flt_nan = std::numeric_limits<float>::quiet_NaN();
78 const double dbl_nan = std::numeric_limits<double>::quiet_NaN();
79}
80#endif
Defines basic traits for the scalar field type.
Smart reference counting pointer class for automatic garbage collection.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...