Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(インテル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