plasmacel plasmacel - 1 month ago 9
C++ Question

Restoring the exact angle from std::cos(angle) using std::acos

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

Answer by StoryTeller:

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.