Instantly share code, notes, and snippets.

# taroyabuki/MyCos.java Created Feb 9, 2016

（インテルCPUの）三角関数は不要 http://blog.unfindable.net/archives/8991
 /* org.apfloat apfloat 1.8.2 */ import org.apfloat.*; public class MyCos { public static void main(String[] args) { int c1BetterCase = 0; int c2BetterCase = 0; final int n = 10000; final long precision = 30; for (int i = 0; i < n; ++i) { double x = Math.random(); double c1 = Math.cos(x); double c2 = StrictMath.cos(x); Apfloat c = ApfloatMath.cos(new Apfloat(x, precision)); if (c1 != c2) { Apfloat delta1 = ApfloatMath.abs(c.subtract(new Apfloat(c1, precision))); Apfloat delta2 = ApfloatMath.abs(c.subtract(new Apfloat(c2, precision))); if (delta1.compareTo(delta2) < 0) { ++c1BetterCase; } else { ++c2BetterCase; } System.out.println("x = " + x); System.out.println("c1 = Math.cos(x) = " + c1); System.out.println("c2 = StrictMath.cos(x) = " + c2); System.out.println("c = ApfloatMath.cos(x) = " + c.toString(true)); System.out.println("abs(c - c1) = " + delta1); System.out.println("abs(c - c2) = " + delta2); System.out.println(); } } System.out.println("c1 is better: " + c1BetterCase); System.out.println("c2 is better: " + c2BetterCase); System.out.println("total: " + n); } }