EEROS  1.0.0.0
API for the EEROS Real-Time Robotics Framework
Pretty.hpp
Go to the documentation of this file.
1 #ifndef ORG_EEROS_LOGGER_PRETTY_HPP_
2 #define ORG_EEROS_LOGGER_PRETTY_HPP_
3 
4 #include <sstream>
5 #include <string>
6 #include <math.h>
7 
8 namespace eeros
9 {
10  namespace logger
11  {
12  std::string pretty(double x)
13  {
14  std::stringstream out;
15 
16  bool npositive = (x >= 0);
17  if (!npositive) x = -x;
18 
19  if (x < 1e-12)
20  {
21  return " 0.000 ";
22  }
23 
24  double l = log10(x);
25  bool positive = (l >= 0);
26  if (!positive) l = -l;
27 
28  int r = (((int)l)/3)*3;
29  if (!positive) r += 3;
30 
31  double b = pow(10,r);
32 
33  if (positive)
34  x /= b;
35  else
36  x *= b;
37 
38  int left = (int)x;
39  int right = (int)((x - left)*1000);
40 
41 
42  if (left < 10)
43  out << " ";
44  else if (left < 100)
45  out << " ";
46 
47  if (npositive)
48  out << ' ';
49  else
50  out << '-';
51 
52  out << left;
53  out << ".";
54 
55  if (right < 10)
56  out << "00";
57  else if (right < 100)
58  out << "0";
59 
60  out << right;
61 
62  if (positive)
63  {
64  switch(r)
65  {
66  case 0:
67  out << " ";
68  break;
69 
70  case 3:
71  out << "k";
72  break;
73 
74  case 6:
75  out << "M";
76  break;
77 
78  case 9:
79  out << "G";
80  break;
81 
82  case 12:
83  out << "T";
84  break;
85 
86  default:
87  out << "e";
88  if (positive) out << '+'; else out << '-';
89  out << r;
90  break;
91  }
92  }
93  else
94  {
95  switch(r)
96  {
97  case 0:
98  out << " ";
99  break;
100 
101  case 3:
102  out << "m";
103  break;
104 
105  case 6:
106  out << "u";
107  break;
108 
109  case 9:
110  out << "n";
111  break;
112 
113  case 12:
114  out << "p";
115  break;
116 
117  default:
118  out << "e";
119  if (positive) out << '+'; else out << '-';
120  out << r;
121  break;
122  }
123  }
124 
125  return out.str();
126  }
127  }
128 }
129 
130 #endif /* ORG_EEROS_LOGGER_PRETTY_HPP_ */
Definition: Config.hpp:14
std::string pretty(double x)
Definition: Pretty.hpp:12