C++20 introduces standard library header, <numbers>, with definitions in namespace std::numbers
for math constants such as sqrt2
and sqrt3
. It provide inverse values like inv_sqrt3
, but not inv_sqrt2
. Why is inv_sqrt2
missing?
1 Answer
Why is
inv_sqrt2
missing?
The library defines a minimal set of commonly-used constants as precisely as the type will allow. It can be tricky to express (√3)-1 without introducing rounding errors, hence inv_sqrt3
. However, (√2)-1 can easily be expressed as: sqrt2 / 2
, so inv_sqrt2
isn't defined.
-
Wouldn't the same reasoning apply to
sqrt3 / 3
, or am I missing something?– cigienMay 19, 2020 at 21:16 -
10@cigien
sqrt2/2
suffers no loss of precision because division by 2 is exact in IEEE arithmetic. Division by 3 not so much. May 19, 2020 at 21:19 -
@RaymondChen it might not be exact, but don't IEEE arithmetic rules dictate that it will be accurate to 1 LSB? I don't see how a hard coded constant could be more accurate. May 19, 2020 at 21:24
-
1@MarkRansom The point is that given
sqrt2
with maximum possible accuracy, you can derive1/sqrt2
with maximum possible accuracy, since it's just decrementing the exponent by 1. May 19, 2020 at 21:35 -
1@MarkRansom: You round twice in forming the constant and then dividing. Only one of those is inevitable in representing whatever final value. May 19, 2020 at 23:49