Instantly share code, notes, and snippets.

# taroyabuki/quad.cpp Last active Feb 8, 2016

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 #include #include #include using namespace std; namespace mp = boost::multiprecision; typedef mp::number > 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; }