Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
float128.cpp
Go to the documentation of this file.
1// @HEADER
2// ***********************************************************************
3//
4// Teuchos: Common Tools Package
5// Copyright (2004) Sandia Corporation
6//
7// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8// license for use of this work by or on behalf of the U.S. Government.
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions are
12// met:
13//
14// 1. Redistributions of source code must retain the above copyright
15// notice, this list of conditions and the following disclaimer.
16//
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// 3. Neither the name of the Corporation nor the names of the
22// contributors may be used to endorse or promote products derived from
23// this software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36//
37// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38//
39// ***********************************************************************
40// @HEADER
41
42#include "Teuchos_Array.hpp"
43#include "Teuchos_ScalarTraits.hpp" // operator<< and operator>> overloads
44#include "Teuchos_Tuple.hpp"
46
47namespace { // (anonymous)
48
49using std::endl;
50
51TEUCHOS_UNIT_TEST( Float128, OutputStreamOp )
52{
53 Teuchos::OSTab tab0 (out);
54
55#ifdef HAVE_TEUCHOSCORE_QUADMATH
56 out << "Test operator<< (std::ostream&, __float128)" << endl;
57
58 __float128 x = 1.0;
59 __float128 y = strtoflt128 ("1.111112222233333", NULL);
60 // __float128 has enough digits to represent this exactly, but
61 // double precision would round.
62 __float128 z = strtoflt128 ("1.111112222233333444445555566666", NULL);
63
64 // FIXME (mfh 04 Sep 2015) The results of printing could depend on
65 // the locale. This works fine for the default locale on my system.
66 {
67 std::ostringstream os;
68 os << x;
69 TEST_EQUALITY_CONST( os.str (), "1.000000000000000000000000000000e+00" );
70 }
71 {
72 std::ostringstream os;
73 os << y;
74 TEST_EQUALITY_CONST( os.str (), "1.111112222233333000000000000000e+00" );
75 }
76 {
77 std::ostringstream os;
78 os << z;
79 TEST_EQUALITY_CONST( os.str (), "1.111112222233333444445555566666e+00" );
80 }
81
82 // Test that operator<< (std::ostream&, __float128) works. The
83 // operator<< overload MUST be defined in the std namespace. If
84 // defined in the global namespace, the compiler will have trouble
85 // with the TEST_COMPARE_ARRAYS expression below.
86
87 out << "Test chaining operator<<:" << endl
88 << "z = " << z << ", it really does." << endl;
89
90 // Test op<<, but make the __float128 arguments entries of an array.
91 // (Make sure that this compiles without "ambiguous overload"
92 // errors.)
93 __float128 a[3];
94 a[0] = x;
95 a[1] = y;
96 a[2] = z;
97 out << "Testing chaining operator<< with array entries:" << endl
98 << "a[0] = " << a[0] << ", a[1] = " << a[1] << ", a[2] = " << a[2]
99 << endl;
100
101 Teuchos::Array<__float128> arrayOfFloat128 (1);
102 Teuchos::Tuple<__float128, 1> tupleOfFloat128;
103 arrayOfFloat128[0] = z;
104 tupleOfFloat128[0] = z;
105 Teuchos::ArrayView<__float128> arrayViewOfFloat128 = arrayOfFloat128 ();
106 // mfh 04 Sep 2015: If operator<< (std::ostream&, __float128) is
107 // defined in the global namespace, instead of in the std namespace,
108 // the TEST_COMPARE_ARRAYS expression will fail to compile. GCC
109 // 5.2.0 complains about the following line of
110 // Teuchos::compareArrays (in
111 // teuchos/core/src/Teuchos_TestingHelpers.hpp):
112 //
113 // out << "\nError, "<<a1_name<<"["<<i<<"] = "<<a1[i]<<" == "
114 // << a2_name<<"["<<i<<"] = "<<a2[i]<<": failed!\n";
115 //
116 // where a1 is a Teuchos::ArrayView<__float128> and a2 is a
117 // Teuchos::Tuple<__float128, 1>. The compiler claims an ambiguous
118 // overload. It has something to do with compareArrays (and
119 // analogous functions in that file) being templated on Array types,
120 // as I don't see that when I directly imitate what that line of
121 // code does (see below).
122
123 TEST_COMPARE_ARRAYS( arrayViewOfFloat128, tupleOfFloat128 );
124
125 std::string s1 ("Some string");
126 out << "Hello there! \"" << s1 << "\" is a string that doesn't mean anything "
127 "on its own, but just makes this line of code more like the line of code "
128 "that doesn't compile. arrayViewOfFloat128[0] = " << arrayViewOfFloat128[0]
129 << " and tupleOfFloat128[0] = " << tupleOfFloat128[0] << "." << endl;
130
131 // Test that operator>> (std::istream&, __float128&) works.
132 {
133 // Use enough digits in the example to catch "cheats" that
134 // truncate to double.
135 const std::string z_str ("1.111112222233333444445555566666");
136 std::istringstream is (z_str);
137 __float128 z_copy = 0.0;
138 is >> z_copy;
139 TEST_EQUALITY_CONST( z, z_copy );
140 }
141
142#else
143 out << "This test only makes sense to run with libquadmath enabled." << endl;
144#endif // HAVE_TEUCHOSCORE_QUADMATH
145}
146
147} // namespace (anonymous)
Templated array class derived from the STL std::vector.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
#define TEST_COMPARE_ARRAYS(a1, a2)
Assert that a1.size()==a2.size() and a[i]==b[i], i=0....
Defines basic traits for the scalar field type.
Unit testing support.
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
Concrete serial communicator subclass.