6

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 1

9

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.

5
  • Wouldn't the same reasoning apply to sqrt3 / 3, or am I missing something?
    – cigien
    May 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 derive 1/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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.