Advertisement
overloop

rational_approximation.cpp

Jul 17th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <vector>
  4. #include <utility>
  5. #include <algorithm>
  6.  
  7. typedef std::pair<int,int> intpair_t;
  8.  
  9. void findRational(std::vector<intpair_t>& result, double v, double e, int qmax) {
  10.    
  11.     int q = 2;
  12.     int p = (int) (v * (double) q);
  13.    
  14.     while (q<qmax) {
  15.        
  16.         int h1, h2;
  17.         double hv1, hv2;
  18.        
  19.         h1 = p;
  20.         h2 = p+1;
  21.         hv1 = (double) h1/q;
  22.         hv2 = (double) h2/q;
  23.         //printf("hyp: %d/%d %d/%d %f %f\n",p,q,p+1,q,hv1,hv2);
  24.        
  25.         if (hv2 >= v && hv1 <=v) {
  26.            
  27.             if (fabs(hv1-v)<fabs(hv2-v)) {
  28.                 if (fabs(hv1-v)<e) {
  29.                     result.push_back(intpair_t(h1,q));
  30.                 }
  31.             } else {
  32.                 if (fabs(hv2-v)<e) {
  33.                     result.push_back(intpair_t(h2,q));
  34.                 }
  35.             }
  36.            
  37.             q++;
  38.             p = (int) (v * (double) q);
  39.         } /*else {
  40.             if (hv2 > v) {
  41.                 p--;
  42.             } else {
  43.                 p++;
  44.             }
  45.             h1 = p;
  46.             h2 = p+1;
  47.             hv1 = (double) h1/q;
  48.             hv2 = (double) h2/q;
  49.             //printf("hyp: %d/%d %d/%d %f %f\n",p,q,p+1,q,hv1,hv2);
  50.             printf("hey!\n");
  51.         }*/
  52.     }
  53.    
  54. }
  55.  
  56. class RationalComparer {
  57. public:
  58.     RationalComparer(double v) {this->v = v;}
  59.     bool operator() (const intpair_t& v1,const intpair_t& v2) {
  60.         double vv1 = (double) v1.first / v1.second;
  61.         double vv2 = (double) v2.first / v2.second;
  62.         return fabs(vv1 - v) < fabs(vv2 - v);
  63.     }
  64.     double v;
  65. };
  66.  
  67. int main(int argc, char** argv) {
  68.    
  69.    
  70.     if (argc < 4) {
  71.         printf("usage: %s value error maximum_denominator\n",argv[0]);
  72.         printf("example: %s 3.1415925 0.000001 1000\n",argv[0]);
  73.         return 0;
  74.     }
  75.    
  76.     double v = atof(argv[1]);
  77.     double e = atof(argv[2]);
  78.     int qmax = atoi(argv[3]);
  79.    
  80.     std::vector<intpair_t> result;
  81.    
  82.     findRational(result,v,e,qmax);
  83.    
  84.     RationalComparer comparer(v);
  85.     std::sort(result.begin(),result.end(),comparer);
  86.    
  87.     for (int i=0;i<result.size();i++) {
  88.         intpair_t pq = result.at(i);
  89.         double vi = (double) pq.first / pq.second;
  90.         printf("%d/%d %f (%g)\n",pq.first,pq.second,vi,fabs(vi-v));
  91.     }
  92.    
  93.     return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement