]> git.donarmstrong.com Git - lilypond.git/blobdiff - flower/rational.cc
Web-ja: update introduction
[lilypond.git] / flower / rational.cc
index 65672f5bcbb3547e1553a54c9d9e8388f4460c3e..559e1646a0cade47b932d7cfab048f0feb641da8 100644 (file)
@@ -1,28 +1,45 @@
 /*
-  rational.cc -- implement Rational
+  This file is part of LilyPond, the GNU music typesetter.
 
-  source file of the Flower Library
+  Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
 
-  (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  LilyPond is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  LilyPond is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #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_ * (double)num_ / (double)den_;
+  if (sign_ == -2)
+    return -HUGE_VAL;
+  else if (sign_ == 2)
+    return HUGE_VAL;
+  else
+    assert (false);
 
-Rational::operator bool () const
-{
-  return sign_ && num_;
+  return 0.0;
 }
 
 #ifdef STREAM_SUPPORT
@@ -34,10 +51,18 @@ operator << (ostream &o, Rational r)
 }
 #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 ()
@@ -46,37 +71,40 @@ 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);
+  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
@@ -103,8 +131,54 @@ Rational::mod_rat (Rational div) const
   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_)
     {
@@ -123,7 +197,7 @@ Rational::normalise ()
     }
   else
     {
-      int g = gcd (num_, den_);
+      I64 g = gcd (num_, den_);
 
       num_ /= g;
       den_ /= g;
@@ -142,11 +216,11 @@ Rational::compare (Rational const &r, Rational const &s)
     return -1;
   else if (r.sign_ > s.sign_)
     return 1;
-  else if (r.is_infinity ())
+  else if (r.is_infinity ()) // here s is also infinite with the same sign
     return 0;
-  else if (r.sign_ == 0)
+  else if (r.sign_ == 0) // here s.sign_ is also zero
     return 0;
-  return r.sign_ * ::sign (int (r.num_ * s.den_) - int (s.num_ * r.den_));
+  return ::sign (r - s);
 }
 
 int
@@ -171,13 +245,13 @@ Rational::operator += (Rational r)
     *this = r;
   else
     {
-      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;
+      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;
 }
@@ -198,35 +272,35 @@ Rational::Rational (double x)
       const int FACT = 1 << 20;
 
       /*
-       Thanks to Afie for this too simple  idea.
+        Thanks to Afie for this too simple  idea.
 
-       do not blindly substitute by libg++ code, since that uses
-       arbitrary-size integers.  The rationals would overflow too
-       easily.
+        do not blindly substitute by libg++ code, since that uses
+        arbitrary-size integers.  The rationals would overflow too
+        easily.
       */
 
-      num_ = (unsigned int) (mantissa * FACT);
-      den_ = (unsigned int) FACT;
-      normalise ();
+      num_ = (U64) (mantissa * FACT);
+      den_ = (U64) FACT;
+      normalize ();
       if (expt < 0)
-       den_ <<= -expt;
+        den_ <<= -expt;
       else
-       num_ <<= expt;
-      normalise ();
+        num_ <<= expt;
+      normalize ();
     }
   else
     {
       num_ = 0;
       den_ = 1;
       sign_ = 0;
-      normalise ();
+      normalize ();
     }
 }
 
 void
 Rational::invert ()
 {
-  int r (num_);
+  I64 r (num_);
   num_ = den_;
   den_ = r;
 }
@@ -244,8 +318,8 @@ Rational::operator *= (Rational r)
   num_ *= r.num_;
   den_ *= r.den_;
 
-  normalise ();
- exit_func:
+  normalize ();
+exit_func:
   return *this;
 }
 
@@ -269,16 +343,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;
@@ -287,7 +361,7 @@ Rational::to_string () const
 int
 Rational::to_int () const
 {
-  return (int) num () / den ();
+  return (int) (num () / den ());
 }
 
 int