Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_SetScientific.hpp
1// @HEADER
2// ***********************************************************************
3//
4// Tpetra: Templated Linear Algebra Services Package
5// Copyright (2008) Sandia Corporation
6//
7// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8// the U.S. Government retains certain rights in this software.
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#ifndef TEUCHOS_SET_SCIENTIFIC_HPP
43#define TEUCHOS_SET_SCIENTIFIC_HPP
44
45#include <Teuchos_as.hpp>
47#include <string>
48#include <ios>
49
50namespace Teuchos {
51
72template<typename Scalar, const bool isFloatingPoint = ! Teuchos::ScalarTraits<Scalar>::isOrdinal>
74
75
76// Partial specialization of SetScientific for floating-point types.
77//
78// This class currently requires that std::log10() take
79// arguments of type Scalar. This may be relaxed in the future
80// if Teuchos::ScalarTraits gets its own log10() method.
81template<typename Scalar>
82class SetScientific<Scalar, true> {
83 public:
84 typedef Scalar scalar_type;
85
86 SetScientific(std::ostream& out, int prec = -1):
87 out_(out),
88 originalFlags_(out.flags()),
89 originalPrecision_(out.precision())
90 {
91 // Print floating-point values in scientific notation.
92 out << std::scientific;
93
95
96 // Set the number of (decimal) digits after the decimal
97 // point to print.
98 out.precision(static_cast<std::streamsize>(prec));
99 }
100
101 static inline int getDefaultPrecision() {
103 typedef typename STS::magnitudeType magnitude_type;
105
106 // We're writing decimal digits, so compute the number of
107 // digits we need to get reasonable accuracy when reading
108 // values back in.
109 //
110 // There is actually an algorithm, due to Guy Steele (yes,
111 // Java's Guy Steele) et al., for idempotent printing of
112 // finite-length floating-point values. We should actually
113 // implement that algorithm, but I don't have time for that
114 // now. Currently, I just print no more than (one decimal
115 // digit more than (the number of decimal digits justified
116 // by the precision of magnitude_type)).
117 //
118 // We need to use STM's log10() rather than (say) std::log10
119 // here, because STM::base() returns a magnitude_type, not
120 // one of C++'s standard integer types.
121 const magnitude_type numDecDigits = STM::t() * STM::log10 (STM::base());
122
123 // Round and add one. The cast to int should not overflow
124 // unless STM::t() is _extremely_ large, so we don't need to
125 // check for that case here.
126 const magnitude_type one = STM::one();
127 const magnitude_type two = one + one;
128 // Cast from magnitude_type to int, since std::ostream's
129 // precision() method expects an int input.
130 const int prec = 1 +
131 Teuchos::as<int>(magnitude_type((two*numDecDigits + one) / two));
132 return prec;
133 }
134
135 ~SetScientific () {
136 out_.flags (originalFlags_);
137 }
138
139 private:
141 std::ostream& out_;
142
144 std::ios_base::fmtflags originalFlags_;
145
147 std::streamsize originalPrecision_;
148};
149
151template<class Scalar>
152class SetScientific<Scalar, false> {
153 public:
154 typedef Scalar scalar_type;
155 SetScientific(std::ostream&) {}
156 ~SetScientific() {}
157};
158
159} // namespace Teuchos
160
161#endif // TEUCHOS_SET_SCIENTIFIC_HPP
Defines basic traits for the scalar field type.
Definition of Teuchos::as, for conversions between types.
Temporarily make an output stream use scientific notation with sufficient precision.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
This structure defines some basic traits for a scalar field type.