java fast math,java中的快速正弦和余弦函数

博客介绍了如何通过预计算并存储在一个表中来提高正弦和余弦函数的执行速度。当对精度要求不高时,这种方法可以避免频繁调用Math.sin()和Math.cos(),从而提升程序性能。作者创建了一个名为CosSineTable的类,包含预先计算好的角度对应的正弦和余弦值,以快速获取结果。
摘要由CSDN通过智能技术生成

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()?

解决方案

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.

Roughly

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值