Skip to content

Instantly share code, notes, and snippets.

@taroyabuki
Created February 9, 2016 16:28
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/200a620ceb44622bb081 to your computer and use it in GitHub Desktop.
Save taroyabuki/200a620ceb44622bb081 to your computer and use it in GitHub Desktop.
(インテルCPUの)三角関数は不要 http://blog.unfindable.net/archives/8991
/*
<dependencies>
<dependency>
<groupId>org.apfloat</groupId>
<artifactId>apfloat</artifactId>
<version>1.8.2</version>
</dependency>
</dependencies>
*/
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment