summaryrefslogtreecommitdiff
path: root/tests/auto/qdoc/catch_generators/src/utilities/statistics/percentages.h
blob: 2d80a459f5d7b98ec4273b3e5e20a53ebbb39d99 (plain)
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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include "../../namespaces.h"

#include <cassert>

namespace QDOC_CATCH_GENERATORS_UTILITIES_ABSOLUTE_NAMESPACE {

    /*!
     * Returns the percentage of \amount over \a total.
     *
     * \a amount needs to be greater or equal to zero and \a total
     * needs to be greater than zero.
     */
    inline double percent_of(double amount, double total) {
        assert(amount >= 0.0);
        assert(total > 0.0);

        return (amount / total) * 100.0;
    }

    /*!
     * Given the cardinality of a set, returns the percentage
     * probability that applied to every element of the set generates
     * a uniform distribution.
     */
    inline double uniform_probability(std::size_t cardinality) {
        assert(cardinality > 0);

        return (100.0 / static_cast<double>(cardinality));
    }

    /*!
     * Returns a percentage probability that is equal to \a
     * probability.
     *
     * \a probability must be in the range [0.0, 1.0]
     */
    inline double probability_to_percentage(double probability) {
        assert(probability >= 0.0);
        assert(probability <= 1.0);

        return probability * 100.0;
    }

} // end QDOC_CATCH_GENERATORS_UTILITIES_ABSOLUTE_NAMESPACE