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?

John McFarlane
  • 5,528
  • 4
  • 34
  • 38

1 Answers1

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.

John McFarlane
  • 5,528
  • 4
  • 34
  • 38
  • Wouldn't the same reasoning apply to `sqrt3 / 3`, or am I missing something? – cigien May 19 '20 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. – Raymond Chen May 19 '20 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. – Mark Ransom May 19 '20 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. – Raymond Chen May 19 '20 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. – Davis Herring May 19 '20 at 23:49