![]() |
| | #1 | |
| Carnivore ('-'v) Join Date: May 2002
Posts: 2,866
| Standard PI constant Quote:
__________________ Just Google It. √ (\ /) ( . .) c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination. | |
| Hunter2 is offline | |
| | #2 |
| Registered User Join Date: Nov 2002
Posts: 126
| I think the M_PI macro in <cmath> is standard, but I'm not sure. |
| PorkyChop is offline | |
| | #3 |
| Carnivore ('-'v) Join Date: May 2002
Posts: 2,866
| Ahhh, just what I was thinking of.. that's actually what I was googling and boardsearching for, lol! But I'd still like to hear from the gurus if this is really standard, or if it's an unofficial extension to the <cmath> header. [self-hijack] I like the new sig, it has a mystic aura about it ![]() [/self-hijack]
__________________ Just Google It. √ (\ /) ( . .) c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination. |
| Hunter2 is offline | |
| | #4 |
| Registered User Join Date: Nov 2002
Posts: 126
| My only doubt would be that M_PI may may be one the the "math constants" that was referred to in your quote. But as far as I can remember, it has been there on every compiler I have used(maybe because they are part of C's standard but not C++?). At any rate, I think the M_ macros are pretty surefire. |
| PorkyChop is offline | |
| | #5 |
| Software Developer Join Date: Feb 2003 Location: University of Waterloo
Posts: 1,916
| "Don't complain. Everything is perfect here and you have no right." lol, that's so awesome |
| jverkoey is offline | |
| | #6 |
| VA National Guard Join Date: May 2004 Location: Manassas, VA USA
Posts: 903
| i think the floating point version of ( 22 / 7 ) might also be another way Code: float pi = 22/7 ;
__________________
Last edited by The Brain; 09-20-2004 at 11:00 PM. |
| The Brain is offline | |
| | #7 |
| Toaster Join Date: Aug 2001
Posts: 2,686
| Get an arbitrary-precision floating point number class and then just load in the first few thousand digits of pi... Who needs standards. |
| Zach L. is offline | |
| | #8 |
| Registered User Join Date: Nov 2002
Posts: 126
| >i think the floating point version of ( 22 / 7 ) might also be another way 22/7 is kind of like pseudo-PI. If you type that into a calculator, you'll get 3.142857, and the first few digits of PI are 3.1415927. It's close, but for many(probably most) situations, not close enough. |
| PorkyChop is offline | |
| | #9 |
| Registered User Join Date: May 2004
Posts: 1,362
| Code: #include <stdio.h>
int main(void)
{
float pi = 22/7;
printf("%f",pi);
return 0;
}
Code: #include <stdio.h>
int main(void)
{
float pi = 22;
pi /= 7;
printf("%f",pi);
return 0;
}
Last edited by sand_man; 09-21-2004 at 12:08 AM. |
| sand_man is offline | |
| | #10 |
| Registered User Join Date: Sep 2004 Location: California
Posts: 3,029
| Thats because you are doing integer arithmetic on the first version. Change it to: Code: #include <stdio.h>
int main(void)
{
float pi = 22.f/7;
printf("%f",pi);
return 0;
}
|
| bithub is offline | |
| | #11 |
| and the hat of marbles Join Date: May 2002 Location: Lund, Sweden
Posts: 2,057
| Hunter2, it's true. There is no M_PI constant defined in the <cmath> header according to the standard. The only macro defined in that header is HUGE_VAL. EDIT: What would be the advantage of using the atan2 function and a multiplication to calculate pi? I'd just use acos to calculate pi Code: const float pi = acos(-1); Code: const float pi = 3.141592...;
__________________ Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling Last edited by Sang-drax; 09-21-2004 at 02:36 AM. |
| Sang-drax is offline | |
| | #12 |
| Registered User Join Date: Aug 2004
Posts: 309
| Just define it manually. Nobody except for a very limited few cases needs more than, say, the first 25-30 digits of PI anyway. http://theory.cs.iitm.ernet.in/~arvindn/pi/ That's the first 1,000 digits after the decimal point. If you're really that desperate, just use a const float with those 1,000 digits. And prepare for muchos accuracy. |
| Lithorien is offline | |
| | #13 |
| Never Exist Join Date: Jul 2004
Posts: 149
| it is a pity the Pi is not a integer
__________________ blow me ... ... |
| Hermitsky is offline | |
| | #14 |
| Carnivore ('-'v) Join Date: May 2002
Posts: 2,866
| >>Just define it manually. Nobody except for a very limited few cases needs more than, say, the first 25-30 digits of PI anyway. I've actually memorized PI to about the 36'th digit or so, but where possible I prefer to not to rely on limited precision like that (whether the added precision is needed or not). And to hardcode 1000+ digits would add another 1kb to my source file, and if Joe Shmo decides to modify one digit then it's all off Problem with 22/7 is that while it's possibly very precise, it's certainly not accurate.Sang-Drax: After googling on M_PI (much more successful than PI C++ standard cmath ), I came to the same conclusion (everywhere said it was nonstandard). >>What would be the advantage of using the atan2 function and a multiplication to calculate pi? I'm not sure if there is one. Possibly the algorithm for finding acos is less accurate/precise than for atan? Anyway, the *4 multiplication would mean a potential loss of (I think) a digit or two of accuracy. I also noticed that in Visual Basic there's no acos/asin functions, you must calculate them by using atn (atan) and manipulating the result. Does anyone know more about this?
__________________ Just Google It. √ (\ /) ( . .) c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination. |
| Hunter2 is offline | |
| | #15 |
| and the hat of Destiny Join Date: Aug 2001 Location: The edge of the known universe
Posts: 22,495
| 355 / 113 is a better approximation. |
| Salem is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Standard efficiency | Jorl17 | C Programming | 3 | 06-18-2009 11:48 AM |
| array initialization & C standard | jim mcnamara | C Programming | 2 | 09-12-2008 03:25 PM |
| Bug in iterator comparison in C++ standard? | steev | C++ Programming | 14 | 07-12-2008 12:02 AM |
| C/C++ standard | confuted | A Brief History of Cprogramming.com | 3 | 07-06-2003 03:22 AM |
| standard language, standard compiler? | doubleanti | A Brief History of Cprogramming.com | 8 | 09-03-2001 04:21 AM |