xlnt
numeric.hpp
1 // Copyright (c) 2014-2022 Thomas Fussell
2 // Copyright (c) 2024-2025 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 <cmath>
28 #include <limits>
29 #include <type_traits>
30 
31 #undef min
32 #undef max
33 
34 namespace xlnt {
35 namespace detail {
36 
40 template <typename Number>
41 constexpr Number abs(Number val)
42 {
43  return (val < Number{0}) ? -val : val;
44 }
45 
49 template <typename NumberL, typename NumberR>
50 constexpr typename std::common_type<NumberL, NumberR>::type (max)(NumberL lval, NumberR rval)
51 {
52  return (lval < rval) ? rval : lval;
53 }
54 
58 template <typename NumberL, typename NumberR>
59 constexpr typename std::common_type<NumberL, NumberR>::type (min)(NumberL lval, NumberR rval)
60 {
61  return (lval < rval) ? lval : rval;
62 }
63 
73 template <typename EpsilonType = float, // the type to extract epsilon from
74  typename LNumber, typename RNumber> // parameter types (deduced)
75 bool float_equals(const LNumber &lhs, const RNumber &rhs,
76  int epsilon_scale = 20) // scale the "fuzzy" equality. Higher value gives a more tolerant comparison
77 {
78  // a type that lhs and rhs can agree on
79  using common_t = typename std::common_type<LNumber, RNumber>::type;
80  // asserts for sane usage
81  static_assert(std::is_floating_point<LNumber>::value || std::is_floating_point<RNumber>::value,
82  "Using this function with two integers is just wasting time. Use ==");
83  static_assert(std::numeric_limits<EpsilonType>::epsilon() < EpsilonType{1},
84  "epsilon >= 1.0 will cause all comparisons to return true");
85 
86  // NANs always compare false with themselves
87  if (std::isnan(lhs) || std::isnan(rhs))
88  {
89  return false;
90  }
91  // epsilon type defaults to float because even if both args are a higher precision type
92  // either or both could have been promoted by prior operations
93  // if a higher precision is required, the template type can be changed
94  constexpr common_t epsilon = static_cast<common_t>(std::numeric_limits<EpsilonType>::epsilon());
95  // the "epsilon" then needs to be scaled into the comparison range
96  // epsilon for numeric_limits is valid when abs(x) <1.0, scaling only needs to be upwards
97  // in particular, this prevents a lhs of 0 from requiring an exact comparison
98  // additionally, a scale factor is applied.
99  common_t scaled_fuzz = epsilon_scale * epsilon * max(max(xlnt::detail::abs<common_t>(lhs),
100  xlnt::detail::abs<common_t>(rhs)), // |max| of parameters.
101  common_t{1}); // clamp
102  return ((lhs + scaled_fuzz) >= rhs) && ((rhs + scaled_fuzz) >= lhs);
103 }
104 
105 } // namespace detail
106 } // namespace xlnt
Enumerates the possible types a cell can be determined by it&#39;s current value.
Definition: cell.hpp:37