FAUST compiler  0.9.9.6b8
num.hh
Go to the documentation of this file.
00001 /************************************************************************
00002  ************************************************************************
00003     FAUST compiler
00004     Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
00005     ---------------------------------------------------------------------
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  ************************************************************************
00020  ************************************************************************/
00021  
00022  
00023  
00024 /*****************************************************************************
00025 ******************************************************************************
00026                                 NUM 
00027                         Y. Orlarey, (c) Grame 2002
00028 ------------------------------------------------------------------------------
00029 Nums are tagged unions of ints and floats. Nums are completly described by 
00030 the num.h file, there is no num.cpp file.
00031 
00032  API:
00033  ----
00034     num(10);                : num with int content
00035     num(3.14159);           : num with double content
00036     
00037     num op num              : op is any C binary operator
00038     
00039     type(num)               : 0 = int, 1 = double
00040     int(num);               : int content of num or conversion
00041     double(num);                : double content of num or conversion
00042     
00043  History :
00044  ---------
00045     2002-02-08 : First version
00046     
00047 ******************************************************************************
00048 *****************************************************************************/
00049 
00050 
00051 #ifndef     __NUM__
00052 #define     __NUM__
00053 
00054 
00055 //-------------------------------------------------------------------------
00056 // Class num = (int x (int + double))
00057 //-------------------------------------------------------------------------
00058 class num
00059 {
00060     int     fType;
00061     union { 
00062         int     i; 
00063         double  f;
00064     } fData;
00065 
00066  public:
00067     // constructeurs
00068     num (int x=0)       : fType(0)          { fData.i = x; }
00069     //num (double x)        : fType(1)          { fData.f = (double)x; }
00070     num (double x)      : fType(1)          { fData.f = x; }
00071     num (const num& n)  : fType(n.fType)    { fData.i = n.fData.i; }
00072 
00073     num& operator = (int n)     { fType = 0; fData.i = n; return *this; }
00074     num& operator = (double n)  { fType = 1; fData.f = n; return *this; }
00075     
00076     // predicats
00077     bool operator == (const num& n) const { return fType == n.fType && fData.i == n.fData.i; }
00078     bool operator != (const num& n) const { return fType != n.fType || fData.i != n.fData.i; }
00079     
00080     // accessors
00081     int         type()      const   { return fType; }
00082     operator    int()       const   { return (fType) ? int(fData.f) : fData.i; }
00083     operator    double()    const   { return (fType) ? fData.f : double(fData.i); }
00084     
00085 };
00086 
00087 
00088 inline int isfloat (const num& n) { return n.type(); }
00089 
00090 inline const num operator+ (const num& x, const num& y) 
00091     { return (isfloat(x)|isfloat(y)) ? num(double(x)+double(y)) : num(int(x)+int(y)); }
00092 
00093 inline const num operator- (const num& x, const num& y) 
00094     { return (isfloat(x)|isfloat(y)) ? num(double(x)-double(y)) : num(int(x)-int(y)); }
00095 
00096 inline const num operator* (const num& x, const num& y) 
00097     { return (isfloat(x)|isfloat(y)) ? num(double(x)*double(y)) : num(int(x)*int(y)); }
00098 
00099 inline const num operator/ (const num& x, const num& y) 
00100     { return (isfloat(x)|isfloat(y)) ? num(double(x)/double(y)) : num(int(x)/int(y)); }
00101 
00102 inline const num operator% (const num& x, const num& y) 
00103     { return num(int(x)%int(y)); }
00104 
00105 
00106 // operations sur les bits
00107 inline const num operator<< (const num& x, const num& y)    
00108     { return num(int(x)<<int(y)); }
00109 
00110 inline const num operator>> (const num& x, const num& y)    
00111     { return num(int(x)>>int(y)); }
00112 
00113 
00114 // operations booléennes sur les bits
00115 inline const num operator& (const num& x, const num& y) 
00116     { return num(int(x)&int(y)); }
00117 
00118 inline const num operator| (const num& x, const num& y) 
00119     { return num(int(x)|int(y)); }
00120 
00121 inline const num operator^ (const num& x, const num& y) 
00122     { return num(int(x)^int(y)); }
00123 
00124 
00125 // operations de comparaison
00126 inline const num operator> (const num& x, const num& y) 
00127     { return (isfloat(x)|isfloat(y)) ? num(double(x)>double(y)) : num(int(x)>int(y)); }
00128 
00129 inline const num operator< (const num& x, const num& y) 
00130     { return (isfloat(x)|isfloat(y)) ? num(double(x)<double(y)) : num(int(x)<int(y)); }
00131 
00132 inline const num operator>= (const num& x, const num& y)    
00133     { return (isfloat(x)|isfloat(y)) ? num(double(x)>=double(y)) : num(int(x)>=int(y)); }
00134 
00135 inline const num operator<= (const num& x, const num& y)    
00136     { return (isfloat(x)|isfloat(y)) ? num(double(x)<=double(y)) : num(int(x)<=int(y)); }
00137 
00138 inline const num operator== (const num& x, const num& y)    
00139     { return (isfloat(x)|isfloat(y)) ? num(double(x)==double(y)) : num(int(x)==int(y)); }
00140 
00141 inline const num operator!= (const num& x, const num& y)    
00142     { return (isfloat(x)|isfloat(y)) ? num(double(x)!=double(y)) : num(int(x)!=int(y)); }
00143 
00144 
00145 #endif