Skip to content

Instantly share code, notes, and snippets.

@taroyabuki
Last active June 8, 2017 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taroyabuki/a1435c9be2416223c7c5 to your computer and use it in GitHub Desktop.
Save taroyabuki/a1435c9be2416223c7c5 to your computer and use it in GitHub Desktop.
(インテルCPUの)三角関数は不要 http://blog.unfindable.net/archives/8991
//On Ubuntu, you need:
//sudo apt-get install libboost-dev
#include <iostream>
#include <cmath>
#include <ctime>
#include <boost/multiprecision/cpp_bin_float.hpp>
using namespace std;
using namespace boost::multiprecision;
typedef cpp_bin_float_100 REAL;
int main()
{
srand((unsigned)time(NULL));
int c1BetterCase = 0;
int c2BetterCase = 0;
const int n = 100000;
for (int i = 0; i < n; ++i) {
double x = 1.0 * rand() / RAND_MAX;
double c1 = 0;
#ifdef __GNUC__
asm("fcos" : "=&t" (c1) : "f" (x));
#elif _MSC_VER
__asm {
fld x;
fcos;
fstp c1;
}
#endif
double c2 = cos(x);
REAL c = cos(REAL(x));
if (c1 != c2) {
REAL delta1 = abs(c - REAL(c1));
REAL delta2 = abs(c - REAL(c2));
if (delta1 < delta2) ++c1BetterCase;
else ++c2BetterCase;
cout << "x = " << setprecision(20) << x << endl <<
"c1 = fcos(x) = " << c1 << endl <<
"c2 = std::cos(x) = " << c2 << endl <<
"c = mp::cos(x) = " << c.str() << endl << setprecision(5) <<
"abs(c - c1) = " << delta1 << endl <<
"abs(c - c2) = " << delta2 << endl << endl;
}
}
cout << "c1 is better: " << c1BetterCase << endl;
cout << "c2 is better: " << c2BetterCase << endl;
cout << "total: " << n << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment