gpt4 book ai didi

java - Java的快速超越/三角函数

转载 作者:IT老高 更新时间:2023-10-28 20:57:22 25 4
gpt4 key购买 nike

由于 java.lang.Math 中的三角函数非常慢:是否有一个库可以快速且良好地进行近似?在不损失太多精度的情况下,似乎可以将计算速度提高几倍。 (在我的机器上,乘法需要 1.5ns,java.lang.Math.sin 需要 46ns 到 116ns)。不幸的是,目前还没有使用硬件功能的方法。

更新:函数应该足够准确,例如 GPS 计算。这意味着您需要至少 7 个十进制数字的准确性,这排除了简单的查找表。它应该比基本 x86 系统上的 java.lang.Math.sin 快得多。否则就没有意义了。

对于 pi/4 以上的值,Java 会执行 some expensive computations除了硬件功能。这样做是有充分理由的,但有时您更关心速度而不是最后一点的准确性。

最佳答案

Computer Approximations通过哈特。表格 Chebyshev-economized一组不同精度的函数的近似公式。

编辑: 将我的副本下架,结果是 a different book听起来很相似。这是一个使用其表的 sin 函数。 (在 C 中测试,因为这对我来说更方便。)我不知道这是否会比 Java 内置更快,但至少可以保证它不那么准确。 :) 您可能需要先对参数进行范围缩小;见 John Cook's suggestions .本书还有arcsin和arctan。

#include <math.h>
#include <stdio.h>

// Return an approx to sin(pi/2 * x) where -1 <= x <= 1.
// In that range it has a max absolute error of 5e-9
// according to Hastings, Approximations For Digital Computers.
static double xsin (double x) {
double x2 = x * x;
return ((((.00015148419 * x2
- .00467376557) * x2
+ .07968967928) * x2
- .64596371106) * x2
+ 1.57079631847) * x;
}

int main () {
double pi = 4 * atan (1);
printf ("%.10f\n", xsin (0.77));
printf ("%.10f\n", sin (0.77 * (pi/2)));
return 0;
}

关于java - Java的快速超越/三角函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/523531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com