Does C++ have a standard symbol representing the value Pi?

S

Steven T. Hatton

I can get the accurate value of Pi to an arbitrary precision, but it would
be nice to have it directly and readily available as a standard feature of
the language. Have I overlooked something, or is it not there?

ISO/IEC14882:2003(E) offers me this:

template<class T> complex<T> log(const complex<T>& x);

"4 Notes: the branch cuts are along the negative real axis.
5 Returns: the complex natural (base e) logarithm of x, in the range of a
strip mathematically unbounded along the real axis and in the interval [-i
times pi, i times pi ] along the imaginary axis. When x is a negative real
number, imag(log(x)) is pi."

3.1415926535897932384626433832795028841971693993751058209749445923078164062862\
089986280348253421170679821480865132823066470938446095505822317253594081284811\
174502841027019385211055596446229489549303819644288109756659334461284756482337\
8678316527120190914564856692346034861045432664821339360726024914127
 
A

Alf P. Steinbach

* "Steven T. Hatton said:
I can get the accurate value of Pi to an arbitrary precision, but it would
be nice to have it directly and readily available as a standard feature of
the language. Have I overlooked something, or is it not there?

It's not there in C (at least pre 1999), so it's not there in C++.

As a non-standard extension you might find M_PI in your <cmath>.

See <url: http://www.eskimo.com/~scs/C-faq/q14.8.html>.


ISO/IEC14882:2003(E) offers me this:

template<class T> complex<T> log(const complex<T>& x);

"4 Notes: the branch cuts are along the negative real axis.
5 Returns: the complex natural (base e) logarithm of x, in the range of a
strip mathematically unbounded along the real axis and in the interval [-i
times pi, i times pi ] along the imaginary axis. When x is a negative real
number, imag(log(x)) is pi."

3.1415926535897932384626433832795028841971693993751058209749445923078164062862\
089986280348253421170679821480865132823066470938446095505822317253594081284811\
174502841027019385211055596446229489549303819644288109756659334461284756482337\
8678316527120190914564856692346034861045432664821339360726024914127

Only a mathematician could have thunk of that! ;-)

Why not acos?

And btw., have you checked the digits after the twentieth? (I haven't,
but suspect garbage there.)
 
M

marbac

Hi,

pi is an "invinite" number. If you want do use pi within your program,
you have to define the precision yourself (depending on your
requirements). Have a look at analysis 1.

regards marbac
 
C

Claudio Puviani

Alf P. Steinbach said:
* "Steven T. Hatton said:
I can get the accurate value of Pi to an arbitrary precision, but it would
be nice to have it directly and readily available as a standard feature of
the language. Have I overlooked something, or is it not there?

It's not there in C (at least pre 1999), so it's not there in C++.

As a non-standard extension you might find M_PI in your <cmath>.

See <url: http://www.eskimo.com/~scs/C-faq/q14.8.html>.


ISO/IEC14882:2003(E) offers me this:

template<class T> complex<T> log(const complex<T>& x);

"4 Notes: the branch cuts are along the negative real axis.
5 Returns: the complex natural (base e) logarithm of x, in the range of a
strip mathematically unbounded along the real axis and in the interval [-i
times pi, i times pi ] along the imaginary axis. When x is a negative real
number, imag(log(x)) is pi."

3.1415926535897932384626433832795028841971693993751058209749445923078164062862\
089986280348253421170679821480865132823066470938446095505822317253594081284811\
174502841027019385211055596446229489549303819644288109756659334461284756482337\
8678316527120190914564856692346034861045432664821339360726024914127

Only a mathematician could have thunk of that! ;-)

Why not acos?

And btw., have you checked the digits after the twentieth? (I haven't,
but suspect garbage there.)

The digits are good, including proper rounding of the last 7. What good this
would do in the real world is certainly debatable.

Claudio Puviani
 
S

Steven T. Hatton

marbac said:
Hi,

pi is an "invinite" number.

Did you mean /infinite/? "Invinite" is not in any of the dictionaries I
queried, and it doesn't show on a google, except as a misspelling.
If you want do use pi within your program,
you have to define the precision yourself (depending on your
requirements).

There could almost certainly be a way to provide a standard representation
of Pi. There may be a need for a PI_d, and a PI_f, but I don't see why
there is a fundamental reason it could not be part of the standard.
Perhaps I should take this over to comp.std.c++.
Have a look at analysis 1.

I don't follow? Are you talking about "analysis" as in /real analysis/?
 
