LibMusicXML 3.22
rational.h
1/*
2 MusicXML Library
3 Copyright (C) Grame 2006-2013
4
5 This Source Code Form is subject to the terms of the Mozilla Public
6 License, v. 2.0. If a copy of the MPL was not distributed with this
7 file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
9 Grame Research Laboratory, 11, cours de Verdun Gensoul 69002 Lyon - France
10 research@grame.fr
11*/
12
13#ifndef __rational__
14#define __rational__
15
16#include <string>
17#include "exports.h"
18
19using namespace std;
20
24
25class EXP rational {
26
27 private:
28
29 long int fNumerator;
30 long int fDenominator;
31
32 // Used by rationalise()
33 long int gcd(long int a, long int b);
34
35 public:
36
37 rational(long int num = 0, long int denom = 1);
38 rational(const rational& d);
39 rational(const string &str);
40
41 long int getNumerator() const { return fNumerator; }
42 long int getDenominator() const { return fDenominator; }
43 void setNumerator(long int d) { fNumerator = d; }
44 void setDenominator(long int d) { fDenominator = d; }
45 void set(long int n, long int d) { fNumerator = n; fDenominator = d; }
46
47 rational operator +(const rational &dur) const;
48 rational operator -(const rational &dur) const;
50 rational operator *(const rational &dur) const;
51 rational operator /(const rational &dur) const;
52 // (i.e. dur * 3/2 or dur * 7/4)
53
54 rational operator *(int num) const;
55 rational operator /(int num) const;
56
57 rational& operator +=(const rational &dur);
58 rational& operator -=(const rational &dur);
60 rational& operator *=(const rational &dur);
61 rational& operator /=(const rational &dur);
62 // (i.e. dur * 3/2 or dur * 7/4)
63
64 rational& operator *=(long int num) { fNumerator *= num; return *this; }
65 rational& operator /=(long int num) { fDenominator *= num; return *this; }
66
67 rational& operator =(const rational& dur);
68
69 bool operator >(const rational &dur) const;
70 bool operator >=(const rational &dur) const {return !(*this < dur);}
71 bool operator <(const rational &dur) const;
72 bool operator <=(const rational &dur) const {return !(*this > dur);}
73
74 bool operator ==(const rational &dur) const;
75 bool operator !=(const rational &dur) const {return !(*this == dur);}
76
77 bool operator > (double num) const;
78 bool operator >=(double num) const;
79 bool operator < (double num) const;
80 bool operator <=(double num) const;
81 bool operator ==(double) const;
82
83 // Used to "rationalise" rational.
84 void rationalise();
85 void print (ostream& os) const;
86
87 operator string () const;
88 operator double () const;
89 operator float () const;
90 operator int () const;
91
92 //virtual string toString() const; Why ?? SL
93 string toString() const;
94 double toDouble() const;
95 float toFloat() const;
96 int toInt() const;
97};
98
99EXP ostream& operator<< (ostream& os, const rational& rat);
100
101#endif
Rational number representation.
Definition rational.h:25