Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <chrono>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- class stoper
- {public:
- chrono::time_point<chrono::high_resolution_clock> a,b;
- void start(){a = chrono::high_resolution_clock::now();}
- void stop() {b = chrono::high_resolution_clock::now();}
- void show()
- {
- chrono::duration<double> elapsed_seconds = b-a;
- cout << elapsed_seconds.count()<<"s ";
- }
- };
- template <typename cfun>
- double test(cfun fun)
- {
- double aku=0;
- for (int i=0;i<5;i++)
- {
- stoper st;
- st.start();
- for (double x=1;x<10000000;x+=1.01 )
- {
- aku += fun(x);
- }
- st.stop();
- st.show();
- }
- cout<<endl;
- return aku;
- }
- double shitlog2(double x)
- {
- int w;
- x = frexp(x,&w);
- return w + 2.0*(x-1.0);
- }
- double fastlog2(double x)
- {
- int w;
- x = frexp(x,&w);
- return w + ((((1.43202073027606*x-6.64984902163488)*x+13.0262412232996)*x
- -14.1861534849729)*x+10.1772348373405)*x-3.79948152975902;
- }
- double mylog2_17(double x)
- {
- int w;
- x = frexp(x,&w);
- return w +95.0354082212196444880354135069-100.5 +
- x *(34.6797528315816853011413739901 +
- x *(-195.451529212527416014523545585 +
- x *(914.687118367090492019088236267 +
- x *(-3359.00328658200541339129556488 +
- x *(9739.55993077093141495559279973 +
- x *(-22548.3565655480936147709657466 +
- x* (42039.3539337947134863905451932 +
- x* (-63422.0205169046056292386635416 +
- x *(77487.8101813068073659642177593 +
- x* (-76418.7051434147324374252002510 +
- x* (60345.2464877410927129479719750 +
- x* (-37623.7416809594729290426177664 +
- x* (18108.4544635302497336104275950 +
- x* (-6491.01004625378413321975755283 +
- x* (1631.56187150122776398754613596 +
- x* (-256.603734082675540229189858863 +
- 19.0033548929828164153801905343 *x))))))))))))))));
- }
- double mylog2_19(double x)
- {
- int w;
- x = frexp(x,&w);
- return w +94.879552359370389779311310752431677657546488670131-100.5+
- x*(38.760953171904166937692617024628986668555406124947+
- x*(-245.85943859848429206309024729452081797918448383011+
- x*(1305.0213373140979443003790999604901721351935859746+
- x*(-5483.3897186528672757505743075974516855722609750103+
- x*(18373.369285947488212376445282807142432894707031893+
- x*(-49720.330484326774507017799362836307490489909887034+
- x*(109801.21447583798026544996683803403658873989417970+
- x*(-199301.45445926506025828784277462614378129514274147+
- x*(298501.60630250569282203503684755811600024394635379+
- x*(-369205.43714028094981807593680305547807524091749705+
- x*(376227.86563934815887666995648017764570129251640536+
- x*(-314015.29189075395963525655516844515933367689469445+
- x*(212512.59498006697236627947497199497997106198214730+
- x*(-114798.02778261877254409048412664787618047864843291+
- x*(48332.575660801499026504675067647169449538315154290+
- x*(-15283.729869425976544135405204428853285998986213565+
- x*(3414.6711038764781556224963615063635427392290076089+
- x*(-480.58172520332090598267316843954641211274866464892+
- x*32.04321789652355477774462860249555604587514031906
- ))))))))))))))))));
- }
- double mylog2_pade_7_6(double x)
- {
- if (x<0) return nan("");
- int w;
- x = frexp(x,&w);
- return w+
- (-0.138283895467597308734825929 +
- x* (-4.75644115041872693502244398 +
- x *(-28.2901284421308848729647527 +
- x *(-31.020711652270330663079404 +
- x *(26.954639037773333537566195 +
- x *(32.065091647893956780806165 +
- (5.14481531055604261525940722 +
- 0.0410191440642069963727475423* x)* x))))))
- /(0.01773580523008302467852483039 +
- x *(1.066870914453938639820160162 +
- x* (11.40739806765892909737152090 +
- x* (35.97371731450040844764454601 +
- x* (38.11130966747411808773004738 +
- x* (12.86083151020947229832310848 + x))))));
- }
- int main()
- {
- stoper st;
- st.start();
- double aku=0;
- for (double x=1;x<10000;x+=1.01 )
- {
- aku = aku + log(x);
- }
- st.stop();
- st.show();
- cout<<endl;
- cout<< "log: ";
- aku+=test([](double x) {return log(x);} );
- cout<< "log10: ";
- aku+=test([](double x) {return log10(x);} );
- cout<< "log2: ";
- aku+=test([](double x) {return log2(x);} );
- cout<< "shitlog2 ";
- aku+=test(shitlog2 );
- cout<< "fastlog2 ";
- aku+=test(fastlog2 );
- cout<< "mylog2_17 ";
- aku+=test(mylog2_17 );
- cout<< "mylog2_19 ";
- aku+=test(mylog2_19 );
- cout<< "mylog2_pade_7_6 ";
- aku+=test(mylog2_pade_7_6 );
- cout<<aku<<endl;
- cout<<" x std::log2, Dshitlog Dfastlog Dmylog17 Dmylog19 Dpade7_6"<<endl;
- for (double x=0.01;x<100;x*=1.04)
- {
- cout<<x<<" "<<log2(x)<<" "<</*shitlog2(x)<<" "<<fastlog2(x)<<" "<<mylog2_17(x)<<" "<<mylog2_19(x)<<" "<<*/
- shitlog2(x)-log2(x)<<" "<<fastlog2(x)-log2(x)<<" "<<mylog2_17(x)-log2(x)<<
- " "<<mylog2_19(x)-log2(x)<<" "<<mylog2_pade_7_6(x)-log2(x)<<endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement