C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-20-2004, 09:39 PM   #1
Carnivore ('-'v)
 
Hunter2's Avatar
 
Join Date: May 2002
Posts: 2,866
Standard PI constant

After googling for a while, I found this:
Quote:
However, it should be noted that the math constants are actually not part of the C++ standard. A standard's conforming way to get pi is to use an expression such as const double PI(4.0 * std::atan2(1.0, 1.0));.
Is this true, that there is no predefined PI constant that is part of the C++ standard?
__________________
Just Google It. √

(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
Hunter2 is offline   Reply With Quote
Old 09-20-2004, 09:56 PM   #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   Reply With Quote
Old 09-20-2004, 10:00 PM   #3
Carnivore ('-'v)
 
Hunter2's Avatar
 
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   Reply With Quote
Old 09-20-2004, 10:19 PM   #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   Reply With Quote
Old 09-20-2004, 10:22 PM   #5
Software Developer
 
jverkoey's Avatar
 
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   Reply With Quote
Old 09-20-2004, 10:58 PM   #6
VA National Guard
 
The Brain's Avatar
 
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 ;
__________________
  • "Problem Solving C++, The Object of Programming" -Walter Savitch
  • "Data Structures and Other Objects using C++" -Walter Savitch
  • "Assembly Language for Intel-Based Computers" -Kip Irvine
  • "Programming Windows, 5th edition" -Charles Petzold
  • "Visual C++ MFC Programming by Example" -John E. Swanke
  • "Network Programming Windows" -Jones/Ohlund
  • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
  • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Last edited by The Brain; 09-20-2004 at 11:00 PM.
The Brain is offline   Reply With Quote
Old 09-20-2004, 11:03 PM   #7
Toaster
 
Zach L.'s Avatar
 
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   Reply With Quote
Old 09-20-2004, 11:15 PM   #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   Reply With Quote
Old 09-21-2004, 12:04 AM   #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;
}
i got 3.000000

Code:
#include <stdio.h>

int main(void)
{
    float pi = 22;
    pi /= 7;
    printf("%f",pi);
    return 0;
}
i got 3.142857

Last edited by sand_man; 09-21-2004 at 12:08 AM.
sand_man is offline   Reply With Quote
Old 09-21-2004, 12:18 AM   #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   Reply With Quote
Old 09-21-2004, 02:27 AM   #11
and the hat of marbles
 
Sang-drax's Avatar
 
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);
Although I really would prefer
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   Reply With Quote
Old 09-21-2004, 05:13 AM   #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   Reply With Quote
Old 09-21-2004, 05:45 AM   #13
Never Exist
 
Hermitsky's Avatar
 
Join Date: Jul 2004
Posts: 149
it is a pity the Pi is not a integer
__________________

blow me ... ...
Hermitsky is offline   Reply With Quote
Old 09-21-2004, 10:36 AM   #14
Carnivore ('-'v)
 
Hunter2's Avatar
 
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   Reply With Quote
Old 09-21-2004, 12:16 PM   #15
and the hat of Destiny
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 22,495
355 / 113 is a better approximation.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:09 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22