File: rational.cc

package info (click to toggle)
ocrad 0.16-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 588 kB
  • ctags: 686
  • sloc: cpp: 7,741; makefile: 151; sh: 88
file content (226 lines) | stat: -rw-r--r-- 5,904 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*  GNU Ocrad - Optical Character Recognition program
    Copyright (C) 2003, 2004, 2005, 2006 Antonio Diaz Diaz.

    This program 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 2 of the License, or
    (at your option) any later version.

    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <algorithm>
#include <cctype>
#include <climits>
#include <cstdlib>
#include <string>

#include "rational.h"


namespace {

int gcd( int n, int m ) throw()		// Greatest Common Divisor
  {
  if( n < 0 ) n = -n;
  if( m < 0 ) m = -m;

  while( true )
    {
    if( m ) n %= m; else return n;
    if( n ) m %= n; else return m;
    }
  }


int lcm( int n, int m ) throw()		// Least Common Multiple
  {
  if( !n || !m ) return 0;

  n /= gcd( n, m ); n *= m;	// lcm( n, m ) == ( n * m ) / gcd( n, m )
  if( n < 0 ) n = -n;
  return n;
  }

} // end namespace


void Rational::normalize() throw()
  {
  if( num == 0 ) { den = 1; return; }
  if( den == 0 )
    { den = 1; if( num > 0 ) num = INT_MAX; else num = INT_MIN; return; }
  if( den < 0 ) { num = -num; den = -den; }

  if( den != 1 )
    {
    const int tmp = gcd( num, den );
    num /= tmp; den /= tmp;
    }
  }


Rational & Rational::operator+=( const Rational & r ) throw()
  {
  const int tmp = lcm( den, r.den );
  num = ( ( tmp / den ) * num ) + ( ( tmp / r.den ) * r.num );
  den = tmp;
  return *this;
  }


Rational & Rational::operator*=( const Rational & r ) throw()
  {
  const int tmp1 = gcd( num, r.den );
  const int tmp2 = gcd( r.num, den );
  num = ( num / tmp1 ) * ( r.num / tmp2 );
  den = ( den / tmp2 ) * ( r.den / tmp1 );
  normalize();				// overflow may break the invariant
  return *this;
  }


Rational & Rational::operator/=( const Rational & r ) throw()
  {
  if( num )
    {
    if( r.num == 0 ) den = 0;
    else
      {
      const int tmp1 = gcd( num, r.num );
      const int tmp2 = gcd( den, r.den );
      num = ( num / tmp1 ) * ( r.den / tmp2 );
      den = ( den / tmp2 ) * ( r.num / tmp1 );
      }
    }
  normalize();
  return *this;
  }


bool Rational::operator<( const Rational & r ) const throw()
  {
  if( num >= 0 && r.num <= 0 ) return false;
  if( ( num <= 0 && r.num > 0 ) || ( num < 0 && r.num >= 0 ) ) return true;

  int tmp1 = num / den, tmp2 = r.num / r.den;	// both values have same sign
  if( tmp1 != tmp2 ) return ( tmp1 < tmp2 );

  tmp1 = gcd( num, r.num );		// values differ by less than 1
  tmp2 = gcd( r.den, den );
  return ( ( num / tmp1 ) * ( r.den / tmp2 ) < ( den / tmp2 ) * ( r.num / tmp1 ) );
  }


Rational Rational::inverse() const throw()
  {
  Rational tmp( den );
  if( num == 0 ) { tmp.num = INT_MAX; tmp.den = 1; }
  else if( num < 0 ) { tmp.num = -den; tmp.den = -num; }
  else tmp.den = num;
  return tmp;
  }


int Rational::round() const throw()
  {
  int result = num / den, rest = std::abs( num ) % den;
  if( rest > 0 && rest >= den - rest )
    { if( num >= 0 ) ++result; else --result; }
  return result;
  }


// Recognized formats: 123 123/456 123.456 .123 12% 12/3% 12.3% .12%
// Values may be preceded by an optional '+' or '-' sign.
// Returns the number of chars read, or 0 if error.
//
int Rational::parse( const char * ptr ) throw()
  {
  if( !ptr || !*ptr ) return 0;
  int n = 0, d = 1, c = 0;
  bool minus = false;

  while( std::isspace( ptr[c] ) ) ++c;
  if( ptr[c] == '+' ) ++c;
  else if( ptr[c] == '-' ) { ++c; minus = true; }
  if( !std::isdigit( ptr[c] ) && ptr[c] != '.' ) return 0;

  while( std::isdigit( ptr[c] ) )
    {
    if( ( INT_MAX - (ptr[c] - '0') ) / 10 < n ) return 0;
    n = (n * 10) + (ptr[c] - '0'); ++c;
    }

  if( ptr[c] == '.' )
    {
    ++c; if( !std::isdigit( ptr[c] ) ) return 0;
    while( std::isdigit( ptr[c] ) )
      {
      if( ( INT_MAX - (ptr[c] - '0') ) / 10 < n || INT_MAX / 10 < d ) return 0;
      n = (n * 10) + (ptr[c] - '0'); d *= 10; ++c;
      }
    }
  else if( ptr[c] == '/' )
    {
    ++c; d = 0;
    while( std::isdigit( ptr[c] ) )
      {
      if( ( INT_MAX - (ptr[c] - '0') ) / 10 < d ) return 0;
      d = (d * 10) + (ptr[c] - '0'); ++c;
      }
    if( d == 0 ) return 0;
    }

  if( ptr[c] == '%' )
    {
    ++c;
    if( n % 100 == 0 ) n /= 100;
    else if( n % 10 == 0 && INT_MAX / 10 >= d ) { n /= 10; d *= 10; }
    else if( INT_MAX / 100 >= d ) d *= 100;
    else return 0;
    }

  if( minus ) n = -n;
  num = n; den = d; normalize();
  return c;
  }


// Returns the fraction "num/den" as a floating point with "prec" decimals.
// If 'prec' is negative, only the needed decimals are shown.
//
const std::string Rational::to_decimal( const int iwidth, int prec ) const throw()
  {
  std::string s;

  if( den == 0 )
    {
    if( num == 0 ) s = "NAN"; else if( num > 0 ) s = "+INF"; else s = "-INF";
    return s;
    }
  bool negative = false, trunc = false;
  int ipart = num / den;
  if( ipart < 0 ) { ipart = -ipart; negative = true; }
  if( prec < 0 ) { prec = -prec; trunc = true; }

  do { s += '0' + ( ipart % 10 ); ipart /= 10; } while( ipart > 0 );
  if( negative ) s += '-';
  if( iwidth > (int)s.size() ) s.append( iwidth - s.size(), ' ' );
  std::reverse( s.begin(), s.end() );
  long long rest = std::abs( num ) % den;
  if( prec > 0 && ( rest > 0 || !trunc ) )
    {
    s += '.';
    while( prec > 0 && ( rest > 0 || !trunc ) )
      { rest *= 10; s += '0' + ( rest / den ); rest %= den; --prec; }
    }
  return s;
  }