Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages
PrecisionWrapper.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2017.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg $
32 // $Authors: Marc Sturm, Clemens Groepl $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_CONCEPT_PRECISIONWRAPPER_H
36 #define OPENMS_CONCEPT_PRECISIONWRAPPER_H
37 
38 #include <OpenMS/config.h>
39 #include <OpenMS/CONCEPT/Types.h>
40 
41 #include <iostream>
42 #include <iomanip>
43 
44 namespace OpenMS
45 {
46 
50  template <typename FloatingPointType>
52  {
54  explicit PrecisionWrapper(const FloatingPointType rhs) :
55  ref_(rhs) {}
57  ref_(rhs.ref_) {}
58  FloatingPointType const ref_;
59 private:
60  PrecisionWrapper(); // intentionally not implemented
61  };
62 
96  template <typename FloatingPointType>
97  inline const PrecisionWrapper<FloatingPointType> precisionWrapper(const FloatingPointType rhs)
98  {
100  }
101 
103  template <typename FloatingPointType>
104  inline std::ostream & operator<<(std::ostream & os, const PrecisionWrapper<FloatingPointType> & rhs)
105  {
106  // Same test as used by isnan(), spelled out here to avoid issues during overload resolution.
107 #pragma clang diagnostic push
108 #pragma clang diagnostic ignored "-Wfloat-equal"
109  if (rhs.ref_ != rhs.ref_)
110 #pragma clang diagnostic pop
111  {
112  // That's what Linux GCC uses, and gnuplot understands.
113  // Windows would print stuff like 1.#QNAN which makes testing hard.
114  return os << "nan";
115  }
116  else
117  {
118  const std::streamsize prec_save = os.precision();
119  os.precision(writtenDigits(FloatingPointType()));
120  os << rhs.ref_;
121  os.precision(prec_save);
122  return os;
123  }
124  }
125 } // namespace OpenMS
126 
127 #endif // #ifndef
Int writtenDigits(const FloatingPointType &=FloatingPointType())
Number of digits commonly used for writing a floating point type (a.k.a. precision). Specializations are defined for float, double, long double.
Definition: Types.h:295
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
const PrecisionWrapper< FloatingPointType > precisionWrapper(const FloatingPointType rhs)
Wrapper function that sets the appropriate precision for output temporarily. The original precision i...
Definition: PrecisionWrapper.h:97
PrecisionWrapper(const FloatingPointType rhs)
Constructor. Note: Normally you will prefer to use the "make"-function precisionWrapper(), which see.
Definition: PrecisionWrapper.h:54
Wrapper class to implement output with appropriate precision. See precisionWrapper().
Definition: PrecisionWrapper.h:51
FloatingPointType const ref_
Definition: PrecisionWrapper.h:58
PrecisionWrapper(const PrecisionWrapper &rhs)
Definition: PrecisionWrapper.h:56

OpenMS / TOPP release 2.3.0 Documentation generated on Tue Jan 9 2018 18:22:03 using doxygen 1.8.13