Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PrintDouble_UnitTests.cpp
Go to the documentation of this file.
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Teuchos: Common Tools Package
6 // Copyright (2004) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
45 
46 #include "Teuchos_PrintDouble.hpp"
47 
48 #include <cstdlib>
49 #include <sstream>
50 #include <cmath>
51 
52 namespace {
53 
54 void test_print_double(double v, const char* s, bool& success, std::ostream& out) {
55  std::stringstream ss;
56  Teuchos::print_double(ss, v);
57  auto s2 = ss.str();
58  TEST_EQUALITY_CONST( s2, s );
59  if (std::isnan(v)) {
60  // NaN != NaN, special case
61  TEST_EQUALITY_CONST( true, std::isnan(std::atof(s)) );
62  } else {
63  TEST_EQUALITY_CONST( v, std::atof(s) );
64  }
65 }
66 
67 void test_print_double(const char* s, bool& success, std::ostream& out) {
68  test_print_double(std::atof(s), s, success, out);
69 }
70 
71 double form_double(bool negative, std::uint64_t mantissa, std::int32_t exponent) {
72  std::uint64_t pun = 0;
73  if (negative) pun |= std::uint64_t(1) << 63;
74  auto biased_exponent = std::uint64_t(exponent) + 1075;
75  pun |= biased_exponent << 52;
76  pun |= mantissa % (std::uint64_t(1) << 52);
77  double value;
78  std::memcpy(&value, &pun, sizeof(value));
79  return value;
80 }
81 
82 TEUCHOS_UNIT_TEST( PrintDouble, Basic )
83 {
84  test_print_double("1.", success, out);
85  test_print_double("-1.", success, out);
86  test_print_double("0.", success, out);
87  test_print_double("4.", success, out);
88  test_print_double("55.", success, out);
89  test_print_double("3.14159", success, out);
90  test_print_double("5404319552844595.", success, out);
91  test_print_double("1e300", success, out);
92  test_print_double("1e-300", success, out);
93  test_print_double("1e2", success, out);
94  test_print_double("10.", success, out);
95  test_print_double(".003", success, out);
96  /* in order to ensure this gets converted back to the right
97  value even if people change their IEEE rounding mode,
98  this long representation is needed */
99  test_print_double("9.999999999999999e22", success, out);
100  test_print_double("inf", success, out);
101  test_print_double("-inf", success, out);
102  test_print_double("nan", success, out);
103  // explicitly form 2^60 to test rare if case in print_double
104  test_print_double(form_double(0, 0, 8), "1152921504606847000.", success, out);
105  test_print_double(form_double(0, 0, 0), "4503599627370496.", success, out);
106  test_print_double(form_double(0, 4503599627370503, -30), "4194304.0000000065", success, out);
107  test_print_double(form_double(0, 4503599627370518, -30), "4194304.0000000205", success, out);
108  // tests from Steele and White's paper
109  test_print_double("1.3", success, out);
110  test_print_double(4.0/3.0, "1.3333333333333333", success, out);
111  test_print_double(1.0/10.0, ".1", success, out);
112 }
113 
114 } // namespace
void print_double(std::ostream &os, double v)
Prints a double-precision floating-point number exactly using minimal characters. ...
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
Unit testing support.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
Declares Teuchos::print_double.