Is it guaranteed by the C++ standard that angle == std::acos(std::cos(angle))
if angle
is in the range [0, Pi], or in other words is it possible to restore the exact original value of angle
from the result of std::cos
using std::acos
given the mentioned range limit?
The marginal cases when angle
is infinity
or NaN
are omitted.
Answer by StoryTeller:
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
.
From cppreference.com:
” If no errors occur, [acos returns] the arc cosine of arg (arccos(arg)) in the range [0 ; π]
In degrees, that's 0 to 180, inclusive, corresponding to cosine values 1 down through -1, inclusive.
Outside that range you can't even get an approximate correspondence. Computing the cosine discards information about which angle you had outside of that range. There's no way to get that information back.
How information is discarded:
First, in degrees, cos(x) = cos(K*360 + x), for arbitrary integer K. Secondly, cos(x) = cos(-x). This adds up to an awful lot of angle values that produce the same cosine value.
Also, even though all readers likely know this, but for completeness: since sines are cosines are very irrational numbers, generally not simple fractions, you can't expect exact results except for maybe cosine 1, which corresponds to 0 degrees.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With