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 // @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 
12 #include "Teuchos_PrintDouble.hpp"
13 
14 #include <cstdlib>
15 #include <sstream>
16 #include <cmath>
17 
18 namespace {
19 
20 void test_print_double(double v, const char* s, bool& success, std::ostream& out) {
21  std::stringstream ss;
22  Teuchos::print_double(ss, v);
23  auto s2 = ss.str();
24  TEST_EQUALITY_CONST( s2, s );
25  if (std::isnan(v)) {
26  // NaN != NaN, special case
27  TEST_EQUALITY_CONST( true, std::isnan(std::atof(s)) );
28  } else {
29  TEST_EQUALITY_CONST( v, std::atof(s) );
30  }
31 }
32 
33 void test_print_double(const char* s, bool& success, std::ostream& out) {
34  test_print_double(std::atof(s), s, success, out);
35 }
36 
37 double form_double(bool negative, std::uint64_t mantissa, std::int32_t exponent) {
38  std::uint64_t pun = 0;
39  if (negative) pun |= std::uint64_t(1) << 63;
40  auto biased_exponent = std::uint64_t(exponent) + 1075;
41  pun |= biased_exponent << 52;
42  pun |= mantissa % (std::uint64_t(1) << 52);
43  double value;
44  std::memcpy(&value, &pun, sizeof(value));
45  return value;
46 }
47 
48 TEUCHOS_UNIT_TEST( PrintDouble, Basic )
49 {
50  test_print_double("1.", success, out);
51  test_print_double("-1.", success, out);
52  test_print_double("0.", success, out);
53  test_print_double("4.", success, out);
54  test_print_double("55.", success, out);
55  test_print_double("3.14159", success, out);
56  test_print_double("5404319552844595.", success, out);
57  test_print_double("1e300", success, out);
58  test_print_double("1e-300", success, out);
59  test_print_double("1e2", success, out);
60  test_print_double("10.", success, out);
61  test_print_double(".003", success, out);
62  /* in order to ensure this gets converted back to the right
63  value even if people change their IEEE rounding mode,
64  this long representation is needed */
65  test_print_double("9.999999999999999e22", success, out);
66  test_print_double("inf", success, out);
67  test_print_double("-inf", success, out);
68  test_print_double("nan", success, out);
69  // explicitly form 2^60 to test rare if case in print_double
70  test_print_double(form_double(0, 0, 8), "1152921504606847000.", success, out);
71  test_print_double(form_double(0, 0, 0), "4503599627370496.", success, out);
72  test_print_double(form_double(0, 4503599627370503, -30), "4194304.0000000065", success, out);
73  test_print_double(form_double(0, 4503599627370518, -30), "4194304.0000000205", success, out);
74  // tests from Steele and White's paper
75  test_print_double("1.3", success, out);
76  test_print_double(4.0/3.0, "1.3333333333333333", success, out);
77  test_print_double(1.0/10.0, ".1", success, out);
78 }
79 
80 } // 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.