summaryrefslogtreecommitdiffstats
path: root/src/datablocks/mixednumber.cpp
blob: 17a90475f96d2a73e0a2d21579bfe9e3ba5ce496 (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/***************************************************************************
*   Copyright (C) 2003 by krecipes.sourceforge.net authors                *
*                                                                         *
*                                                                         *
*   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.                                   *
***************************************************************************/
#include "mixednumber.h"

#include <tqregexp.h>

#include <tdeglobal.h>
#include <tdelocale.h>
#include <kdebug.h>

TQString beautify( const TQString &num )
{
	TQString copy( num );
	copy.remove( TQRegExp( TQString( "(%1){0,1}0+$" ).arg( TQRegExp::escape( TDEGlobal::locale() ->decimalSymbol() ) ) ) );
	return copy;
}

MixedNumber::MixedNumber() :
		m_whole( 0 ),
		m_numerator( 0 ),
		m_denominator( 1 ),
		locale( TDEGlobal::locale() )
{}

MixedNumber::MixedNumber( int whole, int numerator, int denominator ) :
		m_whole( whole ),
		m_numerator( numerator ),
		m_denominator( denominator ),
		locale( TDEGlobal::locale() )
{}

MixedNumber::MixedNumber( double decimal, double precision ) :
		locale( TDEGlobal::locale() )
{
	// find nearest fraction
	int intPart = static_cast<int>( decimal );
	decimal -= static_cast<double>( intPart );

	MixedNumber low( 0, 0, 1 );     // "A" = 0/1
	MixedNumber high( 0, 1, 1 );    // "B" = 1/1

	for ( int i = 0; i < 100; ++i ) {
		double testLow = low.denominator() * decimal - low.numerator();
		double testHigh = high.numerator() - high.denominator() * decimal;

		if ( testHigh < precision * high.denominator() )
			break; // high is answer
		if ( testLow < precision * low.denominator() ) {  // low is answer
			high = low;
			break;
		}

		if ( i & 1 ) {  // odd step: add multiple of low to high
			double test = testHigh / testLow;
			int count = ( int ) test; // "N"
			int num = ( count + 1 ) * low.numerator() + high.numerator();
			int denom = ( count + 1 ) * low.denominator() + high.denominator();

			if ( ( num > 0x8000 ) || ( denom > 0x10000 ) )
				break;

			high.setNumerator( num - low.numerator() ); // new "A"
			high.setDenominator( denom - low.denominator() );
			low.setNumerator( num ); // new "B"
			low.setDenominator( denom );
		}
		else {  // even step: add multiple of high to low
			double test = testLow / testHigh;
			int count = ( int ) test; // "N"
			int num = low.numerator() + ( count + 1 ) * high.numerator();
			int denom = low.denominator() + ( count + 1 ) * high.denominator();

			if ( ( num > 0x10000 ) || ( denom > 0x10000 ) )
				break;

			low.setNumerator( num - high.numerator() ); // new "A"
			low.setDenominator( denom - high.denominator() );
			high.setNumerator( num ); // new "B"
			high.setDenominator( denom );
		}
	}

	m_numerator = high.numerator();
	m_denominator = high.denominator();
	m_whole = intPart;
}

MixedNumber::~MixedNumber()
{}

int MixedNumber::getNumerator( const TQString &input, int space_index, int slash_index, bool *ok )
{
	return input.mid( space_index + 1, slash_index - space_index - 1 ).toInt( ok );
}

int MixedNumber::getDenominator( const TQString &input, int slash_index, bool *ok )
{
	return input.mid( slash_index + 1, input.length() ).toInt( ok );
}

MixedNumber MixedNumber::fromString( const TQString &str, bool *ok, bool locale_aware )
{
	TQString input = str.stripWhiteSpace();
	if ( input.isEmpty() ) {
		if ( ok ) {
			*ok = true;
		}
		return MixedNumber();
	}

	TDELocale *locale = TDEGlobal::locale();

	bool num_ok;

	int whole;
	int numerator;
	int denominator;

	int space_index = input.find( " " );
	int slash_index = input.find( "/" );

	if ( space_index == -1 ) {
		if ( slash_index == -1 )  //input contains no fractional part
		{
			TQString decimal_symbol = ( locale_aware ) ? locale->decimalSymbol() : ".";
			if ( input.endsWith( decimal_symbol ) )
			{
				if ( ok ) {
					*ok = false;
				}
				return MixedNumber();
			}

			double decimal = ( locale_aware ) ? locale->readNumber( input, &num_ok ) : input.toDouble( &num_ok );

			if ( !num_ok )
			{
				if ( ok ) {
					*ok = false;
				}
				return MixedNumber();
			}

			if ( ok )
			{
				*ok = true;
			}
			return MixedNumber( decimal );
		}
		else //input just contains a fraction
		{
			whole = 0;

			numerator = MixedNumber::getNumerator( input, space_index, slash_index, &num_ok );
			if ( !num_ok ) {
				if ( ok ) {
					*ok = false;
				}
				return MixedNumber();
			}

			denominator = MixedNumber::getDenominator( input, slash_index, &num_ok );
			if ( !num_ok || denominator == 0 ) {
				if ( ok ) {
					*ok = false;
				}
				return MixedNumber();
			}

			if ( ok ) {
				*ok = true;
			}
			return MixedNumber( whole, numerator, denominator );
		}
		if ( ok ) {
			*ok = false;
		}
		return MixedNumber();
	}

	whole = input.mid( 0, space_index ).toInt( &num_ok );
	if ( !num_ok ) {
		if ( ok ) {
			*ok = false;
		}
		return MixedNumber();
	}

	numerator = MixedNumber::getNumerator( input, space_index, slash_index, &num_ok );
	if ( !num_ok ) {
		if ( ok ) {
			*ok = false;
		}
		return MixedNumber();
	}

	denominator = MixedNumber::getDenominator( input, slash_index, &num_ok );
	if ( !num_ok || denominator == 0 ) {
		if ( ok ) {
			*ok = false;
		}
		return MixedNumber();
	}

	if ( ok ) {
		*ok = true;
	}
	return MixedNumber( whole, numerator, denominator );
}

TQString MixedNumber::toString( Format format, bool locale_aware ) const
{
	if ( format == DecimalFormat ) {
		if ( locale_aware )
			return beautify( locale->formatNumber( toDouble(), 5 ) );
		else
			return TQString::number( toDouble() );
	}

	if ( m_numerator == 0 && m_whole == 0 )
		return TQString( "0" );


	TQString result;

	if ( m_whole != 0 ) {
		result += TQString::number( m_whole );
		if ( m_numerator != 0 )
			result += " ";
	}

	if ( m_numerator != 0 )
		result += TQString::number( m_numerator ) + "/" + TQString::number( m_denominator );

	return result;
}

bool MixedNumber::operator!=( const MixedNumber &fraction )
{
	return ( fraction.toDouble() != toDouble() );
}

MixedNumber& MixedNumber::operator+=( const MixedNumber &fraction )
{
	m_numerator = ( m_numerator * fraction.m_denominator ) + ( m_denominator * fraction.m_numerator );
	m_denominator = m_denominator * fraction.m_denominator;
	m_whole += fraction.m_whole;
	simplify();

	return *this;
}

MixedNumber& MixedNumber::operator+=( double d )
{
	MixedNumber mn(d);
	*this += mn;
	return *this;
}

void MixedNumber::simplify()
{
	int divisor = gcd( m_numerator, m_denominator );
	m_numerator /= divisor;
	m_denominator /= divisor;
}

double MixedNumber::toDouble() const
{
	return static_cast<double>( m_whole ) + ( static_cast<double>( m_numerator ) / static_cast<double>( m_denominator ) );
}

int MixedNumber::gcd( int n, int m )
{
	int r;
	while ( n != 0 ) {
		r = m % n;
		m = n;
		n = r;
	}

	return m;
}