]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/rational.hh
patch::: 1.1.31.jcn2: ughjeweg
[lilypond.git] / flower / include / rational.hh
1 /*
2   rational.hh -- declare rational helpers
3
4   source file of the Flower Library
5
6   (c)  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9
10 #ifndef RATIONAL_HH
11 #define RATIONAL_HH
12
13 #if PARANOIA
14 #ifndef Rational
15 #define Rational MyRational
16 #endif
17 #endif
18
19 #include "compare.hh"
20 #include "arithmetic-operator.hh"
21 #include "fproto.hh"
22
23 /**
24    Rational numbers.  Included is support for + and - infinity.
25  */
26 class Rational {
27   /**
28      Sign of rational.
29      -2, .. 2
30
31      -2,2 is - and + infinity.
32      -1,1 is negative and positive.
33      0 if *this is zero.
34    */
35   int sign_;
36   unsigned int num_, den_;
37   void normalise ();
38   void copy (Rational const &);
39
40 public:
41   void set_infinite (int sign);
42   bool infty_b () const;
43   void invert ();
44   int num  () const { return sign_ * num_; }
45   int den  () const { return den_; }
46   int num_i  () const { return sign_ * num_; }
47   int den_i  () const { return den_; }
48   Rational trunc_rat () const;
49   Rational div_rat (Rational) const;
50   Rational mod_rat (Rational) const;
51   void negate ();
52   operator bool () const;
53   operator int () const;
54   operator double () const;
55   Rational operator - () const;
56   /**
57      Initialize to 0. 
58    */
59   Rational ();
60   Rational (int, int =1);
61   Rational (double);
62   Rational (Rational const&);
63
64   Rational &operator = (Rational const &);
65   Rational &operator *= (Rational);
66   Rational &operator /= (Rational);  
67   Rational &operator += (Rational);
68   Rational &operator -= (Rational);
69   Rational &operator %= (Rational);
70   static int compare (Rational const&, Rational const&);
71   int sign () const;
72   String str () const;
73 };
74
75 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, / );
76 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, + );
77 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, * );
78 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, - );
79 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, % );
80
81 INSTANTIATE_COMPARE (Rational const&, Rational::compare);
82
83 int compare (Rational const&,Rational const&);
84 int sign (Rational r);
85
86 inline void
87 Rational::copy (Rational const&r)
88 {
89   sign_ = r.sign_;
90   num_ = r.num_;
91   den_ = r.den_;
92 }
93
94 class ostream;
95 ostream &
96 operator << (ostream &,  Rational);
97
98 #endif // RATIONAL_HH