All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Pages
pretty_stream.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2013 Piotr Wygocki
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //=======================================================================
15 #ifndef PAAL_PRETTY_STREAM_HPP
16 #define PAAL_PRETTY_STREAM_HPP
17 
18 #include <cmath>
19 #include <iostream>
20 #include <string>
21 
22 namespace paal {
23 
32 inline std::string pretty_to_string(double x, double epsilon = 1e-9) {
33  auto r = static_cast<int>(std::round(x));
35  epsilon
36  }.e(x, r)) {
37  return std::to_string(r);
38  } else {
39  return std::to_string(x);
40  }
41 }
42 
51 template <typename T> std::string pretty_to_string(T &&t) {
52  return std::to_string(t);
53 }
54 
60 template <typename Stream> struct pretty_stream {
67  pretty_stream(Stream &stream, double epsilon = 1e-9)
68  :
69  // TODO curly braces doesn't work here (gcc compiles error)
70  m_stream(stream),
71  m_epsilon{ epsilon } {}
72 
80  inline std::string pretty_to_string(double x, double epsilon = 1e-9) {
81  int r = std::round(x);
83  epsilon
84  }.e(x, r)) {
85  return std::to_string(r);
86  } else {
87  return std::to_string(x);
88  }
89  }
90 
99  template <typename T> pretty_stream<Stream> &operator<<(T &&t) {
100  m_stream << t;
101  return *this;
102  }
103 
104  using Manipulator = Stream & (Stream &);
105 
114  m_stream << m;
115  return *this;
116  }
117 
118  private:
119 
120  Stream &m_stream;
121  double m_epsilon;
122 };
123 
133 template <typename Stream>
134 pretty_stream<Stream> make_pretty_stream(Stream &s, double epsilon = 1e-9) {
135  return pretty_stream<Stream>(s, epsilon);
136 }
137 
138 }
139 #endif // PAAL_PRETTY_STREAM_HPP
pretty_stream< Stream > & operator<<(T &&t)
operator&lt;&lt; generic
std::string pretty_to_string(double x, double epsilon=1e-9)
pretty_to_string for double
pretty_stream< Stream > make_pretty_stream(Stream &s, double epsilon=1e-9)
make for pretty_stream
pretty_stream stream that uses pretty_to_string method
std::string pretty_to_string(double x, double epsilon=1e-9)
pretty_to_string prints double which is close to int as int
pretty_stream(Stream &stream, double epsilon=1e-9)
constructor
bool e(T a, T b) const
equals
Definition: floating.hpp:33