473,573 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to use PI in c99

i've just found out (searching through n1124.pdf) that in c99 math.h
does not contain M_PI

what is the desired way to use the PI constant in a c code then?

#define PI 3.1415926535897 932384626433832 795
#define PI (4*atan(1))
....
?

Jul 6 '07 #1
17 12263
Szabolcs Nagy wrote:
i've just found out (searching through n1124.pdf) that in c99 math.h
does not contain M_PI

what is the desired way to use the PI constant in a c code then?

#define PI 3.1415926535897 932384626433832 795
#define PI (4*atan(1))
...
?
The problem with defining PI is that you have at least 3 different
representations of pi: float/double/long double/complex/

What would be the best representation?
And using which rounding mode?

The best thing is to define it yourself and arbitrarily decide that you
want the constant as a (say) long double.
#define PI 3.1415926535897 932384626433832 795L

jacob
Jul 6 '07 #2
Szabolcs Nagy wrote:
>
i've just found out (searching through n1124.pdf) that in c99 math.h
does not contain M_PI

what is the desired way to use the PI constant in a c code then?

#define PI 3.1415926535897 932384626433832 795
#define PI (4*atan(1))
...
?
In addition to Jacob's points, you should also consider this:

Do you want to define PI as a constant, or as something that
has to be calculated at runtime every time you use it?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Jul 6 '07 #3
Szabolcs Nagy wrote:
i've just found out (searching through n1124.pdf) that in c99 math.h
does not contain M_PI

what is the desired way to use the PI constant in a c code then?

#define PI 3.1415926535897 932384626433832 795
#define PI (4*atan(1))
....
?
There are two obvious disadvantages to the (4*atan(1)) form
a) atan(1) is not computed at compile time, so the computation of
atan(1) occurs each time PI is used
b) atan() returns a double. Your first form, which has more digits than
most implementations have for a long double, suggests an desire for
a degree of accuracy beyond the requirements even of astrophysics.
If LDBL_DIG DBL_DIG you may well find (4*atan(1)) the less
satisfactory in terms of precision.

Suppose you ran a program like this:

#include <stdio.h>
#include <float.h>
#include <math.h>

#define PI_F 3.1415926535897 932384626433832 795f
#define PI_D 3.1415926535897 932384626433832 795
#define PI_L 3.1415926535897 932384626433832 795l
#define PI_C (4*atan(1))

int main(void)
{
printf("printf PI_x as double:\n");
printf("PI_F %.*g\n", DBL_DIG, PI_F);
printf("PI_D %.*g\n", DBL_DIG, PI_D);
printf("PI_L %.*g\n", DBL_DIG, (double) PI_L);
printf("PI_C %.*g\n\n", DBL_DIG, PI_C);
printf("printf PI_x as long double:\n");
printf("PI_F %.*Lg\n", LDBL_DIG, (long double) PI_F);
printf("PI_D %.*Lg\n", LDBL_DIG, (long double) PI_D);
printf("PI_L %.*Lg\n", LDBL_DIG, PI_L);
printf("PI_C %.*Lg\n\n", LDBL_DIG, (long double) PI_C);

return 0;
}

The output for one implementation
printf PI_x as double:
PI_F 3.1415927410125 7
PI_D 3.1415926535897 9
PI_L 3.1415926535897 9
PI_C 3.1415926535897 9

printf PI_x as long double:
PI_F 3.1415927410125 7324
PI_D 3.1415926535897 9312
PI_L 3.1415926535897 9324
PI_C 3.1415926535897 9312
suggests for this implementation:
a) if float operations are faster than doubles, and no more 8
significant digits are needed
#define PI 3.14159265f
suffices
b) if double operations are at least as fast as floats, and no
more than 16 significant digits, or, whatever the relative
speed of floats and doubles, if at 9 but not more than 16
significant digits are needed
#define PI 3.1415926535897 93
is preferred
c) If the maximum possibel precision is required with a standard
type, use
#define PI 3.1415926535897 932384626433832 795l
is best.

In fact each of those defines should probably carry all the digits up to
some unneeded level of precision, since a different implementation might
well make use of more than does this implementation.

The only reason I can see for
#define PI (4*atan(1))
is that the implementation may well ensure that this has the maximum
precision and accuracy that a double can attain (but no more).
But that can probably be done with
#define PI 3.1415926535897 932384626433832 795 /* double */
better.
If you insist on using the (4*atan(1)) form, use, rather than a define,
in each function that now uses the symbol PI
const double PI = 4 * atan(1);
Straining the usual conventions on identifiers that are all caps.
Jul 6 '07 #4
jacob navia <ja***@jacob.re mcomp.frwrites:
[...]
The best thing is to define it yourself and arbitrarily decide that you
want the constant as a (say) long double.
#define PI 3.1415926535897 932384626433832 795L
This has about 102 significant bits of precision; it can silently fail
if long double happens to have a bigger mantissa than that. In most
applications, 32 digits is more than enough, but if it does matter,
it's going to be difficult to track down.

If you really need huge precision, and you need it to adapt to the
precision supported by your implementation, you can use 4*atan(1) --
or, better, 4*atanl(1) -- and arrange your code so it's only evaluated
once.

Or you can define PI as a macro, as above, and fail during compilation
if long double has more precision than you've specified:

#include <float.h>
#define PI 3.1415926535897 932384626433832 795L
#if LDBL_DIG 32
#error "Definition of PI doesn't use the full precision of long double"
#endif

