differential code coverage report with master
Current view: top level - include/xlnt/utils - numeric.hpp (source / functions) Coverage Total Hit UBC CBC
Current: coverage.info Lines: 91.7 % 12 11 1 11
Current Date: 2025-12-15 23:01:28 Functions: 100.0 % 16 16 16
Baseline: coverage_master.info Branches: 81.2 % 16 13 6 26
Baseline Date: 2025-12-15 23:01:27

             Branch data    TLA  Line data    Source code
       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                 :                : 
      37                 :                : /// <summary>
      38                 :                : /// constexpr abs
      39                 :                : /// </summary>
      40                 :                : template <typename Number>
      41                 :CBC       12582 : constexpr Number abs(Number val)
      42                 :                : {
      43         [ +  + ]:          12582 :     return (val < Number{0}) ? -val : val;
      44                 :                : }
      45                 :                : 
      46                 :                : /// <summary>
      47                 :                : /// constexpr max
      48                 :                : /// </summary>
      49                 :                : template <typename NumberL, typename NumberR>
      50                 :          12588 : constexpr typename std::common_type<NumberL, NumberR>::type (max)(NumberL lval, NumberR rval)
      51                 :                : {
      52         [ +  + ]:          12588 :     return (lval < rval) ? rval : lval;
      53                 :                : }
      54                 :                : 
      55                 :                : /// <summary>
      56                 :                : /// constexpr min
      57                 :                : /// </summary>
      58                 :                : template <typename NumberL, typename NumberR>
      59                 :             12 : constexpr typename std::common_type<NumberL, NumberR>::type (min)(NumberL lval, NumberR rval)
      60                 :                : {
      61         [ +  + ]:             12 :     return (lval < rval) ? lval : rval;
      62                 :                : }
      63                 :                : 
      64                 :                : /// <summary>
      65                 :                : /// Floating point equality requires a bit of fuzzing due to the imprecise nature of fp calculation
      66                 :                : /// References:
      67                 :                : /// - Several blogs/articles were referenced with the following being the most useful
      68                 :                : /// -- https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
      69                 :                : /// -- http://realtimecollisiondetection.net/blog/?p=89
      70                 :                : /// - Testing Frameworks {Catch2, Boost, Google}, primarily for selecting the default scale factor
      71                 :                : /// -- None of these even remotely agree
      72                 :                : /// </summary>
      73                 :                : template <typename EpsilonType = float, // the type to extract epsilon from
      74                 :                :     typename LNumber, typename RNumber> // parameter types (deduced)
      75                 :           6288 : 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   [ +  -  -  +  :           6288 :     if (std::isnan(lhs) || std::isnan(rhs))
                   -  + ]
      88                 :                :     {
      89                 :UBC           0 :         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                 :CBC        6288 :     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                 :           6288 :     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   [ +  +  +  + ]:           6288 :     return ((lhs + scaled_fuzz) >= rhs) && ((rhs + scaled_fuzz) >= lhs);
     103                 :                : }
     104                 :                : 
     105                 :                : } // namespace detail
     106                 :                : } // namespace xlnt
        

Generated by: LCOV version 2.3.1-beta