-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFraction.h
executable file
·276 lines (214 loc) · 11.2 KB
/
Fraction.h
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
//////////////////////////////////////////////////////////////////////
/// @file Fraction.h
/// @brief Interface of the CFraction class - Code for handling fraction strings
///
/// \author Dean Wyant dwyant@mindspring.com
//////////////////////////////////////////////////////////////////////
/// This class is used like a double. Do anything you would do with a double,
/// plus assign strings to it. When you need to get a string representation, use one of the
/// String calls. My favorite is ForceToStockString which returns a string for the closest
/// valid stock transaction denominator up to MaxDen (default 256). This is great for
/// applications that need to show stock prices in fractions without resorting to a table
/// lookup or showing unusual prices in decimal.
///
/// C calls and plain calls for ASCIIZ support are provided
///
/// A description of the \ref Fraction is also given.
//////////////////////////////////////////////////////////////////////
#ifndef __FRACTION_H__
#define __FRACTION_H__
// If you are not using MFC or CString you can remark out this define
#define FRAC_CSTRING
//////////////////////////////////////////////////////////////////////
/// Significant digits
/// \ingroup CFractionGroup
//////////////////////////////////////////////////////////////////////
#define FRACTION_MAX_PRECISION 10.0e-15
double Abs(double f); // Internal use to avoid <math.h>
// define group
/*!
\defgroup CFractionGroup CFraction Library : Code for handling fraction strings
*/
// C support
//////////////////////////////////////////////////////////////////////
/// Convert string that may include a fraction to floating point (double)
/// \ingroup CFractionGroup
///
/// \param pszNum todo
/// \return double
//////////////////////////////////////////////////////////////////////
double Fracatof(const char *pszNum);
//////////////////////////////////////////////////////////////////////
/// \ingroup CFractionGroup
/// Convert double to a string that includes the whole part, space, and the fraction (numerator/denominator).
/// If you want to get a Num and Den for a fractional part less than the default AllowedError,
/// pass an allowed error less than the fractional part. Otherwise the default should be fine.
//////////////////////////////////////////////////////////////////////
char *Fracftoa(double f, char *pszBuffer, double AllowedError = FRACTION_MAX_PRECISION);
//////////////////////////////////////////////////////////////////////
/// \ingroup CFractionGroup
/// Convert double to a string that includes the whole part, space, and the fraction (numerator/denominator).
/// Limits the denominator to MaxDen
//////////////////////////////////////////////////////////////////////
char *Fracftoa(double f, char *pszBuffer, int MaxDen);
//////////////////////////////////////////////////////////////////////
/// \ingroup CFractionGroup
/// Returns %6.4f unless denominator is less than or equal to MaxDen
/// and in 2,4,8,16,32,64,128,256
//////////////////////////////////////////////////////////////////////
char *DoubleToStockString(double f, char *pszBuffer, int MaxDen = 256);
//////////////////////////////////////////////////////////////////////
/// \ingroup CFractionGroup
/// Returns closest fraction string with a denominator less than or equal to MaxDen
/// and in 2,4,8,16,32,64,128,256
//////////////////////////////////////////////////////////////////////
char *ForceDoubleToStockString(double f, char *pszBuffer, int MaxDen = 256);
//////////////////////////////////////////////////////////////////////
/// \ingroup CFractionGroup
/// Convert a double to its fractional parts. Returns Error difference.
/// If you want to get a Num and Den for a fractional part less than the default AllowedError,
/// pass an allowed error less than the fractional part. Otherwise the default should be fine.
//////////////////////////////////////////////////////////////////////
double FracParts(double f, __int64 &Whole, __int64 &Num, __int64 &Den, double AllowedError = FRACTION_MAX_PRECISION);
//////////////////////////////////////////////////////////////////////
/// \ingroup CFractionGroup
/// Convert a double to its fractional parts. Returns Error difference.
/// Limits the denominator to MaxDen
//////////////////////////////////////////////////////////////////////
double FracParts(double f, __int64 &Whole, __int64 &Num, __int64 &Den, int MaxDen);
//////////////////////////////////////////////////////////////////////
/// \ingroup CFractionGroup
/// Convert a double to its fractional parts. Returns Error difference.
/// Limits the denominator to MaxDen and always returns Den as 2,4,8,16,32,64,128,256
//////////////////////////////////////////////////////////////////////
double FracPartsStock(double f, __int64 &Whole, __int64 &Num, __int64 &Den, int MaxDen);
//#ifdef __cplusplus
/////////////////////////////////////////////////////////////////////////////
/// CFraction - Code for handling fraction strings
///
/// \author Dean Wyant dwyant@mindspring.com
/// \ingroup CFractionGroup
/////////////////////////////////////////////////////////////////////////////
///
/// CFraction - This class is used like a double. Do anything you would do with a double,
/// plus assign strings to it. When you need to get a string representation, use one of the
/// String calls. My favorite is ForceToStockString which returns a string for the closest
/// valid stock transaction denominator up to MaxDen (default 256). This is great for
/// applications that need to show stock prices in fractions without resorting to a table
/// lookup or showing unusual prices in decimal.
///
/// C calls and plain calls for ASCIIZ support are provided.
///
/// A description of the \ref Fraction is also given.
/////////////////////////////////////////////////////////////////////////////
class CFraction
{
public:
CFraction(const double d = 0) { m_d = d; }
CFraction(const char *pszNum) { m_d = Fracatof(pszNum); }
operator double() const { return m_d; }
// String calls
#ifdef FRAC_CSTRING // CString calls
CFraction(const CString& S) { m_d = Fracatof(S); }
//////////////////////////////////////////////////////////////////////
/// See ::Fracftoa
//////////////////////////////////////////////////////////////////////
CString ToString(double AllowedError = FRACTION_MAX_PRECISION);
//////////////////////////////////////////////////////////////////////
/// See ::Fracftoa
//////////////////////////////////////////////////////////////////////
CString ToString(int MaxDen);
//////////////////////////////////////////////////////////////////////
/// See ::DoubleToStockString
//////////////////////////////////////////////////////////////////////
CString ToStockString(int MaxDen = 256);
//////////////////////////////////////////////////////////////////////
/// See ::ForceDoubleToStockString
//////////////////////////////////////////////////////////////////////
CString ForceToStockString(int MaxDen = 256);
#endif
// ASCIIZ string support
char *ToStr(char *pszBuffer, double AllowedError = FRACTION_MAX_PRECISION);
char *ToStr(char *pszBuffer, int MaxDen);
// See Fracftoa above. Puts string in fraction format into pszBuffer
char *ToStockStr(char *pszBuffer, int MaxDen = 256); // See DoubleToStockString above.
char *ForceToStockStr(char *pszBuffer, int MaxDen = 256); // See ForceDoubleToStockString above.
// Mostly used internally, but you may need them?
double Parts(__int64 &Whole, __int64 &Num, __int64 &Den, double AllowedError = FRACTION_MAX_PRECISION);
double Parts(__int64 &Whole, __int64 &Num, __int64 &Den, int MaxDen);
// See FracParts above
private:
double m_d; ///< Internal storage for the double
};
//#endif //__cplusplus
/**
\page Fraction Fraction Algorithm
\ingroup CFractionGroup
The formula for converting a decimal fraction to a string fraction is based on
reciprocals. Any fractional part can be represented as 1/(I1 + 1/(I2 + 1/(I3 + 1/In...
where I1 is the integer part of the reciprocal of the fraction and I2 is the
integer part of the reciprocal minus I1... etc.
To determine the integer numerator and denominator, the expression can be reduced.
It turns out that the numerator is simpler to calculate than the denominator, so the
denominator can simply be determined by dividing the numerator by the original decimal.
\verbatim
Reduction of 1/(I1 + 1/(I2 + 1/(I3 + 1/In... -
1 I term:
1
-- Numerator N1 = 1
I1
2 I terms:
1 I2
----- Multiply by I2 = -----------
I1 + 1 to reduce -- (I1*I2) + 1
-- I2
I2
Numerator N2 = I2
3 I terms
1 1 (I2*I3) + 1
----- = ------- = ---------------------
I1 + 1 I1 + I3 I1*((I2*I3) + 1) + I3
------ -----------
I2 + 1 (I2*I3) + 1
--
I3
Numerator N3 = N2*I3 + N1
4 I terms
1 1 1 I2*((I3*I4) + 1) + I4
------ -------- = ---------------- = -----------------------------------------
I1 + 1 I1 + 1 I1 + ((I3*I4) + 1) I1*(I2*((I3*I4) + 1) + I4) + ((I3*I4) + 1)
---- = ------- ---------------------
I2 + 1 I2 + I4 I2*((I3*I4) + 1) + I4
------- -----------
I3 + 1 (I3*I4) + 1
--
I4
Numerator N4 = I4*(I3*I2 + 1) + I2 = N3*I4 + N2
5 I terms
1 1 1 1
------ ------ ------ --------------
I1 + 1 I1 + 1 I1 + 1 I1 + (I3*((I4*I5) + 1) + I5)
------ = ------ = ---------------- -----------------------
I2 + 1 I2 + 1 I2 + (I4*I5) + 1 I2*(I3*((I4*I5) + 1) + I5) + (I4*I5) + 1
------- ------- ---------------------
I3 + 1 I3 + I5 I3*((I4*I5) + 1) + I5
------- ------------
I4 + 1 (I4*I5) + 1
--
I5
= I2*(I3*((I4*I5) + 1) + I5) + (I4*I5) + 1
----------------------------------------------------------------------
I1*(I2*(I3*((I4*I5) + 1) + I5) + (I4*I5) + 1) + (I3*((I4*I5) + 1) + I5)
Numerator N5 = I5*(I4*(I3*I2 + 1) + I2) + I3*I2 + 1 = N4*I5 + N3
\endverbatim
So, the numerator is fairly simple to calcualte based on two previous numerators.
N1 = 1
N2 = I2
Nn = In*N[n-2] + N[n-3]
And since N1 = 1 and original saved numerator can be 0, the last equation can be used
for all numerators except the first:
N1 = 1
Nn = In*N[n-1] + N[n-2] where N[0] = 0;
So, this is fairly simple.
*/
#endif //__FRACTION_H__