forked from mfillpot/mathomatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcd.c
468 lines (450 loc) · 13.5 KB
/
gcd.c
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
* General floating point GCD routine and associated code for Mathomatic.
* These routines are magically tuned to always give good results,
* even though floating point is used.
* Use of this code in other floating point programs that need a gcd() or
* double-to-fraction convert function is recommended.
* It is heavily tested through extensive use in this computer algebra system.
*
* Copyright (C) 1987-2012 George Gesslein II.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
The chief copyright holder can be contacted at gesslein@mathomatic.org, or
George Gesslein II, P.O. Box 224, Lansing, NY 14882-0224 USA.
*/
#include "includes.h"
/*
* Floating point GCD function.
*
* Returns the Greatest Common Divisor (GCD) of doubles d1 and d2,
* by using the Euclidean GCD algorithm.
*
* The GCD is defined as the largest positive number that evenly divides both d1 and d2.
* This should always works perfectly and exactly with two integers up to MAX_K_INTEGER.
* Will usually work with non-integers, but there may be some floating point error.
*
* Returns 0 on failure, otherwise returns the positive GCD.
*/
double
gcd(d1, d2)
double d1, d2;
{
int count;
double larger, divisor, remainder1, lower_limit;
if (!isfinite(d1) || !isfinite(d2)) {
return 0.0; /* operands must be finite */
}
d1 = fabs(d1);
d2 = fabs(d2);
#if 1 /* true for standard gcd(), otherwise returns 0 (failure) if either parameter is 0 */
if (d1 == 0)
return d2;
if (d2 == 0)
return d1;
#endif
if (d1 > d2) {
larger = d1;
divisor = d2;
} else {
larger = d2;
divisor = d1;
}
lower_limit = larger * epsilon;
if (divisor <= lower_limit || larger >= MAX_K_INTEGER) {
return 0.0; /* out of range, result would be too inaccurate */
}
for (count = 1; count < 50; count++) {
remainder1 = fabs(fmod(larger, divisor));
if (remainder1 <= lower_limit || fabs(divisor - remainder1) <= lower_limit) {
if (remainder1 != 0.0 && divisor <= (100.0 * lower_limit))
return 0.0;
return divisor;
}
larger = divisor;
divisor = remainder1;
}
return 0.0;
}
/*
* Verified floating point GCD function.
*
* Returns the verified exact Greatest Common Divisor (GCD) of doubles d1 and d2.
*
* Returns 0 on failure or inexactness, otherwise returns the verified positive GCD result.
* Result is not necessarily integer unless both d1 and d2 are integer.
*/
double
gcd_verified(d1, d2)
double d1, d2;
{
double divisor, d3, d4;
divisor = gcd(d1, d2);
if (divisor != 0.0) {
d3 = d1 / divisor;
d4 = d2 / divisor;
if (fmod(d3, 1.0) != 0.0 || fmod(d4, 1.0) != 0.0)
return 0.0;
if (gcd(d3, d4) != 1.0)
return 0.0;
}
return divisor;
}
/*
* Floating point round function.
*
* Returns the passed floating point double rounded to the nearest integer.
*/
double
my_round(d1)
double d1; /* value to round */
{
if (d1 >= 0.0) {
modf(d1 + 0.5, &d1);
} else {
modf(d1 - 0.5, &d1);
}
return d1;
}
/*
* Convert the passed double d to an equivalent fully reduced fraction.
* This done by the following simple algorithm:
*
* divisor = gcd(d, 1.0)
* numerator = d / divisor
* denominator = 1.0 / divisor
*
* Returns true with integers in numerator and denominator
* if conversion to a fraction was successful.
* Otherwise returns false with numerator = d and denominator = 1.0
*
* True return indicates d is rational and finite, otherwise d is probably irrational.
*/
int
f_to_fraction(d, numeratorp, denominatorp)
double d; /* floating point number to convert */
double *numeratorp; /* returned numerator */
double *denominatorp; /* returned denominator */
{
double divisor;
double numerator, denominator;
double k3, k4;
*numeratorp = d;
*denominatorp = 1.0;
if (!isfinite(d)) {
return false;
}
/* see if "d" is an integer, or very close to an integer: */
if (fmod(d, 1.0) == 0.0) {
/* d is an integer */
return true;
}
/* more than 15 digits in number means we do nothing (for better accuracy) */
if (fabs(d) >= MAX_K_INTEGER)
return false;
k3 = fabs(d) * small_epsilon;
if (k3 >= .5)
return false; /* fixes "factor number 17!" to give error instead of wrong answer */
k4 = my_round(d);
if (k4 != 0.0 && fabs(k4 - d) <= k3) {
/* very close to an integer, make it so (allows gamma() based factorial function to work properly, etc.) */
*numeratorp = k4;
return true;
}
/* try to convert non-integer floating point value in "d" to a fraction: */
if ((divisor = gcd(1.0, d)) > epsilon) {
numerator = my_round(d / divisor);
denominator = my_round(1.0 / divisor);
/* don't allow more than 11 digits in the numerator or denominator: */
if (fabs(numerator) >= 1.0e12)
return false;
if (denominator >= 1.0e12 || denominator < 2.0)
return false;
/* make sure the result is a fully reduced fraction: */
divisor = gcd(numerator, denominator);
if (divisor > 1.0) { /* just in case result isn't already fully reduced */
numerator /= divisor;
denominator /= divisor;
}
k3 = (numerator / denominator);
if (fabs(k3 - d) > (small_epsilon * fabs(k3))) {
return false; /* result is too inaccurate */
}
if (fmod(numerator, 1.0) != 0.0 || fmod(denominator, 1.0) != 0.0) {
/* Shouldn't happen if everything works. */
#if DEBUG
error_bug("Fraction should have been fully reduced by gcd(), but was not.");
#endif
return false;
}
/* numerator and denominator are guaranteed integral */
*numeratorp = numerator;
*denominatorp = denominator;
return true;
}
return false;
}
/*
* Call make_simple_fractions() or make_mixed_fractions() below,
* depending on the current fractions display mode.
*
* Returns true if any fractions were created.
*/
int
make_fractions(equation, np)
token_type *equation; /* equation side pointer */
int *np; /* pointer to length of equation side */
{
switch (fractions_display) {
case 2:
return make_mixed_fractions(equation, np);
break;
default:
return make_simple_fractions(equation, np);
break;
}
}
/*
* Convert all non-integer constants in an equation side to simple, fully reduced fractions,
* when exactly equal to a fraction without a very large numerator or denominator.
* Uses f_to_fraction() above, which limits the numerator and denominator to 11 digits each.
* The floating point gcd() function used limits the complexity of fractions further.
*
* Returns true if any fractions were created.
*/
int
make_simple_fractions(equation, np)
token_type *equation; /* equation side pointer */
int *np; /* pointer to length of equation side */
{
int i, j, k;
int level;
double numerator, denominator;
int inc_level, modified = false;
for (i = 0; i < *np; i += 2) {
if (equation[i].kind == CONSTANT) {
level = equation[i].level;
if (i > 0 && equation[i-1].level == level && (equation[i-1].token.operatr == DIVIDE /* || equation[i-1].token.operatr == POWER */))
continue;
if (!f_to_fraction(equation[i].token.constant, &numerator, &denominator))
continue;
if (denominator == 1.0) {
equation[i].token.constant = numerator;
continue;
}
if ((*np + 2) > n_tokens) {
error_huge();
}
modified = true;
inc_level = (*np > 1);
if ((i + 1) < *np && equation[i+1].level == level) {
switch (equation[i+1].token.operatr) {
case TIMES:
for (j = i + 3; j < *np && equation[j].level >= level; j += 2) {
if (equation[j].level == level && equation[j].token.operatr == DIVIDE) {
break;
}
}
if (numerator == 1.0) {
blt(&equation[i], &equation[i+2], (j - (i + 2)) * sizeof(token_type));
j -= 2;
} else {
equation[i].token.constant = numerator;
blt(&equation[j+2], &equation[j], (*np - j) * sizeof(token_type));
*np += 2;
}
equation[j].level = level;
equation[j].kind = OPERATOR;
equation[j].token.operatr = DIVIDE;
j++;
equation[j].level = level;
equation[j].kind = CONSTANT;
equation[j].token.constant = denominator;
if (numerator == 1.0) {
i -= 2;
}
continue;
case DIVIDE:
inc_level = false;
break;
}
}
j = i;
blt(&equation[i+3], &equation[i+1], (*np - (i + 1)) * sizeof(token_type));
*np += 2;
equation[j].token.constant = numerator;
j++;
equation[j].level = level;
equation[j].kind = OPERATOR;
equation[j].token.operatr = DIVIDE;
j++;
equation[j].level = level;
equation[j].kind = CONSTANT;
equation[j].token.constant = denominator;
if (inc_level) {
for (k = i; k <= j; k++)
equation[k].level++;
}
}
}
return modified;
}
/*
* Convert all non-integer constants in an equation side to mixed, fully reduced fractions,
* when exactly equal to a fraction without a very large numerator or denominator.
* A mixed fraction is an expression like (2 + (1/4)),
* which is equal to the simple fraction 9/4.
* If you only want simple fractions, use make_simple_fractions() above.
*
* Returns true if any fractions were created.
*/
int
make_mixed_fractions(equation, np)
token_type *equation; /* equation side pointer */
int *np; /* pointer to length of equation side */
{
int i, j, k;
int level;
double numerator, denominator, quotient1, remainder1;
int inc_level, modified = false;
for (i = 0; i < *np; i += 2) {
if (equation[i].kind == CONSTANT) {
level = equation[i].level;
if (i > 0 && equation[i-1].level == level && (equation[i-1].token.operatr == DIVIDE /* || equation[i-1].token.operatr == POWER */))
continue;
if (!f_to_fraction(equation[i].token.constant, &numerator, &denominator))
continue;
if (denominator == 1.0) {
equation[i].token.constant = numerator;
continue;
}
modified = true;
if (fabs(numerator) > denominator) {
remainder1 = modf(fabs(numerator) / denominator, "ient1);
remainder1 = my_round(remainder1 * denominator);
if (numerator < 0.0) {
if ((*np + 6) > n_tokens) {
error_huge();
}
blt(&equation[i+7], &equation[i+1], (*np - (i + 1)) * sizeof(token_type));
*np += 6;
equation[i].level = level + 1;
equation[i].token.constant = -1.0;
i++;
equation[i].level = level + 1;
equation[i].kind = OPERATOR;
equation[i].token.operatr = TIMES;
i++;
equation[i].level = level + 2;
equation[i].kind = CONSTANT;
equation[i].token.constant = quotient1;
i++;
equation[i].level = level + 2;
equation[i].kind = OPERATOR;
equation[i].token.operatr = PLUS;
i++;
equation[i].level = level + 3;
equation[i].kind = CONSTANT;
equation[i].token.constant = remainder1;
i++;
equation[i].level = level + 3;
equation[i].kind = OPERATOR;
equation[i].token.operatr = DIVIDE;
i++;
equation[i].level = level + 3;
equation[i].kind = CONSTANT;
equation[i].token.constant = denominator;
} else {
if ((*np + 4) > n_tokens) {
error_huge();
}
blt(&equation[i+5], &equation[i+1], (*np - (i + 1)) * sizeof(token_type));
*np += 4;
equation[i].level = level + 1;
equation[i].token.constant = quotient1;
i++;
equation[i].level = level + 1;
equation[i].kind = OPERATOR;
equation[i].token.operatr = PLUS;
i++;
equation[i].level = level + 2;
equation[i].kind = CONSTANT;
equation[i].token.constant = remainder1;
i++;
equation[i].level = level + 2;
equation[i].kind = OPERATOR;
equation[i].token.operatr = DIVIDE;
i++;
equation[i].level = level + 2;
equation[i].kind = CONSTANT;
equation[i].token.constant = denominator;
}
} else {
if ((*np + 2) > n_tokens) {
error_huge();
}
inc_level = (*np > 1);
if ((i + 1) < *np && equation[i+1].level == level) {
switch (equation[i+1].token.operatr) {
case TIMES:
for (j = i + 3; j < *np && equation[j].level >= level; j += 2) {
if (equation[j].level == level && equation[j].token.operatr == DIVIDE) {
break;
}
}
if (numerator == 1.0) {
blt(&equation[i], &equation[i+2], (j - (i + 2)) * sizeof(token_type));
j -= 2;
} else {
equation[i].token.constant = numerator;
blt(&equation[j+2], &equation[j], (*np - j) * sizeof(token_type));
*np += 2;
}
equation[j].level = level;
equation[j].kind = OPERATOR;
equation[j].token.operatr = DIVIDE;
j++;
equation[j].level = level;
equation[j].kind = CONSTANT;
equation[j].token.constant = denominator;
if (numerator == 1.0) {
i -= 2;
}
continue;
case DIVIDE:
inc_level = false;
break;
}
}
j = i;
blt(&equation[i+3], &equation[i+1], (*np - (i + 1)) * sizeof(token_type));
*np += 2;
equation[j].token.constant = numerator;
j++;
equation[j].level = level;
equation[j].kind = OPERATOR;
equation[j].token.operatr = DIVIDE;
j++;
equation[j].level = level;
equation[j].kind = CONSTANT;
equation[j].token.constant = denominator;
if (inc_level) {
for (k = i; k <= j; k++)
equation[k].level++;
}
}
}
}
if (modified) {
organize(equation, np);
}
return modified;
}