Or, for most applications, you can just define PI with as many digits
as you're going to need.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 6 '07 #5
On Jul 6, 10:48 am, Martin Ambuhl <mamb...@earthl ink.netwrote:
Szabolcs Nagy wrote:
i've just found out (searching through n1124.pdf) that in c99 math.h
does not contain M_PI
what is the desired way to use the PI constant in a c code then?
#define PI 3.1415926535897 932384626433832 795
#define PI (4*atan(1))
....
?

There are two obvious disadvantages to the (4*atan(1)) form
a) atan(1) is not computed at compile time, so the computation of
atan(1) occurs each time PI is used
Would a conforming program be able to tell if it had been computed at
compile time? I'm not aware of any reason why a good compiler couldn't
do it at compile time.

Jul 6 '07 #6
"J. J. Farrell" <jj*@bcs.org.uk writes:
On Jul 6, 10:48 am, Martin Ambuhl <mamb...@earthl ink.netwrote:
>Szabolcs Nagy wrote:
i've just found out (searching through n1124.pdf) that in c99 math.h
does not contain M_PI
what is the desired way to use the PI constant in a c code then?
#define PI 3.1415926535897 932384626433832 795
#define PI (4*atan(1))
....
?

There are two obvious disadvantages to the (4*atan(1)) form
a) atan(1) is not computed at compile time, so the computation of
atan(1) occurs each time PI is used

Would a conforming program be able to tell if it had been computed at
compile time? I'm not aware of any reason why a good compiler couldn't
do it at compile time.
No, a conforming program can't tell, as long as the compile-time
computation produces the same result as a run-time computation would
have.

At least one popular C compiler actually does this computation at
compilation time, even without any command-line options to enable
optimization.

This produces some interesting behavior. If the computation is done
at compilation time, the math library is not required. If it needs to
be done at run time, the math library is required, and the program
won't link without an extra option.

<OT>gcc, "-lm"</OT>

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 6 '07 #7
"J. J. Farrell" wrote:
Martin Ambuhl <mamb...@earthl ink.netwrote:
.... snip ...
>>
There are two obvious disadvantages to the (4*atan(1)) form
a) atan(1) is not computed at compile time, so the computation
of atan(1) occurs each time PI is used

Would a conforming program be able to tell if it had been computed
at compile time? I'm not aware of any reason why a good compiler
couldn't do it at compile time.
gcc does.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com

Jul 6 '07 #8
CBFalconer wrote:
"J. J. Farrell" wrote:
>Martin Ambuhl <mamb...@earthl ink.netwrote:
... snip ...
>>There are two obvious disadvantages to the (4*atan(1)) form
a) atan(1) is not computed at compile time, so the computation
of atan(1) occurs each time PI is used
Would a conforming program be able to tell if it had been computed
at compile time? I'm not aware of any reason why a good compiler
couldn't do it at compile time.

gcc does.
And that can be quite WRONG, specially if you want
to redefine atan() to fix a broken implementation
for instance...

I am not sure all this "clever" optimizations are justified
in this case since this constant is known since ages with great
precision.

Why make it simple when you can do it complicated?

That's seems to be the motto here.
Jul 6 '07 #9
jacob navia <ja***@jacob.re mcomp.frwrites:
CBFalconer wrote:
>"J. J. Farrell" wrote:
>>Martin Ambuhl <mamb...@earthl ink.netwrote:
... snip ...
>>>There are two obvious disadvantages to the (4*atan(1)) form
a) atan(1) is not computed at compile time, so the computation
of atan(1) occurs each time PI is used
Would a conforming program be able to tell if it had been computed
at compile time? I'm not aware of any reason why a good compiler
couldn't do it at compile time.
gcc does.

And that can be quite WRONG, specially if you want
to redefine atan() to fix a broken implementation
for instance...
All identifiers with file scope in the standard headers, including
atan, are reserved for use as identifiers with external linkage (C99
7.1.3p1). If you define your own atan() function with external
linkage, you're invoking undefined behavior.

The standard reserves certain identifiers for good reasons.
I am not sure all this "clever" optimizations are justified
in this case since this constant is known since ages with great
precision.

Why make it simple when you can do it complicated?

That's seems to be the motto here.
I don't believe that gcc evaluates atan() at compilation time just to
make it easy to define pi. It does so for most or all math functions
(I've run into this for sqrt() as well). It's a perfectly ordinary
optimization.

As a programmer, of course, one probably shouldn't depend on 4*atan(1)
being evaluated at compilation time, since not all compilers will
perform that optimization -- though the penalty for making that
assumption is merely a loss of speed.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 6 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
11192
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL) on the server because of that. Our site will have an SSL certificate next week, so I would like to use AIM instead of SIM, however, I don't know how...
2
5788
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues to execute the code until the browser send his reply to the header instruction. So an exit(); after each redirection won't hurt at all
3
22981
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which field is completed.
0
8455
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. 354 roberto@ausone:Build/php-4.3.2> ldd /opt/php4/bin/php libsablot.so.0 => /usr/local/lib/libsablot.so.0 libstdc++.so.5 => ...
1
8558
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the column below. The viewer can select states from the drop down lists above the other two columns as well. If the viewer selects only one, only one...
4
18232
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the user comes back to a page where he had a submitted POST data the browser keeps telling that the data has expired and asks if repost. How to avoid...
1
6792
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url http://www.mis.gla.ac.uk/biquery/training/ but each of the courses held have maximum of 8 people that could be
2
31366
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value to :parameter I dont like the idea of making the SQL statement on the fly without binding parameters as I dont want a highly polluted SQL cache.
3
23550
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the results of the picture half the size. The PHP I have installed support 1.62 or higher. And all I would like to do is take and image and make it fit a...
0
7760
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7996
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8191
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7760
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8049
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5573
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5284
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2185
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1029
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.