Implementing Fixed-Point Numbers and support all comparison operators - C++ Class

C++ examples for Class:Operator Overload

Description

Implementing Fixed-Point Numbers and support all comparison operators

Demo Code

#include <iostream>

using namespace std;

template<int E>
struct MyNumber//from w w w.ja  va 2s . c  om
{
  typedef MyNumber self;
  static const int factor = 1 << (E - 1);
  MyNumber() : m(0) { }
  MyNumber(double d) : m(static_cast<int>(d * factor)) { }
  self& operator+=(const self& x) { m += x.m; return *this; }
  self& operator-=(const self& x) { m -= x.m; return *this; }
  self& operator*=(const self& x) { m *= x.m; m >>= E; return *this; }
  self& operator/=(const self& x) { m /= x.m; m *= factor; return *this; }
  self& operator*=(int x) { m *= x; return *this; }
  self& operator/=(int x) { m /= x; return *this; }
  self operator-() { return self(-m); }
  double toDouble() const { return double(m) / factor;  }

  // friend functions
  friend self operator+(self x, const self& y) { return x += y; }
  friend self operator-(self x, const self& y) { return x -= y; }
  friend self operator*(self x, const self& y) { return x *= y; }
  friend self operator/(self x, const self& y) { return x /= y; }

  // comparison operators
  friend bool operator==(const self& x, const self& y) { return x.m == y.m; }
  friend bool operator!=(const self& x, const self& y) { return x.m != y.m; }
  friend bool operator>(const self& x, const self& y) { return x.m > y.m; }
  friend bool operator<(const self& x, const self& y) { return x.m < y.m; }
  friend bool operator>=(const self& x, const self& y) { return x.m >= y.m; }
  friend bool operator<=(const self& x, const self& y) { return x.m <= y.m; }
private:
  int m;
};

typedef MyNumber<10> FixedReal;

int main() {
  FixedReal x(0);
  for (int i=0; i < 100; ++i) {
    x += FixedReal(0.0625);
  }
  cout << x.toDouble() << endl;
}

Result


Related Tutorials