git » metnum-tp1.git » commit 3b7a65c

Agregar método de fracciónes continuas

author Alberto Bertogli
2010-08-31 01:09:14 UTC
committer Alberto Bertogli
2010-08-31 01:11:20 UTC
parent 323404e9c320bd7a5cf68e55bbac60bdb1064c42

Agregar método de fracciónes continuas

De este método ya hablamos varias implementaciónes posibles, incluyendo una
que no utiliza flotantes hasta solo el último paso.

Esta implementación es bien simple y literal, realizando las fracciónes todo
el tiempo, pero al menos converge a algo estable:

$ for i in `seq 1 50`; do echo $i `./fcont 52 $i`; done
1 1.250000000000000000000000000000000000000000000000000000000000 0x3ff4000000000000
2 1.444444444444444419772821674996521323919296264648437500000000 0x3ff71c71c71c71c7
3 1.409090909090909171652583609102293848991394042968750000000000 0x3ff68ba2e8ba2e8c
4 1.415094339622641417264503616024740040302276611328125000000000 0x3ff6a439f656f182
5 1.414062500000000000000000000000000000000000000000000000000000 0x3ff6a00000000000
6 1.414239482200647213261390788829885423183441162109375000000000 0x3ff6a0b9944c3856
7 1.414209115281501283334364416077733039855957031250000000000000 0x3ff6a099bcbd6d20
8 1.414214325374791858536127620027400553226470947265625000000000 0x3ff6a09f33504f9a
9 1.414213431462741477062650119478348642587661743164062500000000 0x3ff6a09e435b2481
10 1.414213584833762027415104967076331377029418945312500000000000 0x3ff6a09e6c86b808
11 1.414213558519453961537237773882225155830383300781250000000000 0x3ff6a09e657669d4
12 1.414213563034275367158443259540945291519165039062500000000000 0x3ff6a09e66acab68
13 1.414213562259654555219867688720114529132843017578125000000000 0x3ff6a09e66777022
14 1.414213562392558465319325478048995137214660644531250000000000 0x3ff6a09e66809234
15 1.414213562369755594616549387865234166383743286132812500000000 0x3ff6a09e667f010d
16 1.414213562373668020555328439513687044382095336914062500000000 0x3ff6a09e667f45e1
17 1.414213562372996779714640069869346916675567626953125000000000 0x3ff6a09e667f3a12
18 1.414213562373111798819991236086934804916381835937500000000000 0x3ff6a09e667f3c18
19 1.414213562373092258894757833331823348999023437500000000000000 0x3ff6a09e667f3bc0
20 1.414213562373095589563831708801444619894027709960937500000000 0x3ff6a09e667f3bcf
21 1.414213562373094923430016933707520365715026855468750000000000 0x3ff6a09e667f3bcc
22 1.414213562373095145474621858738828450441360473632812500000000 0x3ff6a09e667f3bcd
23 1.414213562373095145474621858738828450441360473632812500000000 0x3ff6a09e667f3bcd
24 1.414213562373095145474621858738828450441360473632812500000000 0x3ff6a09e667f3bcd

.gitignore +1 -0
Makefile +5 -1
fcont.cpp +32 -0

diff --git a/.gitignore b/.gitignore
index 7c0bff3..ed9763d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,4 +3,5 @@
 calc
 babilonio
 binomial
+fcont
 
diff --git a/Makefile b/Makefile
index 47639f7..81742b5 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@ CXXFLAGS += -Wall -std=c++98 -pedantic -Wno-long-long
 
 OBJS = aritmetica.o
 
-default: calc babilonio binomial
+default: calc babilonio binomial fcont
 
 all: default
 
@@ -13,6 +13,9 @@ calc: $(OBJS) calc.o
 babilonio: $(OBJS) babilonio.o
 	$(CXX) $(CXXFLAGS) $^ -o $@
 
+fcont: $(OBJS) fcont.o
+	$(CXX) $(CXXFLAGS) $^ -o $@
+
 binomial: $(OBJS) binomial.o
 	$(CXX) $(CXXFLAGS) $^ -o $@
 
@@ -20,5 +23,6 @@ clean:
 	rm -f $(OBJS)
 	rm -f calc.o calc
 	rm -f babilonio.o babilonio
+	rm -f fcont.o fcont
 	rm -f binomial.o binomial
 
diff --git a/fcont.cpp b/fcont.cpp
new file mode 100644
index 0000000..b9d6ed8
--- /dev/null
+++ b/fcont.cpp
@@ -0,0 +1,32 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include "aritmetica.hpp"
+
+int main(int argc, char *argv[])
+{
+	if (argc != 3) {
+		printf("Uso: %s <prec> <nterm>\n", argv[0]);
+		return 1;
+	}
+
+	int prec = atoi(argv[1]);
+	int nterm = atoi(argv[2]);
+
+	numero n(prec, 0.5);
+
+	/* Loop de la fracción, nterm - 1 términos ya que el último es
+	 * distinto */
+	for (int i = 0; i < nterm; i++) {
+		n = numero(prec, 1) / n + 2.0;
+	}
+
+	/* El último término tiene sumado 1 en lugar de 2 */
+	n = numero(prec, 1) / n + 1.0;
+
+	printf("%.60f 0x%lx\n", (double) n, to_u64(n));
+
+	return 0;
+}
+