Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
(インテルCPUの)三角関数は不要 http://blog.unfindable.net/archives/8991
//Pre build: sudo apt-get install libboost-dev
//To build: g++ quad.cpp -lquadmath
#include <cmath>
#include <ctime>
#include <quadmath.h>
#include <boost/multiprecision/cpp_dec_float.hpp>
using namespace std;
namespace mp = boost::multiprecision;
typedef mp::number<mp::cpp_dec_float<30> > decimal;
int main()
{
srand((unsigned)time(NULL));
int count = 0;
const int n = 100000;
char buf[100];
for (int i = 0; i < n; ++i) {
double x = 1.0 * rand() / RAND_MAX;
double c1;
asm("fcos" : "=&t" (c1) : "f" (x));
double c2 = cos(x);
long double c3 = cosl(x);
__float128 c4 = cosq(x);
decimal c = mp::cos(decimal(x));
if (c1 != c2) {
++count;
quadmath_snprintf(buf, 100, "%.40Qf", c4);
cout << "x = " << setprecision(20) << x << endl <<
"c1 = fcos(x) = " << c1 << endl <<
"c2 = std::cos(x) = " << c2 << endl << setprecision(25) <<
"c3 = std::cosl(x) = " << c3 << endl <<
"c4 = std::cosq(x) = " << buf << endl <<
"c = mp::cos(x) = " << c.str() << endl << setprecision(5) <<
"abs(c - c1) = " << mp::abs(c - decimal(c1)) << endl <<
"abs(c - c2) = " << mp::abs(c - decimal(c2)) << endl <<
"abs(c - c3) = " << mp::abs(c - decimal(c3)) << endl <<
"abs(c - c4) = " << mp::abs(c - decimal(buf)) << endl << endl;
}
}
cout << count << " / " << n << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment