ztsdb
double.hpp
1 // (C) 2016 Leonardo Silvestri
2 //
3 // This file is part of ztsdb.
4 //
5 // ztsdb is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // ztsdb is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with ztsdb. If not, see <http://www.gnu.org/licenses/>.
17 
18 
19 #ifndef DOUBLE_HPP
20 #define DOUBLE_HPP
21 
22 
23 #include <string>
24 #include <sstream>
25 #include <double-conversion.h>
26 
27 
28 namespace ztsdb {
29 
35  inline std::string to_string(double d, int digits) {
36  const int bufsz = 1024;
37  char buf[bufsz];
38  // see https://github.com/google/double-conversion
39  double_conversion::DoubleToStringConverter dtosc(double_conversion::
40  DoubleToStringConverter::
41  EMIT_POSITIVE_EXPONENT_SIGN,
42  "Inf", "NaN", 'e', -6, 6, 6, 6);
43  double_conversion::StringBuilder sb(buf, bufsz);
44  dtosc.ToShortest(d, &sb);
45  auto res = sb.position();
46  sb.Finalize();
47  if (res > digits) {
48  double_conversion::StringBuilder sb2(buf, bufsz);
49  dtosc.ToFixed(d, digits, &sb2);
50  }
51 
52  std::stringstream ss;
53  ss << buf;
54  return ss.str();
55  }
56 
57  inline std::string fixed_scientific(double d, int digits) {
58  const int bufsz = 1024;
59  char buf[bufsz];
60  // see https://github.com/google/double-conversion
61  double_conversion::DoubleToStringConverter dtosc(double_conversion::
62  DoubleToStringConverter::
63  EMIT_POSITIVE_EXPONENT_SIGN,
64  "Inf", "NaN", 'e', 0, 0, 0, 0);
65  double_conversion::StringBuilder sb(buf, bufsz);
66  dtosc.ToExponential(d, digits-1, &sb);
67  sb.Finalize();
68 
69  return string(buf);
70  }
71 
72  inline std::string fixed_decimal(double d, int digits) {
73  const int bufsz = 1024;
74  char buf[bufsz];
75  // see https://github.com/google/double-conversion
76  double_conversion::DoubleToStringConverter dtosc(double_conversion::
77  DoubleToStringConverter::
78  EMIT_POSITIVE_EXPONENT_SIGN,
79  "Inf", "NaN", 'e', 0, 0, 0, 0);
80  double_conversion::StringBuilder sb(buf, bufsz);
81  dtosc.ToFixed(d, digits-1, &sb);
82  sb.Finalize();
83 
84  return string(buf);
85  }
86 
87 
88  inline std::string shortest_scientific(double d) {
89  const int bufsz = 1024;
90  char buf[bufsz];
91  // see https://github.com/google/double-conversion
92  double_conversion::DoubleToStringConverter dtosc(double_conversion::
93  DoubleToStringConverter::
94  EMIT_POSITIVE_EXPONENT_SIGN,
95  "Inf", "NaN", 'e', 0, 0, 0, 0);
96  double_conversion::StringBuilder sb(buf, bufsz);
97  dtosc.ToShortest(d, &sb);
98  sb.Finalize();
99 
100  return string(buf);
101  }
102 
103  inline std::string shortest_decimal(double d) {
104  const int bufsz = 1024;
105  char buf[bufsz];
106  // see https://github.com/google/double-conversion
107  double_conversion::DoubleToStringConverter dtosc(double_conversion::
108  DoubleToStringConverter::
109  EMIT_POSITIVE_EXPONENT_SIGN,
110  "Inf", "NaN", 'e', -99, 99, 0, 0);
111  double_conversion::StringBuilder sb(buf, bufsz);
112  dtosc.ToShortest(d, &sb);
113  sb.Finalize();
114 
115  return string(buf);
116  }
117 
118 }
119 
120 
121 #endif