Advertisement
bartekltg

logarytmy

Jul 22nd, 2014
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.37 KB | None | 0 0
  1. #include <iostream>
  2. #include <chrono>
  3. #include <cmath>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. class stoper
  10. {public:
  11.     chrono::time_point<chrono::high_resolution_clock> a,b;
  12.     void start(){a = chrono::high_resolution_clock::now();}
  13.     void stop() {b = chrono::high_resolution_clock::now();}
  14.     void show()
  15.     {
  16.         chrono::duration<double> elapsed_seconds = b-a;
  17.         cout << elapsed_seconds.count()<<"s ";
  18.     }
  19. };
  20.  
  21.  
  22. template <typename cfun>
  23. double test(cfun fun)
  24. {
  25.     double aku=0;
  26.     for (int i=0;i<5;i++)
  27.     {
  28.         stoper st;
  29.         st.start();
  30.  
  31.         for (double x=1;x<10000000;x+=1.01 )
  32.         {
  33.             aku +=  fun(x);
  34.         }
  35.         st.stop();
  36.         st.show();
  37.     }
  38.     cout<<endl;
  39.     return aku;
  40. }
  41.  
  42.  
  43. double shitlog2(double x)
  44. {
  45.     int w;
  46.     x = frexp(x,&w);
  47.     return w + 2.0*(x-1.0);
  48. }
  49.  
  50. double fastlog2(double x)
  51. {
  52.     int w;
  53.     x = frexp(x,&w);
  54.     return w + ((((1.43202073027606*x-6.64984902163488)*x+13.0262412232996)*x
  55.             -14.1861534849729)*x+10.1772348373405)*x-3.79948152975902;
  56. }
  57. double mylog2_17(double x)
  58. {
  59.     int w;
  60.     x = frexp(x,&w);
  61. return w +95.0354082212196444880354135069-100.5 +
  62.  x *(34.6797528315816853011413739901 +
  63.     x *(-195.451529212527416014523545585 +
  64.        x *(914.687118367090492019088236267 +
  65.           x *(-3359.00328658200541339129556488 +
  66.              x *(9739.55993077093141495559279973 +
  67.                 x *(-22548.3565655480936147709657466 +
  68.                    x* (42039.3539337947134863905451932 +
  69.                     x* (-63422.0205169046056292386635416 +
  70.                     x *(77487.8101813068073659642177593 +
  71.                     x* (-76418.7051434147324374252002510 +
  72.                     x* (60345.2464877410927129479719750 +
  73.                     x* (-37623.7416809594729290426177664 +
  74.                     x* (18108.4544635302497336104275950 +
  75.                     x* (-6491.01004625378413321975755283 +
  76.                     x* (1631.56187150122776398754613596 +
  77.                     x* (-256.603734082675540229189858863 +
  78.                     19.0033548929828164153801905343 *x))))))))))))))));
  79. }
  80. double mylog2_19(double x)
  81. {
  82.     int w;
  83.     x = frexp(x,&w);
  84. return w +94.879552359370389779311310752431677657546488670131-100.5+
  85.         x*(38.760953171904166937692617024628986668555406124947+
  86.         x*(-245.85943859848429206309024729452081797918448383011+
  87.         x*(1305.0213373140979443003790999604901721351935859746+
  88.         x*(-5483.3897186528672757505743075974516855722609750103+
  89.         x*(18373.369285947488212376445282807142432894707031893+
  90.         x*(-49720.330484326774507017799362836307490489909887034+
  91.         x*(109801.21447583798026544996683803403658873989417970+
  92.         x*(-199301.45445926506025828784277462614378129514274147+
  93.         x*(298501.60630250569282203503684755811600024394635379+
  94.         x*(-369205.43714028094981807593680305547807524091749705+
  95.         x*(376227.86563934815887666995648017764570129251640536+
  96.         x*(-314015.29189075395963525655516844515933367689469445+
  97.         x*(212512.59498006697236627947497199497997106198214730+
  98.         x*(-114798.02778261877254409048412664787618047864843291+
  99.         x*(48332.575660801499026504675067647169449538315154290+
  100.         x*(-15283.729869425976544135405204428853285998986213565+
  101.         x*(3414.6711038764781556224963615063635427392290076089+
  102.         x*(-480.58172520332090598267316843954641211274866464892+
  103.         x*32.04321789652355477774462860249555604587514031906
  104.         ))))))))))))))))));
  105. }
  106.  
  107. double mylog2_pade_7_6(double x)
  108. {
  109.     if (x<0) return nan("");
  110.     int w;
  111.     x = frexp(x,&w);
  112. return w+
  113. (-0.138283895467597308734825929 +
  114.    x* (-4.75644115041872693502244398 +
  115.       x *(-28.2901284421308848729647527 +
  116.          x *(-31.020711652270330663079404 +
  117.             x *(26.954639037773333537566195 +
  118.                x *(32.065091647893956780806165 +
  119. (5.14481531055604261525940722 +
  120.                     0.0410191440642069963727475423* x)* x))))))
  121.    /(0.01773580523008302467852483039 +
  122.    x *(1.066870914453938639820160162 +
  123.       x* (11.40739806765892909737152090 +
  124.          x* (35.97371731450040844764454601 +
  125.             x* (38.11130966747411808773004738 +
  126.                x* (12.86083151020947229832310848 + x))))));
  127. }
  128.  
  129. int main()
  130. {
  131.     stoper st;
  132.     st.start();
  133.     double aku=0;
  134.     for (double x=1;x<10000;x+=1.01 )
  135.     {
  136.         aku = aku + log(x);
  137.     }
  138.     st.stop();
  139.     st.show();
  140.     cout<<endl;
  141.  
  142.     cout<< "log: ";
  143.     aku+=test([](double x) {return log(x);} );
  144.     cout<< "log10: ";
  145.     aku+=test([](double x) {return log10(x);} );
  146.     cout<< "log2: ";
  147.     aku+=test([](double x) {return log2(x);} );
  148.     cout<< "shitlog2 ";
  149.     aku+=test(shitlog2 );
  150.     cout<< "fastlog2 ";
  151.     aku+=test(fastlog2 );
  152.     cout<< "mylog2_17 ";
  153.     aku+=test(mylog2_17 );
  154.     cout<< "mylog2_19 ";
  155.     aku+=test(mylog2_19 );
  156.     cout<< "mylog2_pade_7_6 ";
  157.     aku+=test(mylog2_pade_7_6 );
  158.  
  159.  
  160.     cout<<aku<<endl;
  161.  
  162.     cout<<"  x     std::log2, Dshitlog Dfastlog Dmylog17 Dmylog19 Dpade7_6"<<endl;
  163.     for (double x=0.01;x<100;x*=1.04)
  164.     {
  165.         cout<<x<<" "<<log2(x)<<" "<</*shitlog2(x)<<" "<<fastlog2(x)<<" "<<mylog2_17(x)<<" "<<mylog2_19(x)<<" "<<*/
  166.               shitlog2(x)-log2(x)<<" "<<fastlog2(x)-log2(x)<<" "<<mylog2_17(x)-log2(x)<<
  167.               " "<<mylog2_19(x)-log2(x)<<" "<<mylog2_pade_7_6(x)-log2(x)<<endl;
  168.  
  169.     }
  170.  
  171.  
  172.  
  173.  
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement