home *** CD-ROM | disk | FTP | other *** search
/ NEXT Generation 27 / NEXT27.iso / pc / demos / emperor / dx3.exe / SDK / SAMPLES / FLIP2D / FIXED.H < prev    next >
C/C++ Source or Header  |  1996-08-28  |  2KB  |  43 lines

  1. /**************************************************************************
  2.  
  3.     Fixed.h - 16.16 Fixed point class
  4.  
  5.  **************************************************************************/
  6.  
  7. inline long fixed_mul(long a, long b) {return MulDiv(a, b, 65536);}
  8. inline long fixed_div(long a, long b) {return MulDiv(a, 65536, b);}
  9.  
  10. class Fixed {
  11.     private:
  12.         long fx;
  13.     public:
  14.         Fixed()             {}
  15.         ~Fixed()            {}
  16.  
  17.         Fixed(long l)       {fx = l<<16;}
  18.         Fixed(int i)        {fx = (long)i<<16;}
  19.         Fixed(double d)     {fx = (long)(d * 65536.0);}
  20.  
  21.         int Int()           {return (int)(fx >> 16);}
  22.         int Frac()          {return (int)(fx & 0xFFFF);}
  23.  
  24.         operator int()      {return (int)(fx >> 16);}
  25.         operator double()   {return (double)fx / 65536.0;}
  26.  
  27.         Fixed operator +(Fixed a) {Fixed c; c.fx = fx + a.fx; return c;}
  28.         Fixed operator -(Fixed a) {Fixed c; c.fx = fx - a.fx; return c;}
  29.         Fixed operator *(Fixed a) {Fixed c; c.fx = fixed_mul(fx,a.fx); return c;}
  30.         Fixed operator /(Fixed a) {Fixed c; c.fx = fixed_div(fx,a.fx); return c;}
  31.  
  32.         int operator <(Fixed a)  {return fx < a.fx;}
  33.         int operator >(Fixed a)  {return fx > a.fx;}
  34.         int operator ==(Fixed a) {return fx == a.fx;}
  35.         int operator !=(Fixed a) {return fx != a.fx;}
  36.  
  37. //      Fixed& operator  =(Fixed a) {fx  = a.fx; return *this;}
  38.         Fixed& operator +=(Fixed a) {fx += a.fx; return *this;}
  39.         Fixed& operator -=(Fixed a) {fx -= a.fx; return *this;}
  40.         Fixed& operator *=(Fixed a) {fx  = fixed_mul(fx,a.fx); return *this;}
  41.         Fixed& operator /=(Fixed a) {fx  = fixed_div(fx,a.fx); return *this;}
  42. };
  43.