xlnt
numeric.hpp
1 // Copyright (c) 2014-2022 Thomas Fussell
2 // Copyright (c) 2024 xlnt-community
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE
21 //
22 // @license: http://www.opensource.org/licenses/mit-license.php
23 // @author: see AUTHORS file
24 
25 #pragma once
26 
27 #include <xlnt/xlnt_config.hpp>
28 #include <algorithm>
29 #include <cassert>
30 #include <cmath>
31 #include <cstddef>
32 #include <limits>
33 #include <sstream>
34 #include <type_traits>
35 
36 #undef min
37 #undef max
38 
39 namespace xlnt {
40 namespace detail {
41 
45 template <typename Number>
46 constexpr Number abs(Number val)
47 {
48  return (val < Number{0}) ? -val : val;
49 }
50 
54 template <typename NumberL, typename NumberR>
55 constexpr typename std::common_type<NumberL, NumberR>::type (max)(NumberL lval, NumberR rval)
56 {
57  return (lval < rval) ? rval : lval;
58 }
59 
63 template <typename NumberL, typename NumberR>
64 constexpr typename std::common_type<NumberL, NumberR>::type (min)(NumberL lval, NumberR rval)
65 {
66  return (lval < rval) ? lval : rval;
67 }
68 
78 template <typename EpsilonType = float, // the type to extract epsilon from
79  typename LNumber, typename RNumber> // parameter types (deduced)
80 bool float_equals(const LNumber &lhs, const RNumber &rhs,
81  int epsilon_scale = 20) // scale the "fuzzy" equality. Higher value gives a more tolerant comparison
82 {
83  // a type that lhs and rhs can agree on
84  using common_t = typename std::common_type<LNumber, RNumber>::type;
85  // asserts for sane usage
86  static_assert(std::is_floating_point<LNumber>::value || std::is_floating_point<RNumber>::value,
87  "Using this function with two integers is just wasting time. Use ==");
88  static_assert(std::numeric_limits<EpsilonType>::epsilon() < EpsilonType{1},
89  "epsilon >= 1.0 will cause all comparisons to return true");
90 
91  // NANs always compare false with themselves
92  if (std::isnan(lhs) || std::isnan(rhs))
93  {
94  return false;
95  }
96  // epsilon type defaults to float because even if both args are a higher precision type
97  // either or both could have been promoted by prior operations
98  // if a higher precision is required, the template type can be changed
99  constexpr common_t epsilon = static_cast<common_t>(std::numeric_limits<EpsilonType>::epsilon());
100  // the "epsilon" then needs to be scaled into the comparison range
101  // epsilon for numeric_limits is valid when abs(x) <1.0, scaling only needs to be upwards
102  // in particular, this prevents a lhs of 0 from requiring an exact comparison
103  // additionally, a scale factor is applied.
104  common_t scaled_fuzz = epsilon_scale * epsilon * max(max(xlnt::detail::abs<common_t>(lhs),
105  xlnt::detail::abs<common_t>(rhs)), // |max| of parameters.
106  common_t{1}); // clamp
107  return ((lhs + scaled_fuzz) >= rhs) && ((rhs + scaled_fuzz) >= lhs);
108 }
109 
111 {
112  static constexpr int Excel_Digit_Precision = 15; //sf
113  bool should_convert_comma;
114 
115  static void convert_comma_to_pt(char *buf, int len)
116  {
117  char *buf_end = buf + len;
118  char *decimal = std::find(buf, buf_end, ',');
119  if (decimal != buf_end)
120  {
121  *decimal = '.';
122  }
123  }
124 
125  static void convert_pt_to_comma(char *buf, size_t len)
126  {
127  char *buf_end = buf + len;
128  char *decimal = std::find(buf, buf_end, '.');
129  if (decimal != buf_end)
130  {
131  *decimal = ',';
132  }
133  }
134 
135 public:
136  explicit number_serialiser()
137  : should_convert_comma(localeconv()->decimal_point[0] == ',')
138  {
139  }
140 
141  // for printing to file.
142  // This matches the output format of excel irrespective of current locale
143  std::string serialise(double d) const
144  {
145  char buf[30];
146  int len = snprintf(buf, sizeof(buf), "%.15g", d);
147  if (should_convert_comma)
148  {
149  convert_comma_to_pt(buf, len);
150  }
151  return std::string(buf, static_cast<size_t>(len));
152  }
153 
154  // replacement for std::to_string / s*printf("%f", ...)
155  // behaves same irrespective of locale
156  std::string serialise_short(double d) const
157  {
158  char buf[30];
159  int len = snprintf(buf, sizeof(buf), "%f", d);
160  if (should_convert_comma)
161  {
162  convert_comma_to_pt(buf, len);
163  }
164  return std::string(buf, static_cast<size_t>(len));
165  }
166 
167  double deserialise(const std::string &s, ptrdiff_t *len_converted) const
168  {
169  assert(!s.empty());
170  assert(len_converted != nullptr);
171  char *end_of_convert;
172  if (!should_convert_comma)
173  {
174  double d = strtod(s.c_str(), &end_of_convert);
175  *len_converted = end_of_convert - s.c_str();
176  return d;
177  }
178  char buf[30];
179  assert(s.size() + 1 < sizeof(buf));
180  const char *cstr = s.c_str();
181  auto copy_end = std::copy(cstr, cstr + s.size() + 1, buf);
182  convert_pt_to_comma(buf, static_cast<size_t>(copy_end - buf));
183  double d = strtod(buf, &end_of_convert);
184  *len_converted = end_of_convert - buf;
185  return d;
186  }
187 
188  double deserialise(const std::string &s) const
189  {
190  ptrdiff_t ignore;
191  return deserialise(s, &ignore);
192  }
193 };
194 
195 } // namespace detail
196 } // namespace xlnt
Enumerates the possible types a cell can be determined by it&#39;s current value.
Definition: cell.hpp:37
Definition: numeric.hpp:110