summaryrefslogtreecommitdiff
path: root/mtxrational.h
blob: e07d9d053dec923d04a4ce5de991ac5c2803cce8 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* $Id$ */

/*
 * Copyright (C) 2005-2006 Richard Braun
 * 
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef _MTXRATIONAL_H
#define _MTXRATIONAL_H

#include <string>
#include <iostream>

#include "mtx.h"
#include "mtxvalue.h"
#include "mtxinteger.h"

/**
 * A rational.
 * 
 * This class merely allows to create rationals which consist of a numerator
 * and a denominator which are both MtxInteger objects.
 */
class MtxRational: public MtxValue
{
  private:
    /**
     * Numerator.
     */
    MtxInteger numerator;

    /**
     * Denominator.
     */
    MtxInteger denominator;

    /**
     * Precision used when computing floating point representation.
     * 
     * @see getPrecision()
     * @see setPrecision()
     */
    static unsigned int precision;

  public:
    /**
     * Default constructor.
     * 
     * 0/1 is the default value.
     */
    MtxRational();

    /**
     * Constructor usable for "rational = 123" cases.
     */
    MtxRational(int integer);

    /**
     * Constructor usable for "rational = 1.23" cases.
     * 
     * If 1.23 is given to this constructor, numerator is 123 and
     * denominator is 100.
     */
    MtxRational(double rational);

    /**
     * Constructor usable for "rational = "1.23"" cases.
     * 
     * Same as MtxRational(double rational), with a character string for
     * larger values.
     * 
     * @see MtxRational(double rational)
     */
    MtxRational(const char *rational);

    /**
     * Main constructor.
     */
    MtxRational(const MtxInteger &numerator,
                const MtxInteger &denominator = 1);

    virtual ~MtxRational();

    /**
     * Get the numerator.
     */
    MtxInteger getNumerator() const;

    /**
     * Set the numerator.
     */
    void setNumerator(const MtxInteger &numerator);

    /**
     * Get the denominator.
     */
    MtxInteger getDenominator() const;

    /**
     * Set the denominator. An exception is raised if the given denominator
     * is 0.
     */
    void setDenominator(const MtxInteger &denominator);

    /*
     * Some operators are available for easiness.
     */
    MtxRational operator+() const;
    MtxRational operator-() const;
    bool operator==(const MtxRational &rational) const;
    bool operator!=(const MtxRational &rational) const;
    bool operator>(const MtxRational &rational) const;
    bool operator>=(const MtxRational &rational) const;
    bool operator<(const MtxRational &rational) const;
    bool operator<=(const MtxRational &rational) const;
    MtxRational operator+(const MtxRational &rational) const;
    MtxMatrix operator+(const MtxMatrix &matrix) const;
    MtxRational & operator++();
    MtxRational & operator++(int a);
    MtxRational & operator+=(const MtxRational &rational);
    MtxRational operator-(const MtxRational &rational) const;
    MtxMatrix operator-(const MtxMatrix &matrix) const;
    MtxRational & operator--();
    MtxRational & operator--(int a);
    MtxRational & operator-=(const MtxRational &rational);
    MtxRational operator*(const MtxRational &rational) const;
    MtxMatrix operator*(const MtxMatrix &matrix) const;
    MtxRational & operator*=(const MtxRational &rational);
    MtxRational operator/(const MtxRational &rational) const;
    MtxMatrix operator/(const MtxMatrix &matrix) const;
    MtxRational & operator/=(const MtxRational &rational);
    friend std::ostream & operator<<(std::ostream &cout,
                                     const MtxRational &rational);

    /**
     * Simplify the numerator and denominator if possible.
     * 
     * E.g. 5/10 becomes 1/2.
     */
    void simplify();

    /**
     * Normalize the rational.
     * 
     * If it's negative, the sign is set on the numerator. It is then
     * simplified. The object is returned so that this function can be
     * used inline.
     */
    MtxRational & normalize();

    /**
     * Return the numerator[/denominator] string representation of
     * this rational. If denominator is 1, it is ommited.
     */
    virtual std::string toString() const;

    /**
     * Return the floating point representation of this rational.
     */
    std::string toFloatString() const;

    /*
     * See mtxvalue.h.
     */
    MTXVALUE_CLONE_METHOD_DECLARATION(MtxRational);
    virtual MtxValue * add(const MtxValue *value) const;
    virtual MtxValue * subtract(const MtxValue *value) const;
    virtual MtxValue * multiply(const MtxValue *value) const;
    virtual MtxValue * divide(const MtxValue *value) const;
    virtual MtxValue * opposite() const;

    /**
     * Return the reciprocal of this rational.
     */
    MtxRational reciprocal() const;

    /**
     * Get precision.
     * 
     * @see precision
     * @see setPrecision()
     */
    static unsigned int getPrecision();

    /**
     * Set precision.
     * 
     * @see precision
     * @see getPrecision()
     */
    static void setPrecision(unsigned int new_precision);
};

#endif /* _MTXRATIONAL_H */