Java的快速超越/三角函数 [英] Fast transcendent / trigonometric functions for Java

查看:180
本文介绍了Java的快速超越/三角函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Since the trigonometric functions in java.lang.Math are quite slow: is there a library that does a quick and good approximation? It seems possible to do a calculation several times faster without losing much precision. (On my machine a multiplication takes 1.5ns, and java.lang.Math.sin 46ns to 116ns). Unfortunately there is not yet a way to use the hardware functions.

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

UPDATE: The functions should be accurate enough, say, for GPS calculations. That means you would need at least 7 decimal digits accuracy, which rules out simple lookup tables. And it should be much faster than java.lang.Math.sin on your basic x86 system. Otherwise there would be no point in it.

对于pi / 4以上的值,Java确实一些昂贵的计算以及硬件功能。这样做有充分理由,但有时你更关心速度而不是最后一位精度。

For values over pi/4 Java does some expensive computations in addition to the hardware functions. It does so for a good reason, but sometimes you care more about the speed than for last bit accuracy.

推荐答案

Hart的计算机近似值。将 Chebyshev-economized 近似公式列表为不同精度的一系列函数。

Computer Approximations by Hart. Tabulates Chebyshev-economized approximate formulas for a bunch of functions at different precisions.

修改:将我的副本从架子上取下来,结果是一本不同的书听起来非常相似。这是使用其表格的sin函数。 (在C中测试,因为这对我来说更方便。)我不知道这是否会比Java内置更快,但至少可以保证它不那么准确。 :)您可能需要先缩小参数范围;请参阅 John Cook的建议。这本书还有arcsin和arctan。

Getting my copy off the shelf, it turned out to be a different book that just sounds very similar. Here's a sin function using its tables. (Tested in C since that's handier for me.) I don't know if this will be faster than the Java built-in, but it's guaranteed to be less accurate, at least. :) You may need to range-reduce the argument first; see John Cook's suggestions. The book also has arcsin and 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的快速超越/三角函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