Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance 为什么atan2(y,x)的计算速度比arcsin或arccos快?_Performance_Trigonometry - Fatal编程技术网

Performance 为什么atan2(y,x)的计算速度比arcsin或arccos快?

Performance 为什么atan2(y,x)的计算速度比arcsin或arccos快?,performance,trigonometry,Performance,Trigonometry,我已经读到,当我知道Y和X时,最好计算atan2(Y,X)来获得角度,而不是使用单个值与asin和acos。我试图深入研究math.hlib,但没有找到任何公式 有人能解释一下为什么atan2更好吗 theta = atan2(y,x); 这比: float in = 1.0/sqrt(x*x+y*y); theta = acos(x*in); if(y<0) theta = -acos(x*in); else theta = acos(y*in); 更简单,可能比at

我已经读到,当我知道Y和X时,最好计算
atan2(Y,X)
来获得角度,而不是使用单个值与asin和acos。我试图深入研究
math.h
lib,但没有找到任何公式

有人能解释一下为什么atan2更好吗

theta = atan2(y,x);
这比:

float in = 1.0/sqrt(x*x+y*y);
theta = acos(x*in);
if(y<0)
    theta = -acos(x*in);
else
    theta = acos(y*in);

更简单,可能比atan2更快。但同样,速度随着实现的不同而不同。atan2可能使用acos和asin实现,也可能不使用,或者使用更快的算法。

我猜您正在比较两段代码,它们看起来大致如下:

angle = atan2(x, y);

(我假设是C代码)

第一部分直接计算您需要的内容,而第二部分以一种迂回的方式进行计算——很自然地,第一部分会更快(除非
atan2
实现包含第二个代码示例的一些变体)


此外,
atan
是一个相当“原始”的函数,它“感觉”比“acos”或“asin”更为通用,因此,我希望
acos
的旧实现在内部使用
atan2
。此外,我对SSE了解不够,但期望SSE实现
atan2
,即使只是为了与x87兼容,也是合理的。

好吧,有明显的原因说明
atan2
更好:你不需要先计算商,一个变量为零或接近零不会有任何问题,您不必调整输出以进入正确的半圆……顺便说一句,当您提出问题时,发布代码通常是好的。这样人们就不必猜测,而且可以提供更好的答案。另外,最好为代码的语言添加一个标记。这将使该语言的专家更容易发现您的问题。您可以将您的问题包含在代码中。
angle = atan2(x, y);
angle = acos(x / sqrt(x * x + y * y));