问题描述
如何定义以度为参数的三角函数,而不是通常的弧度,并为这些参数计算正确的四舍五入结果?
将参数乘以<$因为 M_PI / 180.0
不是π/ 180,所以在传递给相应的弧度函数之前,c $ c> M_PI / 180.0 浮点算术手册第5.5节提供了一种方法,用π/ 180计算正确舍入的参数乘积,但有些参数仍然会使得这个乘积接近两个连续的浮点数之间的中点,然后应用即使是一个正确的圆角函数的弧度可以产生错误的最终结果。
两个单独或组合工作的策略是使用更高的精度和使用 sinpi
, cospi
, tanpi
,分别计算 sin(πx)
, cos(πx)
和 tan(πx)
。
对于后一种策略,仍然存在180除法的问题,这对许多论点来说是不准确的。
精确的策略(把参数乘以扩展π/ 180的精确表示,然后应用以弧度表示的扩展精度函数),那么确切情况可能仍然存在问题。这个定理指出了 sin
, cos
和 tan $在code> 0
中获得一个有理参数的c $ c>仅适用于弧度版本。它显然不适用于度数版本,如果对于某些浮点输入x,sindeg(x)恰好是两个连续的可表示浮点数之间的中点,那么没有任何中间精度就足以保证final结果是正确的四舍五入。
唯一有理由 q
code> cosdeg(360q)是合理的,分母为1,2,3,4或6。 Joerg Jahnel的包含了一个短而美的证明使用领域(实际上,作者使用欧拉的总体函数表征了代数数 cosdeg(360q)
的程度)。所以没有浮点数 q
,这样 cosdeg(360q)
就是两个相邻浮点数之间的一半。
所以我想答案是大约和你实现 sin
以及弧度的朋友一样,尽管@ gnasher729使得减少度数很多,更好。
How could I define trigonometric functions that take arguments in degrees instead of the usual radians, and compute correctly rounded results for these arguments?
Multiplying the argument by M_PI/180.0
before passing it to the corresponding function in radians does not work, because M_PI/180.0
is not π/180. Section 5.5 of the Handbook of Floating-Point Arithmetic offers a method to compute the correctly rounded product of the argument by π/180, but some arguments will still be such that this product is close to the midpoint between two consecutive representable floats, and then applying even a correctly rounded function in radians can produce the wrong final result.
Two strategies that may work alone or in combination are using higher precision and using the sinpi
,cospi
, tanpi
trigonometric functions from CRlibm, that compute respectively sin(πx)
, cos(πx)
and tan(πx)
.
For the latter strategy, there remains the problem of the division by 180, which is not exact for many arguments.
Regarding the higher-precision strategy (multiplying the argument by an extended-precision representation of π/180, then applying the extended-precision function in radians), there may remain a problem with "exact" cases. The theorem that states that the only rational results of sin
, cos
and tan
for a rational argument are obtained in 0
only applies to the radian versions. It obviously does not apply to the degree versions, and if for some floating-point input x, sindeg(x) is exactly the midpoint between two consecutive representable floating-point numbers, then no amount of intermediate precision is enough to guarantee that the final result is correctly rounded.
The only rationals q
for which cosdeg(360q)
is rational have 1, 2, 3, 4, or 6 as the denominator. This paper by Joerg Jahnel contains a short and beautiful proof using field theory in section 6. (Indeed, the author characterises the degree of the algebraic number cosdeg(360q)
using Euler's totient function.) So there is no floating-point q
such that cosdeg(360q)
is halfway between two adjacent floating-point numbers.
So I guess the answer is "about the same way you implement sin
and friends for radians," though @gnasher729 makes the excellent point that argument reduction for degrees is much, much nicer.
这篇关于如何正确计算以度为单位的圆角三角函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!