]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/rational.hh
release: 1.0.15
[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--1998 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 truncated () const;
49   void negate ();
50   operator bool () const;
51   operator int () const;
52   operator double () const;
53   Rational operator - () const;
54   /**
55      Initialize to 0. 
56    */
57   Rational ();
58   Rational (int, int =1);
59   Rational (double);
60   Rational (Rational const&);
61
62   Rational &operator = (Rational const &);
63   Rational &operator *= (Rational);
64   Rational &operator /= (Rational);  
65   Rational &operator += (Rational);
66   Rational &operator -= (Rational);
67   static int compare (Rational const&, Rational const&);
68   int sign () const;
69   String str () const;
70 };
71
72 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, / );
73 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, + );
74 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, * );
75 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, - );
76
77 INSTANTIATE_COMPARE (Rational const&, Rational::compare);
78
79 int compare (Rational const&,Rational const&);
80 int sign (Rational r);
81
82 inline void
83 Rational::copy (Rational const&r)
84 {
85   sign_ = r.sign_;
86   num_ = r.num_;
87   den_ = r.den_;
88 }
89
90 class ostream;
91 ostream &
92 operator << (ostream &,  Rational);
93
94 #endif // RATIONAL_HH