-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathcontfrac.c
498 lines (486 loc) · 15.4 KB
/
contfrac.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//
// This file is part of Alpertron Calculators.
//
// Copyright 2015-2021 Dario Alejandro Alpern
//
// Alpertron Calculators 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.
//
// Alpertron Calculators 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 Alpertron Calculators. If not, see <http://www.gnu.org/licenses/>.
//
#include <string.h>
#include "bignbr.h"
#include "expression.h"
#include "highlevel.h"
#include "showtime.h"
static BigInteger num;
static BigInteger den;
static BigInteger delta;
static BigInteger origNum;
static BigInteger origDen;
static BigInteger origDelta;
static BigInteger Temp;
static BigInteger bigTmp;
static BigInteger U1;
static BigInteger U2;
static BigInteger U3;
static BigInteger V1;
static BigInteger V2;
static BigInteger V3;
static BigInteger startPeriodNum;
static BigInteger startPeriodDen;
static BigInteger intSqrt;
static BigInteger integerPart;
static char *ptrOutput;
static void ShowRational(BigInteger *pNum, BigInteger *pDen);
static bool isShowingConvergents;
static int periodIndex;
static int coefIndex;
static bool hexadecimal;
static void showText(const char *text)
{
copyStr(&ptrOutput, text);
}
static void BigInteger2Out(char **ppDecimal, const BigInteger *pBigInt, int groupLength)
{
if (hexadecimal)
{
BigInteger2Hex(ppDecimal, pBigInt, groupLength);
}
else
{
BigInteger2Dec(ppDecimal, pBigInt, groupLength);
}
}
static void ShowConvergents(int coeffIndex, const BigInteger *coeff)
{
CopyBigInt(&U3, &U2); // U3 <- U2, U2 <- U1, U1 <- a*U2 + U3
CopyBigInt(&U2, &U1);
(void)BigIntMultiply(coeff, &U2, &U1);
BigIntAdd(&U1, &U3, &U1);
CopyBigInt(&V3, &V2); // V3 <- V2, V2 <- V1, V1 <- a*V2 + V3
CopyBigInt(&V2, &V1);
(void)BigIntMultiply(coeff, &V2, &V1);
BigIntAdd(&V1, &V3, &V1);
*ptrOutput = 'A';
ptrOutput++;
*ptrOutput = '[';
ptrOutput++;
int2dec(&ptrOutput, coeffIndex);
*ptrOutput = ']';
ptrOutput++;
*ptrOutput = '/';
ptrOutput++;
*ptrOutput = 'B';
ptrOutput++;
*ptrOutput = '[';
ptrOutput++;
int2dec(&ptrOutput, coeffIndex);
*ptrOutput = ']';
ptrOutput++;
*ptrOutput = ' ';
ptrOutput++;
*ptrOutput = '=';
ptrOutput++;
*ptrOutput = ' ';
ptrOutput++;
BigInteger2Out(&ptrOutput, &U1, groupLen); // Show continued fraction convergent.
*ptrOutput = '/';
ptrOutput++;
BigInteger2Out(&ptrOutput, &V1, groupLen); // Show continued fraction convergent.
*ptrOutput = ',';
ptrOutput++;
*ptrOutput = ' ';
ptrOutput++;
*ptrOutput = 'a';
ptrOutput++;
*ptrOutput = '[';
ptrOutput++;
int2dec(&ptrOutput, coeffIndex);
*ptrOutput = ']';
ptrOutput++;
*ptrOutput = ' ';
ptrOutput++;
*ptrOutput = '=';
ptrOutput++;
*ptrOutput = ' ';
ptrOutput++;
BigInteger2Out(&ptrOutput, coeff, groupLen); // Show continued fraction coefficient.
*ptrOutput = '<';
ptrOutput++;
*ptrOutput = 'b';
ptrOutput++;
*ptrOutput = 'r';
ptrOutput++;
*ptrOutput = '>';
ptrOutput++;
*ptrOutput = 0;
}
// PQa algorithm for
// (P+G)/Q where G is the square root of discriminant
// If D - U^2 is not multiple of V then
// U = U*V
// V = V*V
// G = G*V
// U1 <- 1, U2 <- 0
// V1 <- 0, V2 <- 1
// Perform loop:
// a = floor((U + G)/V)
// U3 <- U2, U2 <- U1, U1 <- a*U2 + U3
// V3 <- V2, V2 <- V1, V1 <- a*V2 + V3
// U <- a*V - U
// V <- (D - U^2)/V
// Inside period when: 0 <= G - U < V
static void PeriodicContinuedFraction(bool isContinuation)
{
bool ended;
size_t diffPtrs;
if (!isContinuation)
{
// Check whether parameters have to be changed.
// Initialize variables.
intToBigInteger(&startPeriodNum, -1); // Less than zero means outside period.
intToBigInteger(&startPeriodDen, -1);
coefIndex = 0;
periodIndex = 0;
}
ended = false;
do
{
BigIntAdd(&num, &intSqrt, &bigTmp);
if (den.sign == SIGN_NEGATIVE)
{ // If denominator is negative, round square root upwards.
addbigint(&bigTmp, 1);
}
floordiv(&bigTmp, &den, &Temp); // Temp = Term of continued fraction.
if (coefIndex == 0)
{
CopyBigInt(&integerPart, &Temp);
}
if (isShowingConvergents)
{ // Show convergent checkbox is checked.
ShowConvergents(coefIndex, &Temp);
}
else
{ // Show convergent checkbox not checked.
BigInteger2Out(&ptrOutput, &Temp, groupLen); // Show continued fraction coefficient.
copyStr(&ptrOutput, ((coefIndex == 0) ? " + //" : ", ")); // Show separator.
}
(void)BigIntMultiply(&Temp, &den, &bigTmp); // U <- a*V - U
BigIntSubt(&bigTmp, &num, &num);
(void)BigIntMultiply(&num, &num, &bigTmp); // V <- (D - U^2)/V
BigIntSubt(&delta, &bigTmp, &bigTmp);
(void)BigIntDivide(&bigTmp, &den, &Temp);
CopyBigInt(&den, &Temp);
coefIndex++;
if (startPeriodNum.sign == SIGN_POSITIVE)
{ // Already inside period.
periodIndex++;
if (BigIntEqual(&num, &startPeriodNum) && BigIntEqual(&den, &startPeriodDen))
{ // New period started.
ended = true;
break; // Go out in this case.
}
}
else
{ // Check if periodic part of continued fraction has started.
BigIntSubt(&intSqrt, &num, &bigTmp); // G - U must be >= 0 and < V.
if (bigTmp.sign == SIGN_POSITIVE)
{
BigIntSubt(&bigTmp, &den, &bigTmp);
if (bigTmp.sign == SIGN_NEGATIVE)
{ // Periodic part of continued fraction started.
CopyBigInt(&startPeriodNum, &num); // Save U and V to check period end.
CopyBigInt(&startPeriodDen, &den);
showText("<span class=\"offscr\">");
showText(lang ? "inicio del período" : "start periodic part");
showText("</span><span class=\"bold\">");
}
}
}
diffPtrs = ptrOutput - &output[0];
} while ((int)diffPtrs < ((int)sizeof(output) - 30000));
if (!isShowingConvergents)
{ // Show convergent checkbox not checked.
ptrOutput -= 2; // Delete extra comma and space at the end.
}
if (ended)
{
showText("</span>");
if (!isShowingConvergents)
{ // Show convergent checkbox not checked.
showText("//");
}
showText("<br><span aria-hidden=\"true\">");
showText(lang ? "donde la parte periódica está señalada en negrita</span>" :
"where the periodic part is marked in bold</span>");
if (periodIndex > 1)
{
showText(lang ? " (el período tiene " : " (the period has ");
int2dec(&ptrOutput, periodIndex);
showText(lang ? " coeficientes)" : " coefficients)");
}
}
else
{ // Too many coefficients.
if (!isShowingConvergents)
{ // Show convergents checkbox not checked.
showText(", ... </span>//");
}
else
{
showText("... </span>");
}
showText(lang ? "<br>donde la parte periódica (truncada a partir de los " :
"<br>where the periodic part (truncated after ");
int2dec(&ptrOutput, periodIndex);
showText(lang ? " coeficientes) está señalada en negrita.</p>" :
" coefficients) is marked in bold.</p>");
output[0] = '6'; // Show Continue button.
}
}
static void showFooter(void)
{
#ifdef __EMSCRIPTEN__
copyStr(&ptrOutput, lang ? "<p>Transcurrió " : "<p>Time elapsed: ");
int elapsedTime = (int)(tenths() - originalTenthSecond);
GetDHMSt(&ptrOutput, elapsedTime);
#endif
showText("<p>");
showText(lang ? COPYRIGHT_SPANISH : COPYRIGHT_ENGLISH);
showText("</p>");
}
static void showFormula(void)
{
ptrOutput = output;
// Show formula.
showText("2<p><var>x</var> = <span class=\"fraction\"><span class=\"offscr\">");
copyStr(&ptrOutput, lang ? " la fracción cuyo numerador es </span>" : " the fraction whose numerator is </span>");
showText("<span class=\"fup\">");
BigInteger2Out(&ptrOutput, &origNum, groupLen); // Show numerator.
showText(" + <span class=\"sqrtout\"><span class=\"sqrtin\">");
BigInteger2Out(&ptrOutput, &origDelta, groupLen); // Show radicand.
showText("</span></span></span><span class=\"bar\"> </span><span class=\"fdn\"><span class=\"offscr\">");
copyStr(&ptrOutput, lang ? " y el denominador es </span>" : " and the denominator is </span>");
BigInteger2Out(&ptrOutput, &origDen, groupLen); // Show denominator.
showText("</span></span></span></p>");
}
static void showX(void)
{
showText("<p>");
if (!isShowingConvergents)
{ // Show convergent checkbox not checked.
showText("<span class = \"offscr\">");
showText(lang ? "El desarrollo en fracción continua de" :
"The expansion in continued fraction of");
showText(" </span><var>x</var> = ");
}
}
static void ContFrac(void)
{
CopyBigInt(&origNum, &num);
CopyBigInt(&origDen, &den);
CopyBigInt(&origDelta, &delta);
showFormula();
// Validate input.
if (BigIntIsZero(&den))
{
showText(lang? "<p>Error: El denominador es cero.</p>": "<p>Error: The denominator is zero.</p>");
return;
}
if (delta.sign==SIGN_NEGATIVE)
{ /* Complex number */
showText(lang? "<p>El número no es real, por lo que no tiene desarrollo en fracciones continuas.</p>":
"<p>The number is not real, so it does not have continued fraction expansion.</p>");
return;
}
showX();
intToBigInteger(&U1, 1);
intToBigInteger(&U2, 0);
intToBigInteger(&V1, 0);
intToBigInteger(&V2, 1);
if (BigIntIsZero(&delta))
{ /* Rational number */
ShowRational(&num, &den);
return;
}
(void)BigIntMultiply(&num, &num, &Temp);
BigIntSubt(&delta, &Temp, &Temp); // Temp <- delta - num^2.
(void)BigIntRemainder(&Temp, &den, &Temp);
if (!BigIntIsZero(&Temp))
{ // If delta - num^2 is not multiple of den...
enum eSign sign;
(void)BigIntMultiply(&delta, &den, &delta);
(void)BigIntMultiply(&delta, &den, &delta); // delta <- delta*den^2
sign = den.sign;
(void)BigIntMultiply(&num, &den, &num); // num <- num*den
(void)BigIntMultiply(&den, &den, &den); // den <- den*den
if (sign == SIGN_NEGATIVE)
{
BigIntNegate(&num, &num); // Change sign to both
BigIntNegate(&den, &den); // numerator and denominator.
}
}
squareRoot(delta.limbs, intSqrt.limbs, delta.nbrLimbs, &intSqrt.nbrLimbs);
intSqrt.sign = SIGN_POSITIVE;
(void)BigIntMultiply(&intSqrt, &intSqrt, &Temp);
if (TestBigNbrEqual(&Temp, &delta) != 0)
{ // delta is a perfect square, so number is rational.
BigIntAdd(&num, &intSqrt, &Temp);
ShowRational(&Temp, &den);
}
else
{ // delta is not a perfect square. Periodic continued fraction.
PeriodicContinuedFraction(false); // Start showing periodic continued fraction.
}
showFooter();
}
static void ShowRational(BigInteger *pNum, BigInteger *pDen)
{
static BigInteger Tmp;
const char *sep;
int coeffIndex = 0;
BigIntGcd(pNum, pDen, &Tmp); // Tmp <- GCD of numerator and denominator.
(void)BigIntDivide(pNum, &Tmp, pNum);
(void)BigIntDivide(pDen, &Tmp, pDen);
floordiv(pNum, pDen, &Tmp);
(void)BigIntMultiply(pDen, &Tmp, &bigTmp);
BigIntSubt(pNum, &bigTmp, pNum); // Reduce numerator.
if (isShowingConvergents)
{ // Show convergent checkbox is checked.
ShowConvergents(coeffIndex, &Tmp);
}
else
{
BigInteger2Out(&ptrOutput, &Tmp, groupLen); // Show convergent.
}
(void)BigIntRemainder(pNum, pDen, pNum);
sep = " + //";
while (!BigIntIsZero(pNum))
{ // Numerator greater than zero.
coeffIndex++;
if (!BigIntIsZero(pDen))
{ // Denominator greater than zero.
floordiv(pDen, pNum, &Tmp);
if (isShowingConvergents)
{ // Show convergent checkbox is checked.
ShowConvergents(coeffIndex, &Tmp);
}
else
{
showText(sep);
BigInteger2Out(&ptrOutput, &Tmp, groupLen); // Show convergent.
}
sep = ", ";
}
(void)BigIntMultiply(pNum, &Tmp, &bigTmp);
BigIntSubt(pDen, &bigTmp, &Tmp);
CopyBigInt(pDen, pNum);
CopyBigInt(pNum, &Tmp);
}
if (!isShowingConvergents && (sep[0] == ','))
{ // Inside continued fraction. Close it.
showText("//");
}
}
static void getNumber(BigInteger *pNumber, const char *title, char** pptrInput)
{
enum eExprErr rc;
rc = ComputeExpression(*pptrInput, pNumber);
if (rc != EXPR_OK)
{
if (ptrOutput == output)
{
*ptrOutput = '2';
ptrOutput++;
}
showText("<p>");
showText(title);
*ptrOutput = ':';
ptrOutput++;
*ptrOutput = ' ';
ptrOutput++;
textError(&ptrOutput, rc);
showText("</p>");
}
*pptrInput += strlen(*pptrInput) + 1U; // Skip terminator.
}
// input contains three expressions separated by 00h (null character).
void contfracText(char *input, int GroupLen, bool hex, bool converg)
{
if (input == NULL)
{
showFormula();
if (!isShowingConvergents)
{ // Show convergents checkbox not checked.
showX();
BigInteger2Out(&ptrOutput, &integerPart, groupLen); // Show integer part.
copyStr(&ptrOutput, " + // "); // Show separator.
}
showText("<span class=\"offscr\">"); // Indicate start of periodic part.
showText(lang ? "inicio del período" : "start periodic part");
showText("</span><span class=\"bold\">");
copyStr(&ptrOutput, "..."); // Show continuation.
PeriodicContinuedFraction(true);
showFooter();
return;
}
char *ptrInput = input;
groupLen = GroupLen;
ptrOutput = output;
isShowingConvergents = converg;
hexadecimal = hex;
getNumber(&num, lang? "Numerador": "Numerator", &ptrInput);
getNumber(&delta, lang? "Argumento de la raíz cuadrada": "Square root argument", &ptrInput);
getNumber(&den, lang? "Denominador": "Denominator", &ptrInput);
if (ptrOutput != output)
{ // Error written to output. Do not compute continued fraction.
return;
}
groupLen = GroupLen;
ContFrac();
}
#if defined(__EMSCRIPTEN__) && !defined(_MSC_VER)
EXTERNALIZE void doWork(void)
{
int app;
bool hex;
bool converg;
int grpLen = 0;
char* ptrData = inputString;
if (*ptrData == 'C')
{ // User pressed Continue button.
contfracText(NULL, 0, false, false); // Routine does not use parameters in this case.
databack(output);
return;
}
originalTenthSecond = tenths();
while (*ptrData != ',')
{
grpLen = (grpLen * 10) + (*ptrData - '0');
ptrData++;
}
ptrData++; // Skip comma.
app = *ptrData - '0';
while (*(ptrData + 1) != ',')
{
ptrData++;
app = (app * 10) + *ptrData - '0';
}
#ifndef lang
lang = ((app & 1) ? true : false);
#endif
hex = ((app & 0x40) != 0);
converg = ((app & 0x20) != 0);
contfracText(ptrData + 2, grpLen, hex, converg);
databack(output);
}
#endif