Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class floating_point {
- private:
- double x;
- void safe_code_tm(double val){
- assert(!std::isinf(val) && "number is infinite");
- assert(!std::isnan(val) && "number is NaN");
- }
- public:
- floating_point(double value) : x(value) {}
- floating_point() : floating_point(0.0) {}
- floating_point& operator+=(const floating_point& rhs)
- {
- this->x += rhs.x;
- this->safe_code_tm(this->x);
- return *this;
- }
- floating_point& operator-=(const floating_point& rhs)
- {
- this->x -= rhs.x;
- this->safe_code_tm(this->x);
- return *this;
- }
- floating_point& operator/=(const floating_point& rhs)
- {
- this->x /= rhs.x;
- this->safe_code_tm(this->x);
- return *this;
- }
- floating_point& operator*=(const floating_point& rhs)
- {
- this->x *= rhs.x;
- this->safe_code_tm(this->x);
- return *this;
- }
- friend floating_point operator+(floating_point lhs, const floating_point& rhs)
- {
- lhs += rhs;
- lhs.safe_code_tm(lhs.x);
- return lhs;
- }
- friend floating_point operator-(floating_point lhs, const floating_point& rhs)
- {
- lhs -= rhs;
- lhs.safe_code_tm(lhs.x);
- return lhs;
- }
- friend floating_point operator/(floating_point lhs, const floating_point& rhs)
- {
- lhs /= rhs;
- lhs.safe_code_tm(lhs.x);
- return lhs;
- }
- friend floating_point operator*(floating_point lhs, const floating_point& rhs)
- {
- lhs *= rhs;
- lhs.safe_code_tm(lhs.x);
- return lhs;
- }
- inline friend bool operator==(const floating_point& lhs, const floating_point& rhs)
- {
- return (lhs.x == rhs.x);
- }
- inline friend bool operator!=(const floating_point& lhs, const floating_point& rhs)
- {
- return !(lhs == rhs);
- }
- friend std::ostream& operator << (std::ostream& stream, const floating_point& p)
- {
- stream << p.x;
- return stream;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement