问题描述
我知道 Math.sin()
和 Math.cos()
函数,但我是我想知道是否有一种方法可以创建(或使用已经存在的)更快的功能,因为我不关心精确定位。我正在寻求执行基本的sin或cos计算,并使其基本上尽可能快地执行。只需迭代sigma几次比 Math.sin()
?
I know about the Math.sin()
and Math.cos()
functions, but I'm wondering if there's a way I can create (or use an already-existing) a faster function, given that I don't care about pinpoint accuracy. I'm looking to execute a basic sin or cos calculation, and have it perform essentially as fast as possible. Would simply iterating the sigma a few times be any faster than Math.sin()
?
推荐答案
由于你不太关心准确性将它存储在预计算或只计算一次的表中,这就是我想要避免调用 Math 很多时候很贵。
Since you don't care much about accuracy store it in a table that is precomputed or only computed once, this is what I do when I want to avoid calls to
Math
which can be expensive when done alot.
大致
public class CosSineTable {
double[] cos = new double[361];
double[] sin = new double[361];
private static CosSineTable table = new CosSineTable();
private CosSineTable() {
for (int i = 0; i <= 360; i++) {
cos[i] = Math.cos(Math.toRadians(i));
sin[i] = Math.sin(Math.toRadians(i));
}
}
public double getSine(int angle) {
int angleCircle = angle % 360;
return sin[angleCircle];
}
public double getCos(int angle) {
int angleCircle = angle % 360;
return cos[angleCircle];
}
public static CosSineTable getTable() {
return table;
}
}
我将循环优化和方法留给你。
I leave the optimization of the loop and methods to you.
这篇关于java中的快速正弦和余弦函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!