OpenMS  2.4.0
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-2018.
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 #pragma once
36 
37 #include <OpenMS/config.h>
38 #include <OpenMS/CONCEPT/Types.h>
39 
40 #include <iostream>
41 #include <iomanip>
42 
43 namespace OpenMS
44 {
45 
49  template <typename FloatingPointType>
51  {
53  explicit PrecisionWrapper(const FloatingPointType rhs) :
54  ref_(rhs) {}
56  ref_(rhs.ref_) {}
57  FloatingPointType const ref_;
58 private:
59  PrecisionWrapper(); // intentionally not implemented
60  };
61 
95  template <typename FloatingPointType>
96  inline const PrecisionWrapper<FloatingPointType> precisionWrapper(const FloatingPointType rhs)
97  {
99  }
100 
102  template <typename FloatingPointType>
103  inline std::ostream & operator<<(std::ostream & os, const PrecisionWrapper<FloatingPointType> & rhs)
104  {
105  // Same test as used by isnan(), spelled out here to avoid issues during overload resolution.
106 #pragma clang diagnostic push
107 #pragma clang diagnostic ignored "-Wfloat-equal"
108  if (rhs.ref_ != rhs.ref_)
109 #pragma clang diagnostic pop
110  {
111  // That's what Linux GCC uses, and gnuplot understands.
112  // Windows would print stuff like 1.#QNAN which makes testing hard.
113  return os << "nan";
114  }
115  else
116  {
117  const std::streamsize prec_save = os.precision();
118  os.precision(writtenDigits(FloatingPointType()));
119  os << rhs.ref_;
120  os.precision(prec_save);
121  return os;
122  }
123  }
124 } // namespace OpenMS
125 
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:294
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
const PrecisionWrapper< FloatingPointType > precisionWrapper(const FloatingPointType rhs)
Wrapper function that sets the appropriate precision for output temporarily. The original precision i...
Definition: PrecisionWrapper.h:96
PrecisionWrapper(const FloatingPointType rhs)
Constructor. Note: Normally you will prefer to use the "make"-function precisionWrapper(), which see.
Definition: PrecisionWrapper.h:53
Wrapper class to implement output with appropriate precision. See precisionWrapper().
Definition: PrecisionWrapper.h:50
FloatingPointType const ref_
Definition: PrecisionWrapper.h:57
PrecisionWrapper(const PrecisionWrapper &rhs)
Definition: PrecisionWrapper.h:55