source file of the Flower Library
- (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+ (c) 1997--2008 Han-Wen Nienhuys <hanwen@xs4all.nl>
*/
#include "rational.hh"
#include <cmath>
+#include <cassert>
#include <cstdlib>
+using namespace std;
#include "string-convert.hh"
#include "libc-extension.hh"
-Rational::operator double () const
+double
+Rational::to_double () const
{
- return (double)sign_ * num_ / den_;
+ if (sign_ == -1 || sign_ == 1 || sign_ == 0)
+ return ((double)sign_) * num_ / den_;
+ if (sign_ == -2)
+ return -HUGE_VAL;
+ else if (sign_ == 2)
+ return HUGE_VAL;
+ else
+ assert (false);
+
+ return 0.0;
}
+
#ifdef STREAM_SUPPORT
ostream &
-operator<< (ostream &o, Rational r)
+operator << (ostream &o, Rational r)
{
o << r.string ();
return o;
}
#endif
+Rational
+Rational::abs () const
+{
+ return Rational (num_, den_);
+}
+
Rational
Rational::trunc_rat () const
{
- return Rational (num_ - (num_ % den_), den_);
+ if (is_infinity())
+ return *this;
+ return Rational ((num_ - (num_ % den_)) * sign_, den_);
}
Rational::Rational ()
num_ = den_ = 1;
}
-Rational::Rational (int n, int d)
+Rational::Rational (I64 n, I64 d)
{
sign_ = ::sign (n) * ::sign (d);
- num_ = abs (n);
- den_ = abs (d);
- normalise ();
+ num_ = ::abs (n);
+ den_ = ::abs (d);
+ normalize ();
}
-Rational::Rational (int n)
+Rational::Rational (I64 n)
{
sign_ = ::sign (n);
- num_ = abs (n);
- den_= 1;
+ num_ = ::abs (n);
+ den_ = 1;
}
-static inline
-int gcd (int a, int b)
+Rational::Rational (U64 n)
{
- int t;
- while ((t = a % b))
- {
- a = b;
- b = t;
- }
- return b;
+ sign_ = 1;
+ num_ = n;
+ den_ = 1;
}
+Rational::Rational (int n)
+{
+ sign_ = ::sign (n);
+ num_ = ::abs (n);
+ den_ = 1;
+}
+
+
void
Rational::set_infinite (int s)
{
sign_ = ::sign (s) * 2;
+ num_ = 1;
}
Rational
-Rational::operator- () const
+Rational::operator - () const
{
Rational r (*this);
r.negate ();
return r;
}
+
+/*
+ copy & paste from scm_gcd (GUILE).
+ */
+static I64
+gcd (I64 u, I64 v)
+{
+ I64 result = 0;
+ if (u == 0)
+ result = v;
+ else if (v == 0)
+ result = u;
+ else
+ {
+ I64 k = 1;
+ I64 t;
+ /* Determine a common factor 2^k */
+ while (!(1 & (u | v)))
+ {
+ k <<= 1;
+ u >>= 1;
+ v >>= 1;
+ }
+ /* Now, any factor 2^n can be eliminated */
+ if (u & 1)
+ t = -v;
+ else
+ {
+ t = u;
+ b3:
+ t = t >> 1;
+ }
+ if (!(1 & t))
+ goto b3;
+ if (t > 0)
+ u = t;
+ else
+ v = -t;
+ t = u - v;
+ if (t != 0)
+ goto b3;
+ result = u * k;
+ }
+
+ return result;
+}
+
+
void
-Rational::normalise ()
+Rational::normalize ()
{
if (!sign_)
{
}
else
{
- int g = gcd (num_, den_);
+ I64 g = gcd (num_, den_);
num_ /= g;
den_ /= g;
return 0;
else if (r.sign_ == 0)
return 0;
- else
- {
- return r.sign_ * ::sign (int (r.num_ * s.den_) - int (s.num_ * r.den_));
- }
+ return r.sign_ * ::sign ((I64) (r.num_ * s.den_) - (I64) (s.num_ * r.den_));
}
int
}
Rational &
-Rational::operator%= (Rational r)
+Rational::operator %= (Rational r)
{
- *this = r.mod_rat (r);
+ *this = mod_rat (r);
return *this;
}
Rational &
-Rational::operator+= (Rational r)
+Rational::operator += (Rational r)
{
- if (is_infinity ());
+ if (is_infinity ())
+ ;
else if (r.is_infinity ())
- {
- *this = r;
- }
+ *this = r;
else
{
- int n = sign_ * num_ *r.den_ + r.sign_ * den_ * r.num_;
- int d = den_ * r.den_;
+ I64 lcm = (den_ / gcd (r.den_, den_)) * r.den_;
+ I64 n = sign_ * num_ * (lcm / den_) + r.sign_ * r.num_ * (lcm / r.den_);
+ I64 d = lcm;
sign_ = ::sign (n) * ::sign (d);
- num_ = abs (n);
- den_ = abs (d);
- normalise ();
+ num_ = ::abs (n);
+ den_ = ::abs (d);
+ normalize ();
}
return *this;
}
easily.
*/
- num_ = (unsigned int) (mantissa * FACT);
- den_ = (unsigned int) FACT;
- normalise ();
+ num_ = (U64) (mantissa * FACT);
+ den_ = (U64) FACT;
+ normalize ();
if (expt < 0)
den_ <<= -expt;
else
num_ <<= expt;
- normalise ();
+ normalize ();
}
else
{
num_ = 0;
den_ = 1;
- sign_ =0;
- normalise ();
+ sign_ = 0;
+ normalize ();
}
}
void
Rational::invert ()
{
- int r (num_);
+ I64 r (num_);
num_ = den_;
den_ = r;
}
Rational &
-Rational::operator*= (Rational r)
+Rational::operator *= (Rational r)
{
sign_ *= ::sign (r.sign_);
if (r.is_infinity ())
num_ *= r.num_;
den_ *= r.den_;
- normalise ();
+ normalize ();
exit_func:
return *this;
}
Rational &
-Rational::operator/= (Rational r)
+Rational::operator /= (Rational r)
{
r.invert ();
return (*this *= r);
}
Rational &
-Rational::operator-= (Rational r)
+Rational::operator -= (Rational r)
{
r.negate ();
return (*this += r);
}
-String
+string
Rational::to_string () const
{
if (is_infinity ())
{
- String s (sign_ > 0 ? "" : "-");
- return String (s + "infinity");
+ string s (sign_ > 0 ? "" : "-");
+ return string (s + "infinity");
}
- String s = ::to_string (num ());
+ string s = ::to_string (num ());
if (den () != 1 && num ())
s += "/" + ::to_string (den ());
return s;
int
Rational::to_int () const
{
- return num () / den ();
+ return (int) num () / den ();
}
int