我如何解释舍入误差在浮点运算的反三角函数(和开方)功能(C语言)? [英] How can I account for round-off errors in floating-point arithmetic for inverse trig (and sqrt) functions (in C)?

查看:204
本文介绍了我如何解释舍入误差在浮点运算的反三角函数(和开方)功能(C语言)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当复杂的函数,它是重新present两个向量形式(大小,经度,纬度),其中纬度和经度弧度的三维空间,和一个角度几个双重价值。该功能的目的是旋转所指定的角度围绕第二第一矢量,并返回所得到的载体。我已经验证了code是逻辑上是正确和有效。

I have a fairly complicated function that takes several double values that represent two vectors in 3-space of the form (magnitude, latitude, longitude) where latitude and longitude are in radians, and an angle. The purpose of the function is to rotate the first vector around the second by the angle specified and return the resultant vector. I have already verified that the code is logically correct and works.

函数的预期目的,是为图形,所以双precision是没有必要的;然而,目标平台,三角函数(和开方),该取浮标(SINF,cosf,atan2f,asinf,acosf和sqrtf特别)比浮体(更快上双打工作功能上可能是因为指令以计算这些值实际上可能需要一双;如果浮动传递,值必须被转换为双,这就要求它有更多的内存复制到一个区域 - 即开销)。其结果是,所有参与的函数的变量是双precision

The expected purpose of the function is for graphics, so double precision is not necessary; however, on the target platform, trig (and sqrt) functions that take floats (sinf, cosf, atan2f, asinf, acosf and sqrtf specifically) work faster on doubles than on floats (probably because the instruction to calculate such values may actually require a double; if a float is passed, the value must be cast to a double, which requires copying it to an area with more memory -- i.e. overhead). As a result, all of the variables involved in the function are double precision.

下面是问题:我想我的优化功能,以便它可以被称为每秒倍。因此,我已经取代了呼叫正弦,余弦,开方,等等通过调用这些函数的浮点版本,因为它们导致总体3-4倍的速度增加。这适用于几乎所有的投入;然而,如果输入矢量接近与标准单位矢量(I,J,或k),以平行的,用于各种功能的舍入误差累积足以引起以后调用sqrtf或逆三角函数(asinf,acosf, atan2f)来传递是的参数只是勉强的这些函数的域之外。

Here is the issue: I am trying to optimize my function so that it can be called more times per second. I have therefore replaced the calls to sin, cos, sqrt, et cetera with calls to the floating point versions of those functions, as they result in a 3-4 times speed increase overall. This works for almost all inputs; however, if the input vectors are close to parallel with the standard unit vectors (i, j, or k), round-off errors for the various functions build up enough to cause later calls to sqrtf or inverse trig functions (asinf, acosf, atan2f) to pass arguments that are just barely outside of the domain of those functions.

所以,我留下了这个两难问题:要么我只能叫双precision功能,避免该问题(以及每秒约130万矢量操作的限制结束),或者我可以尝试拿出与别的东西。最后,我想一个办法来清理输入逆三角函数照顾边缘的情况下(这是平凡的这样做的开方:只使用ABS)。分支是不是一种选择,因为即使一个条件语句增添了非常多的开销,任何性能提升都将丢失。

So, I am left with this dilemma: either I can only call double precision functions and avoid the problem (and end up with a limit of about 1,300,000 vector operations per second), or I can try to come up with something else. Ultimately, I would like a way to sanitize the input to the inverse trig functions to take care of edge cases (it is trivial for do so for sqrt: just use abs). Branching is not an option, as even a single conditional statement adds so much overhead that any performance gains are lost.

所以,任何想法?

编辑:有人前pressed混乱我用双打与浮点运算。功能多,如果我确实比存储,如果我把它们存储在浮动大小的容器中的双尺寸的容器我所有的值(即双类型的变量)更快。但是,浮点precision trig的操作比双precision trig的操作,原因很明显更快。

someone expressed confusion over my using doubles versus floating point operations. The function is much faster if I actually store all my values in double-size containers (I.E. double-type variables) than if I store them in float-size containers. However, floating point precision trig operations are faster than double precision trig operations for obvious reasons.

推荐答案

基本上,你需要找到一个数字稳定算法,解决您的问题。有对这种事情没有通用的解决方案,它需要针对特定​​情况下使用的概念做如条件数中,如果各个步骤。它可能实际上是不可能的,如果根本的问题本身是病态的。

Basically, you need to find a numerically stable algorithm that solves your problem. There are no generic solutions to this kind of thing, it needs to be done for your specific case using concepts such as the condition number if the individual steps. And it may in fact be impossible if the underlying problem is itself ill-conditioned.

这篇关于我如何解释舍入误差在浮点运算的反三角函数(和开方)功能(C语言)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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