]> git.donarmstrong.com Git - lilypond.git/blobdiff - flower/rational.cc
* scm/define-grobs.scm (all-grob-descriptions): use callback to
[lilypond.git] / flower / rational.cc
index ec39ad307c726d3d768339e994fbc0817923d9e0..a0c0d22410e235726eeee037b760fee1f7dcaef9 100644 (file)
@@ -3,20 +3,26 @@
 
   source file of the Flower Library
 
-  (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
 */
 
 #include "rational.hh"
 
 #include <cmath>
 #include <cstdlib>
+using namespace std;
 
 #include "string-convert.hh"
 #include "libc-extension.hh"
 
 Rational::operator double () const
 {
-  return (double)sign_ * num_ / den_;
+  return ((double)sign_) * num_ / den_;
+}
+
+Rational::operator bool () const
+{
+  return sign_ && num_;
 }
 
 #ifdef STREAM_SUPPORT
@@ -165,8 +171,9 @@ Rational::operator += (Rational r)
     *this = r;
   else
     {
-      int n = sign_ * num_ * r.den_ + r.sign_ * den_ * r.num_;
-      int d = den_ * r.den_;
+      int lcm = (den_ / gcd (r.den_, den_)) * r.den_;
+      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);
@@ -262,16 +269,16 @@ Rational::operator -= (Rational r)
   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;
@@ -280,7 +287,7 @@ Rational::to_string () const
 int
 Rational::to_int () const
 {
-  return num () / den ();
+  return (int) num () / den ();
 }
 
 int