File: expectation_limits.hpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (106 lines) | stat: -rw-r--r-- 3,523 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Boost.Geometry (aka GGL, Generic Geometry Library)

// Copyright (c) 2020 Barend Gehrels, Amsterdam, the Netherlands.

// This file was modified by Oracle on 2021.
// Modifications copyright (c) 2021, Oracle and/or its affiliates.
// Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle

// Use, modification and distribution is subject to the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#ifndef GEOMETRY_TEST_EXPECTATION_LIMITS_HPP
#define GEOMETRY_TEST_EXPECTATION_LIMITS_HPP

#include <boost/geometry/util/math.hpp>

#include <ostream>

// Structure to manage expectations: there might be small variations in area, for different
// types or options, which are all acceptable. With tolerance this is inconvenient.
// The values are stored as doubles, but the member functions accept any type,
// also for example Boost.MultiPrecision types
struct expectation_limits
{
    expectation_limits(double expectation)
        : m_lower_limit(expectation)
        , m_upper_limit(expectation)
    {
    }

    expectation_limits(double lower_limit, double upper_limit)
        : m_lower_limit(lower_limit)
        , m_upper_limit(upper_limit)
    {
    }

    double get() const { return m_lower_limit; }

    bool is_zero() const { return m_lower_limit < 1.0e-8; }

    bool has_two_limits() const { return m_lower_limit < m_upper_limit; }

    template<typename T>
    bool contains_logarithmic(const T& value, double tolerance) const
    {
        using std::abs;
        using std::log;
        return abs(log(value) - std::log(m_lower_limit)) < tolerance;
    }

    template<typename T>
    bool contains(const T& value, double percentage, bool logarithmic = false) const
    {
        if (m_upper_limit < 1.0e-8)
        {
            return value < 1.0e-8;
        }
        if (logarithmic)
        {
            return contains_logarithmic(value, percentage);
        }

        // Note the > and <= and percentages, this is to make it exactly equivalent to
        // BOOST_CHECK_CLOSE(m_lower_limit, value, percentage) (if lower == upper)
        // But for two limits and optional slivers, >= is needed (for 0.00)
        double const fraction = percentage / 100.0;
        double const lower_limit = m_lower_limit * (1.0 - fraction);
        double const upper_limit = m_upper_limit * (1.0 + fraction);
        return has_two_limits()
                ? value >= lower_limit && value <= upper_limit
                : value > lower_limit && value <= upper_limit;
    }

    expectation_limits operator+(const expectation_limits& a) const
    {
        return this->has_two_limits() || a.has_two_limits()
                ? expectation_limits(this->m_lower_limit + a.m_lower_limit,
                                     this->m_upper_limit + a.m_upper_limit)
                : expectation_limits(this->m_lower_limit + a.m_lower_limit);
    }

    friend std::ostream &operator<<(std::ostream &os, const expectation_limits& lim)
    {
        if (lim.has_two_limits())
        {
            os << "[" << lim.m_lower_limit << " .. " << lim.m_upper_limit << "]";
        }
        else
        {
            os << lim.m_lower_limit;
        }
        return os;
    }

private :
    double const m_lower_limit;
    double const m_upper_limit;
};

inline expectation_limits optional_sliver(double upper_limit = 1.0e-4)
{
    return expectation_limits(0, upper_limit);
}

#endif // GEOMETRY_TEST_EXPECTATION_LIMITS_HPP