Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2017
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. class floating_point {
  2. private:
  3.     double x;
  4.     void safe_code_tm(double val){
  5.         assert(!std::isinf(val) && "number is infinite");
  6.         assert(!std::isnan(val) && "number is NaN");
  7.     }
  8. public:
  9.     floating_point(double value) : x(value) {}
  10.     floating_point() : floating_point(0.0) {}
  11.     floating_point& operator+=(const floating_point& rhs)
  12.     {
  13.         this->x += rhs.x;
  14.         this->safe_code_tm(this->x);
  15.         return *this;
  16.     }
  17.     floating_point& operator-=(const floating_point& rhs)
  18.     {
  19.         this->x -= rhs.x;
  20.         this->safe_code_tm(this->x);
  21.         return *this;
  22.     }
  23.     floating_point& operator/=(const floating_point& rhs)
  24.     {
  25.         this->x /= rhs.x;
  26.         this->safe_code_tm(this->x);
  27.         return *this;
  28.     }
  29.     floating_point& operator*=(const floating_point& rhs)
  30.     {
  31.         this->x *= rhs.x;
  32.         this->safe_code_tm(this->x);
  33.         return *this;
  34.     }
  35.     friend floating_point operator+(floating_point lhs, const floating_point& rhs)
  36.     {
  37.         lhs += rhs;
  38.         lhs.safe_code_tm(lhs.x);
  39.         return lhs;
  40.     }
  41.     friend floating_point operator-(floating_point lhs, const floating_point& rhs)
  42.     {
  43.         lhs -= rhs;
  44.         lhs.safe_code_tm(lhs.x);
  45.         return lhs;
  46.     }
  47.     friend floating_point operator/(floating_point lhs, const floating_point& rhs)
  48.     {
  49.         lhs /= rhs;
  50.         lhs.safe_code_tm(lhs.x);
  51.         return lhs;
  52.     }
  53.     friend floating_point operator*(floating_point lhs, const floating_point& rhs)
  54.     {
  55.         lhs *= rhs;
  56.         lhs.safe_code_tm(lhs.x);
  57.         return lhs;
  58.     }
  59.     inline friend bool operator==(const floating_point& lhs, const floating_point& rhs)
  60.     {
  61.         return (lhs.x == rhs.x);
  62.     }
  63.     inline friend bool operator!=(const floating_point& lhs, const floating_point& rhs)
  64.     {
  65.         return !(lhs == rhs);
  66.     }
  67.     friend std::ostream& operator << (std::ostream& stream, const floating_point& p)
  68.     {
  69.         stream << p.x;
  70.         return stream;
  71.     }
  72. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement