0

Using the header <cmath>, the symbol M_PI is giving the value,

3.14159 26535 89793 11599 79635

But the correct value for Pi is,

3.14159 26535 89793 23846 26433

So how do I get the correct value of PI from M_PI?

Syramid
  • 73
  • 1
  • 7
  • Floating point math is only accurate to a certain number of digits. – Joseph Larson Oct 27 '21 at 15:11
  • 1
    Remember that floating point representation cannot accurately represent all numbers and numbers like Pi are really , really hard to accurately represent. See [if `pi_v`](https://en.cppreference.com/w/cpp/numeric/constants) is more to your liking. – user4581301 Oct 27 '21 at 15:12
  • 1
    Look at your value of `std::numeric_limits::digits10`. – Jarod42 Oct 27 '21 at 15:13
  • I know it's often there, but was `M_PI` ever Standardized? I'm not seeing it, but my Standard-Fu's not the best. – user4581301 Oct 27 '21 at 15:16
  • 1
    @user4581301 `M_PI` was not, but C++20 added `pi_v` and a bunch of others: https://en.cppreference.com/w/cpp/numeric/constants – NathanOliver Oct 27 '21 at 15:17
  • `M_PI` is not pi - just the closest `double` approximation. All `double` finite values are rational. pi is irrational. Cannot encode exactly. – chux - Reinstate Monica Oct 27 '21 at 15:17
  • That certain number of digits is given by DBL_DIG. Typically that's 15. Although by an annoying mathematical property, you get DBL_DIG + 1 96% of the time. You're in that lucky 96%. By the way, the accuracy you get is good enough to calculate the length of the orbit of Neptune to around 10cm. Not bad going really. – Bathsheba Oct 27 '21 at 15:19
  • @NathanOliver Thank you. – user4581301 Oct 27 '21 at 15:19
  • If only the [Indiana Pi Bill](https://gizmodo.com/the-eccentric-crank-who-tried-to-legislate-the-value-of-5880792) had passed, then π wouldn't have all this awkwardness. – Eljay Oct 27 '21 at 15:50
  • @Eljay I'm a big fan of changing the calendar to 13 months of 28 days. That leaves one day every year for recovering from the new years party and an extra leap day every four-or-so years. – user4581301 Oct 27 '21 at 16:44
  • @user4581301 • the [Tranquility Calendar](https://en.everybodywiki.com/Tranquility_Calendar) is one of my favorites! – Eljay Oct 27 '21 at 18:48
  • @Eljay I knew I couldn't be the only person who did the math. – user4581301 Oct 27 '21 at 18:53

0 Answers0