summaryrefslogtreecommitdiffstats
path: root/sigcore/Fraction.h
blob: 1b17130386289540097086119a3e65e4fd8cb61c (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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Copyright 2007-2008 Ben Boeckel <MathStuf@gmail.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * \file sigcore/Fraction.h
 */

#ifndef SIGCORE_FRACTION
#define SIGCORE_FRACTION

// Sigcore includes
#include "Global.h"

// Qt includes
#include <QtCore/QMetaType>

namespace Sigcore
{
/**
 * \class Sigcore::Fraction Fraction.h sigcore/Fraction.h
 * \brief Class that represents a fractional quantity.
 * 
 * This class should be used in place of \p double because of accuracy.
 */
class SIGCORE_EXPORT Fraction
{
    public:
        /**
         * Constructor.
         * 
         * \param numerator The numerator of the fraction.
         * \param denominator The denominator of the fraction.
         */
        explicit Fraction(const int numerator = 1, const int denominator = 1);
        /**
         * Copy constructor.
         * 
         * \param fraction The value to copy.
         */
        Fraction(const Fraction& fraction);
        
        /**
         * Set the value of the fraction.
         * 
         * \param numerator The numerator of the fraction.
         * \param denominator The denominator of the fraction.
         */
        void set(const int numerator, const int denominator);
        /**
         * Sets the numerator of the fraction.
         * 
         * \param numerator The numerator of the fraction.
         */
        void setNumerator(const int numerator);
        /**
         * Set the denominator of the fraciton.
         * 
         * \param denominator The denominator of the fraction.
         */
        void setDenominator(const int denominator);
        
        /**
         * \return The numerator of the fraction.
         */
        int numerator() const;
        /**
         * \return The denominator of the fraction.
         */
        int denominator() const;
        
        /**
         * Reduces the fraction.
         */
        void reduce();
        /**
         * Uses the value of the fraction to return a weighted boolean.
         * 
         * \return \p true with a weight of the fraction, \p false otherwise.
         */
        bool poll() const;
        /**
         * Convenience function to get a weighted boolean value.
         * 
         * \param numerator The numerator of the chance that the function returns \p true.
         * \param demoninator The denominator of the chance that the function returns \p true.
         * \return \p true with a weight of the (\p numerator / \p denominator), \p false otherwise.
         */
        static bool poll(const int numerator, const int demoninator);
        
        Fraction& operator=(const Fraction& rhs);
        operator double() const;
        Fraction operator+(const Fraction& rhs) const;
        Fraction operator-(const Fraction& rhs) const;
        Fraction operator*(const Fraction& rhs) const;
        Fraction operator/(const Fraction& rhs) const;
        Fraction operator%(const Fraction& rhs) const;
        Fraction& operator+=(const Fraction& rhs);
        Fraction& operator-=(const Fraction& rhs);
        Fraction& operator*=(const Fraction& rhs);
        Fraction& operator/=(const Fraction& rhs);
        Fraction& operator%=(const Fraction& rhs);
        bool operator==(const Fraction& rhs) const;
        bool operator!=(const Fraction& rhs) const;
        bool operator<(const Fraction& rhs) const;
        bool operator<=(const Fraction& rhs) const;
        bool operator>(const Fraction& rhs) const;
        bool operator>=(const Fraction& rhs) const;
    protected:
        void normalize();
    private:
        int m_numerator;
        int m_denominator;
};
}
Q_DECLARE_METATYPE(Sigcore::Fraction)

#endif