Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cmath>
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- #include <cstdlib>
- using namespace std;
- pair<long int,long int > farey(const __float80 &x, const long int &N){
- pair < long int , long int > p;
- __float80 media;
- long int a = 0 ,b = 1,c = 1,d = 1;
- while ( b <= N && d <= N ){
- media = (__float80(a+c)/(b+d));
- //cout << media << endl;
- if ( x == media ){
- if ((b+d) <= N){
- return make_pair(a+c,b+d);
- }
- else if( d > b ){
- return make_pair(c,d);
- }
- else{
- return make_pair(a,b);
- }
- }
- else if( x > media ){
- a = a+c;
- b = b+d;
- }
- else {
- c = c+a;
- d = b+d;
- }
- }
- if ( b > N ){
- return make_pair(c,d);
- }else{
- return make_pair(a,b);
- }
- }
- int main(int argc , char *argv[]){
- if (argc <= 1 ) {
- cout << "Necesita un argumento decimal entre 0 y 1" << endl;
- exit (1);
- }
- __float80 pp = atof(argv[1]);
- pair<long int,long int> p1 ,p2;
- __float80 rate = 1;
- __float80 prec = 1;
- __float80 decimal = pp;
- __float80 real_precision = 1E-12;
- long int j = 10000;
- while( prec > real_precision ){
- j += 10000;
- p1 = farey(decimal,j);
- rate = __float80(double(p1.first))/(p1.second);
- prec = abs(decimal-rate);
- }
- cout << "la fracion es : ";
- cout << setw(6) << setprecision(15) << p1.first << "/" << p1.second ;
- cout <<" " << setw(9) << rate <<" "<< setw(12) << prec << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment