使用 std::acos 从 std::cos(angle) 恢复精确角度
Restoring the exact angle from std::cos(angle) using std::acos
C++标准是否保证angle == std::acos(std::cos(angle))
如果angle
在[0,Pi]范围内,或者换句话说是否可以恢复[=的精确原始值13=] 从 std::cos
的结果使用 std::acos
给定提到的范围限制?
忽略angle
为infinity
或NaN
的边缘情况。
说书人的回答:
The standard cannot make that guarantee, simply because the result of std::cos
may not be representable exactly by a double
, so you get a truncation error, which will affect the result of std::acos
.
来自cppreference.com:
” If no errors occur, [acos returns] the arc cosine of arg (arccos(arg)) in the range [0 ; π]
以度为单位,从 0 到 180(含),对应余弦值 1 到 -1(含)。
在这个范围之外你甚至无法得到一个大概的对应关系。计算余弦 discards 关于您超出该范围的角度的信息。无法取回该信息。
如何丢弃信息:
首先,对于任意整数 K,cos(x) = cos(K*360 + x)。其次,cos(x) = cos(-x)。这加起来产生相同余弦值的角度值非常多。
此外,即使所有读者可能都知道这一点,但为了完整起见:由于正弦和余弦是非常无理数,通常不是简单的分数,所以除了可能对应于 0 的余弦 1 外,您不能期望得到准确的结果度。
即使在数学上这也是不可能的。例如,cos(2*PI)
是 0,但 cos(4*PI)
.
也是
按照标准:
This International Standard imposes no requirements on the accuracy
of floating-point operations; see also 18.3.2. — end note ]
http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/n4606.pdf
C++标准是否保证angle == std::acos(std::cos(angle))
如果angle
在[0,Pi]范围内,或者换句话说是否可以恢复[=的精确原始值13=] 从 std::cos
的结果使用 std::acos
给定提到的范围限制?
忽略angle
为infinity
或NaN
的边缘情况。
说书人的回答:
The standard cannot make that guarantee, simply because the result of
std::cos
may not be representable exactly by adouble
, so you get a truncation error, which will affect the result ofstd::acos
.
来自cppreference.com:
” If no errors occur, [acos returns] the arc cosine of arg (arccos(arg)) in the range [0 ; π]
以度为单位,从 0 到 180(含),对应余弦值 1 到 -1(含)。
在这个范围之外你甚至无法得到一个大概的对应关系。计算余弦 discards 关于您超出该范围的角度的信息。无法取回该信息。
如何丢弃信息:
首先,对于任意整数 K,cos(x) = cos(K*360 + x)。其次,cos(x) = cos(-x)。这加起来产生相同余弦值的角度值非常多。
此外,即使所有读者可能都知道这一点,但为了完整起见:由于正弦和余弦是非常无理数,通常不是简单的分数,所以除了可能对应于 0 的余弦 1 外,您不能期望得到准确的结果度。
即使在数学上这也是不可能的。例如,cos(2*PI)
是 0,但 cos(4*PI)
.
按照标准:
This International Standard imposes no requirements on the accuracy of floating-point operations; see also 18.3.2. — end note ]
http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/n4606.pdf