1 /************************************************************************
2 ************************************************************************
4 Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
5 ---------------------------------------------------------------------
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 ************************************************************************
20 ************************************************************************/
24 /*****************************************************************************
25 ******************************************************************************
27 Y. Orlarey, (c) Grame 2002
28 ------------------------------------------------------------------------------
29 Nums are tagged unions of ints and floats. Nums are completly described by
30 the num.h file, there is no num.cpp file.
34 num(10); : num with int content
35 num(3.14159); : num with double content
37 num op num : op is any C binary operator
39 type(num) : 0 = int, 1 = double
40 int(num); : int content of num or conversion
41 double(num); : double content of num or conversion
45 2002-02-08 : First version
47 ******************************************************************************
48 *****************************************************************************/
55 //-------------------------------------------------------------------------
56 // Class num = (int x (int + double))
57 //-------------------------------------------------------------------------
68 num (int x=0) : fType(0) { fData.i = x; }
69 //num (double x) : fType(1) { fData.f = (double)x; }
70 num (double x) : fType(1) { fData.f = x; }
71 num (const num& n) : fType(n.fType) { fData.i = n.fData.i; }
73 num& operator = (int n) { fType = 0; fData.i = n; return *this; }
74 num& operator = (double n) { fType = 1; fData.f = n; return *this; }
77 bool operator == (const num& n) const { return fType == n.fType && fData.i == n.fData.i; }
78 bool operator != (const num& n) const { return fType != n.fType || fData.i != n.fData.i; }
81 int type() const { return fType; }
82 operator int() const { return (fType) ? int(fData.f) : fData.i; }
83 operator double() const { return (fType) ? fData.f : double(fData.i); }
88 inline int isfloat (const num& n) { return n.type(); }
90 inline const num operator+ (const num& x, const num& y)
91 { return (isfloat(x)|isfloat(y)) ? num(double(x)+double(y)) : num(int(x)+int(y)); }
93 inline const num operator- (const num& x, const num& y)
94 { return (isfloat(x)|isfloat(y)) ? num(double(x)-double(y)) : num(int(x)-int(y)); }
96 inline const num operator* (const num& x, const num& y)
97 { return (isfloat(x)|isfloat(y)) ? num(double(x)*double(y)) : num(int(x)*int(y)); }
99 inline const num operator/ (const num& x, const num& y)
100 { return (isfloat(x)|isfloat(y)) ? num(double(x)/double(y)) : num(int(x)/int(y)); }
102 inline const num operator% (const num& x, const num& y)
103 { return num(int(x)%int(y)); }
106 // operations sur les bits
107 inline const num operator<< (const num& x, const num& y)
108 { return num(int(x)<<int(y)); }
110 inline const num operator>> (const num& x, const num& y)
111 { return num(int(x)>>int(y)); }
114 // operations booléennes sur les bits
115 inline const num operator& (const num& x, const num& y)
116 { return num(int(x)&int(y)); }
118 inline const num operator| (const num& x, const num& y)
119 { return num(int(x)|int(y)); }
121 inline const num operator^ (const num& x, const num& y)
122 { return num(int(x)^int(y)); }
125 // operations de comparaison
126 inline const num operator> (const num& x, const num& y)
127 { return (isfloat(x)|isfloat(y)) ? num(double(x)>double(y)) : num(int(x)>int(y)); }
129 inline const num operator< (const num& x, const num& y)
130 { return (isfloat(x)|isfloat(y)) ? num(double(x)<double(y)) : num(int(x)<int(y)); }
132 inline const num operator>= (const num& x, const num& y)
133 { return (isfloat(x)|isfloat(y)) ? num(double(x)>=double(y)) : num(int(x)>=int(y)); }
135 inline const num operator<= (const num& x, const num& y)
136 { return (isfloat(x)|isfloat(y)) ? num(double(x)<=double(y)) : num(int(x)<=int(y)); }
138 inline const num operator== (const num& x, const num& y)
139 { return (isfloat(x)|isfloat(y)) ? num(double(x)==double(y)) : num(int(x)==int(y)); }
141 inline const num operator!= (const num& x, const num& y)
142 { return (isfloat(x)|isfloat(y)) ? num(double(x)!=double(y)) : num(int(x)!=int(y)); }