From 523fe57eb6be805b4184ff6b59ff5c85cd30351d Mon Sep 17 00:00:00 2001 From: Han-Wen Nienhuys Date: Fri, 22 Dec 2006 16:28:49 +0100 Subject: [PATCH] rational cleanups: suppress some automatic conversions. --- flower/include/rational.hh | 7 ++++--- flower/rational.cc | 39 +++++++++++++++++--------------------- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/flower/include/rational.hh b/flower/include/rational.hh index 290d02b960..bc64465cfe 100644 --- a/flower/include/rational.hh +++ b/flower/include/rational.hh @@ -28,7 +28,7 @@ class Rational */ int sign_; unsigned int num_, den_; - void normalise (); + void normalize (); void copy (Rational const &); public: @@ -43,9 +43,10 @@ public: Rational trunc_rat () const; Rational div_rat (Rational) const; Rational mod_rat (Rational) const; + Rational abs () const; void negate (); int to_int () const; - operator bool () const; + operator double () const; Rational operator - () const; @@ -53,7 +54,6 @@ public: Initialize to 0. */ Rational (); - Rational (int); Rational (int, int); Rational (double); Rational (Rational const &r) { copy (r);} @@ -73,6 +73,7 @@ public: }; #include "arithmetic-operator.hh" + IMPLEMENT_ARITHMETIC_OPERATOR (Rational, /); IMPLEMENT_ARITHMETIC_OPERATOR (Rational, +); IMPLEMENT_ARITHMETIC_OPERATOR (Rational, *); diff --git a/flower/rational.cc b/flower/rational.cc index b82a35e667..150ace0359 100644 --- a/flower/rational.cc +++ b/flower/rational.cc @@ -30,10 +30,6 @@ Rational::operator double () const return 0.0; } -Rational::operator bool () const -{ - return sign_ && num_; -} #ifdef STREAM_SUPPORT ostream & @@ -44,6 +40,12 @@ operator << (ostream &o, Rational r) } #endif +Rational +Rational::abs () const +{ + return Rational (num_, den_); +} + Rational Rational::trunc_rat () const { @@ -59,16 +61,9 @@ Rational::Rational () Rational::Rational (int n, int d) { sign_ = ::sign (n) * ::sign (d); - num_ = abs (n); - den_ = abs (d); - normalise (); -} - -Rational::Rational (int n) -{ - sign_ = ::sign (n); - num_ = abs (n); - den_ = 1; + num_ = ::abs (n); + den_ = ::abs (d); + normalize (); } static inline @@ -114,7 +109,7 @@ Rational::mod_rat (Rational div) const } void -Rational::normalise () +Rational::normalize () { if (!sign_) { @@ -185,9 +180,9 @@ Rational::operator += (Rational r) int n = sign_ * num_ * (lcm / den_) + r.sign_ * r.num_ * (lcm / r.den_); int d = lcm; sign_ = ::sign (n) * ::sign (d); - num_ = abs (n); - den_ = abs (d); - normalise (); + num_ = ::abs (n); + den_ = ::abs (d); + normalize (); } return *this; } @@ -217,19 +212,19 @@ Rational::Rational (double x) num_ = (unsigned int) (mantissa * FACT); den_ = (unsigned int) FACT; - normalise (); + normalize (); if (expt < 0) den_ <<= -expt; else num_ <<= expt; - normalise (); + normalize (); } else { num_ = 0; den_ = 1; sign_ = 0; - normalise (); + normalize (); } } @@ -254,7 +249,7 @@ Rational::operator *= (Rational r) num_ *= r.num_; den_ *= r.den_; - normalise (); + normalize (); exit_func: return *this; } -- 2.39.2