J

Jacek Dziedzic

Claudio said:
The digits are good, including proper rounding of the last 7. What good this
would do in the real world is certainly debatable.

Haven't you seen the movie "Pi"? :))

- J.
 
S

Steven T. Hatton

Claudio said:
I haven't, but I intend to rent it some time. I wonder if it's a sequel to
the movie 'e': http://www.imdb.com/title/tt0132667/

;-)

Claudio Puviani

Then there's the sequel to Pi:

hattons@ljosalfr:~/code/c++/scratch/forward/
Wed Apr 28 17:58:06:> echo "N[Pi,600]" | math
Mathematica 5.0 for Linux
Copyright 1988-2003 Wolfram Research, Inc.
-- Motif graphics initialized --

In[1]:=
Out[1]=
3.1415926535897932384626433832795028841971693993751058209749445923078\
 
L

Leor Zolman

I haven't, but I intend to rent it some time. I wonder if it's a sequel to the
movie 'e': http://www.imdb.com/title/tt0132667/

Looks like the title of that movie is E, not e, so it probably isn't
related ;-)

As for Pi (the title is actually the Greek letter, but I can't figure out
how to insert it at all with Agent, let alone in a way that would allow
anyone else to read it...), if you suffer from migraines, like I do, you'll
/really/ relate to this movie... I'd put it up there somewhere with Repo
Man and Brazil (but not quite as good as either).
-leor
 
M

marbac

Did you mean /infinite/? "Invinite" is not in any of the dictionaries I
queried, and it doesn't show on a google, except as a misspelling.

Yes, i meant "infinite" (sorry, was a typing error)


There could almost certainly be a way to provide a standard representation
of Pi. There may be a need for a PI_d, and a PI_f, but I don't see why
there is a fundamental reason it could not be part of the standard.
Perhaps I should take this over to comp.std.c++.


Someone wrote it before, some constants are defined in math.h (cmath).


/* Some useful constants. */
#if defined __USE_BSD || defined __USE_XOPEN
# define M_E 2.7182818284590452354 /* e */
# define M_LOG2E 1.4426950408889634074 /* log_2 e */
# define M_LOG10E 0.43429448190325182765 /* log_10 e */
# define M_LN2 0.69314718055994530942 /* log_e 2 */
# define M_LN10 2.30258509299404568402 /* log_e 10 */
# define M_PI 3.14159265358979323846 /* pi */
# define M_PI_2 1.57079632679489661923 /* pi/2 */
# define M_PI_4 0.78539816339744830962 /* pi/4 */
# define M_1_PI 0.31830988618379067154 /* 1/pi */
# define M_2_PI 0.63661977236758134308 /* 2/pi */
# define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */
# define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
# define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
#endif

/* The above constants are not adequate for computation using `long
double's.
Therefore we provide as an extension constants with similar names as a
GNU extension. Provide enough digits for the 128-bit IEEE quad. */
#ifdef __USE_GNU
# define M_El 2.7182818284590452353602874713526625L /* e */
# define M_LOG2El 1.4426950408889634073599246810018921L /* log_2 e */
# define M_LOG10El 0.4342944819032518276511289189166051L /* log_10
e */
# define M_LN2l 0.6931471805599453094172321214581766L /* log_e 2 */
# define M_LN10l 2.3025850929940456840179914546843642L /* log_e
10 */
# define M_PIl 3.1415926535897932384626433832795029L /* pi */
# define M_PI_2l 1.5707963267948966192313216916397514L /* pi/2 */
# define M_PI_4l 0.7853981633974483096156608458198757L /* pi/4 */
# define M_1_PIl 0.3183098861837906715377675267450287L /* 1/pi */
# define M_2_PIl 0.6366197723675813430755350534900574L /* 2/pi */
# define M_2_SQRTPIl 1.1283791670955125738961589031215452L /*
2/sqrt(pi) */
# define M_SQRT2l 1.4142135623730950488016887242096981L /* sqrt(2) */
# define M_SQRT1_2l 0.7071067811865475244008443621048490L /*
1/sqrt(2) */
#endif




I don't follow? Are you talking about "analysis" as in /real analysis/?

whats an irrational number .. thats what i wanted to say.

It makes no sense if you define Pi with ..... lets say 50 digits (in
this case: base 10) if you not define a datatype in this range for all
your variables.

simple example: 2*pi*r ...... what is the accuracy of pi (example long
double) worth, if r (example float) is only of low accuracy?


regards marbac
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top