473,806 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

algorithm for finding Pi in C

42 3835
On Fri, 23 May 2008 20:53:16 -0700 (PDT), aa*****@gmail.c om wrote in
comp.lang.c:
Hi all,

see:- http://mathforum.org/library/drmath/view/54456.html
Even better:

double pi = acos(-1);

Of course, if you want it outside of a function, you can't initialize
it with a function, so instead:

double pi;

int main(void)
{
pi = acos(-1);
/* stuff */
return 0;
}

Don't forget to include <math.h>, and you might have to do some extra
work in linking if you work on an antique system.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jun 27 '08 #11
On Sat, 24 May 2008 22:31:55 -0500, Jack Klein wrote:
On Fri, 23 May 2008 20:53:16 -0700 (PDT), aa*****@gmail.c om wrote in
comp.lang.c:
>Hi all,

see:- http://mathforum.org/library/drmath/view/54456.html

Even better:

double pi = acos(-1);

Of course, if you want it outside of a function, you can't initialize it
with a function, so instead:

double pi;

int main(void)
{
pi = acos(-1);
/* stuff */
return 0;
}
Or take it a step further and copy and paste the output of the following
program into a suitable header file.

/* pi.c */
#include <math.h>
#include <stdio.h>
int main(void)
{
printf("#define PI %.32f\n", acos(-1.0));
return 0;
}

Output:

#define PI 3.1415926535897 931159979634685 4419
Don't forget to include <math.h>, and you might have to do some extra
work in linking if you work on an antique system.
s/antique/linux

jaysome@ubuntu:/tmp$ gcc -o pi pi.c
/tmp/ccIx5hjS.o: In function `main':
pi.c:(.text+0x1 c): undefined reference to `acos'
collect2: ld returned 1 exit status

Of course the way to fix this is explained in the C FAQ:

jaysome@ubuntu:/tmp$ gcc -o pi pi.c -lm

Regards
--
jay

Jun 27 '08 #12
Andy G. wrote:
On Sat, 24 May 2008 22:31:55 -0500, Jack Klein wrote:
>On Fri, 23 May 2008 20:53:16 -0700 (PDT), aa*****@gmail.c om wrote in
comp.lang.c:
>>Hi all,

see:- http://mathforum.org/library/drmath/view/54456.html
Even better:

double pi = acos(-1);

Of course, if you want it outside of a function, you can't initialize it
with a function, so instead:

double pi;

int main(void)
{
pi = acos(-1);
/* stuff */
return 0;
}

Or take it a step further and copy and paste the output of the following
program into a suitable header file.

/* pi.c */
#include <math.h>
#include <stdio.h>
int main(void)
{
printf("#define PI %.32f\n", acos(-1.0));
return 0;
}

Output:

#define PI 3.1415926535897 931159979634685 4419
That's wrong.
Pi is 3.1415926535897 932384626433832 7950
s/antique/linux

jaysome@ubuntu:/tmp$ gcc -o pi pi.c
/tmp/ccIx5hjS.o: In function `main':
pi.c:(.text+0x1 c): undefined reference to `acos'
collect2: ld returned 1 exit status

Of course the way to fix this is explained in the C FAQ:

jaysome@ubuntu:/tmp$ gcc -o pi pi.c -lm

Regards

--
pete
Jun 27 '08 #13
"Andy G." <an******@spamc op.netwrites:
[...]
Or take it a step further and copy and paste the output of the following
program into a suitable header file.

/* pi.c */
#include <math.h>
#include <stdio.h>
int main(void)
{
printf("#define PI %.32f\n", acos(-1.0));
return 0;
}

Output:

#define PI 3.1415926535897 931159979634685 4419
#include <float.h>
#define PI 3.1415926535897 923284626433832 795028841971693 993751058209749 44592
#if LDBL_DIG 64
#error "Need more digits for PI"
#endif

Or you can write code to compute it for you if you're concerned that
the value might change.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #14
Keith Thompson said:
"Andy G." <an******@spamc op.netwrites:
<snip>
> printf("#define PI %.32f\n", acos(-1.0));
return 0;
}

Output:

#define PI 3.1415926535897 931159979634685 4419
His program's first error is in the 17th digit (the 16th decimal place).
#include <float.h>
#define PI
#3.141592653589 792328462643383 279502884197169 399375105820974 944592
Your first error, however, is in the 16th digit (the 15th decimal place).
:-p

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #15
On May 25, 11:15 am, Keith Thompson <ks...@mib.orgw rote:
Richard Heathfield <r...@see.sig.i nvalidwrites:
Keith Thompson said:
"Andy G." <andyg...@spamc op.netwrites:
<snip>
> printf("#define PI %.32f\n", acos(-1.0));
return 0;
}
>Output:
>#define PI 3.1415926535897 931159979634685 4419
His program's first error is in the 17th digit (the 16th decimal place).
#include <float.h>
#define PI
#3.141592653589 792328462643383 279502884197169 399375105820974 944592
Your first error, however, is in the 16th digit (the 15th decimal place).
:-p

Whoops. s/232/323/

(His error was due to rounding; mine was a typo.)
.... typo? You don't remember PI up to all these decimal places, do you?
Jun 27 '08 #16
vi******@gmail. com wrote:
Keith Thompson <ks...@mib.orgw rote:
>Richard Heathfield <r...@see.sig.i nvalidwrites:
>>Keith Thompson said:
"Andy G." <andyg...@spamc op.netwrites:
.... snip ...
>>
>>>>#define PI 3.1415926535897 931159979634685 4419
>>His program's first error is in the 17th digit (the 16th
decimal place).
>>>#include <float.h>
#define PI
#3.141592653 589792328462643 383279502884197 169399375105820 974944592
>>Your first error, however, is in the 16th digit (the 15th
decimal place). :-p

Whoops. s/232/323/

(His error was due to rounding; mine was a typo.)

... typo? You don't remember PI up to all these decimal places,
do you?
Of course he does. These are c.l.c regulars you are speaking to.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #17
vi******@gmail. com writes:
On May 25, 11:15 am, Keith Thompson <ks...@mib.orgw rote:
>Richard Heathfield <r...@see.sig.i nvalidwrites:
Keith Thompson said:
>"Andy G." <andyg...@spamc op.netwrites:
<snip>
>> printf("#define PI %.32f\n", acos(-1.0));
return 0;
}
>>Output:
>>#define PI 3.1415926535897 931159979634685 4419
His program's first error is in the 17th digit (the 16th decimal place).
>#include <float.h>
#define PI
#3.14159265358 979232846264338 327950288419716 939937510582097 4944592
Your first error, however, is in the 16th digit (the 15th decimal place).
:-p

Whoops. s/232/323/

(His error was due to rounding; mine was a typo.)
... typo? You don't remember PI up to all these decimal places, do you?
Yes. From memory:

3.1415926535897 932384626433832 795028841971693 993751058209749 445923078164

I used fewer digits above to avoid making the line too long.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #18
On May 24, 5:41*pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
spinoza1111 said:
On May 24, 2:44 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
Presumably you want Pi? Easy. It's about 3.
I'm disappointed no one in the thread mentioned the
almost unbelievable expression due to Ramanujan:
\frac{1}{\pi} = \frac{2\sqrt{2} }{9801}
\sum^\infty_{k= 0} \frac{(4k)!(110 3+26390k)}{(k!) ^4 396^{4k}}

Admittedly, this formula may have little interest for those
for whom "about 3" is an adequate approximation.
No (but somehow I get the feeling that either I'm misinterpreting your
question, or you're going to misinterpret my answer, or perhaps both).
Or perhaps even all three.

James Dow Allen
Jun 27 '08 #19
James Dow Allen said:
On May 24, 5:41 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
>spinoza1111 said:
On May 24, 2:44 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
Presumably you want Pi? Easy. It's about 3.

I'm disappointed no one in the thread mentioned the
almost unbelievable expression due to Ramanujan:
\frac{1}{\pi} = \frac{2\sqrt{2} }{9801}
\sum^\infty_{k= 0} \frac{(4k)!(110 3+26390k)}{(k!) ^4 396^{4k}}
But someone did mention it. In case you missed the article, the message ID
is <4a************ *************** *******@f24g200 0prh.googlegrou ps.com-
check it out!
Admittedly, this formula may have little interest for those
for whom "about 3" is an adequate approximation.
<grinWell, "about 3" *is* an adequate approximation - on occasion. That
doesn't mean it's adequate for *all* uses.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #20

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

Similar topics

22
9938
by: Bernard Fields | last post by:
Greets, all. As the title suggests, I'm trying to make a maze. Specifically, it's a top-down, 2-d maze, preferably randomly generated, though I'm willing to forego that particular aspect as this point. I've done many, many web-searches, but nothing that I've found so far has provided any clues....
3
2759
by: ckroom | last post by:
Anyone knows a good algorithm to get the next prime of a number n? prime = nextprime( n ); It's for some stuff about double rehashing, thanks. -- ckroom http://nazaries.net/~ckroom
4
5638
by: m sergei | last post by:
I am not asking for code but wanted help with understanding the algorithm to permute all characters of a string. say string is "ABCD" I want to know the algorithm for finding all permutations of the given string, without recursion and with recursion.
12
14872
by: Erik the Red | last post by:
In Fundamental Algorithms (The Art of Computer Programming), the first algorithm discussed is Euclid's Algorithm. The only idea I have of writing this in python is that it must involve usage of the modulo % sign. How do I write this in python?
16
6773
by: a | last post by:
We are writing an app that assigns people to teams based on their curent score. Teams are 8 people, there are 2 teams. (i would like it to be flexible, but this is a start). I need an algorithm that creates the 2 teams such that the total score for each team is as close as possible. Any ideas?? Thanks!
2
2150
by: Sherrie Laraurens | last post by:
Hi all, I'm trying to write a generic algorithm routine that will take begin and end iterators of a container, iterate through the range and perform a "calculation" of sorts. The trouble is that the algorithm will behave differently for the two different types. I've considered calling the algorithm foo_A and foo_B, but I don't like that approach because it will blow out in naming complexity down the track.
4
2642
by: sklett | last post by:
I realize this could be a little off-topic, but there are some great minds on this NG and I hope you can let me slide this time ;0) I'm designing our system to manage what products can fit in which cartons and how many per carton. For example: Widget A measures 10W x 10H x 10D Carton A measures 20W x 10H x 10D Carton B measures 50W x 10H x 10D If I'm fulfilling an order for 2 Widget As I can choose Carton A as it will
4
32087
prometheuzz
by: prometheuzz | last post by:
Hello (Java) enthusiasts, In this article I’d like to tell you a little bit about graphs and how you can search a graph using the BFS (breadth first search) algorithm. I’ll address, and hopefully answer, the following questions: • what is a graph? • how can a graph be represented as an ADT? • how can we search/walk through a graph using the BFS algorithm?
2
4664
by: shashankbs | last post by:
Given a topology and a certain node X, find the shortest path tree with X as the root. * Input: a topology file similar to the following (which is a three-node ring) -------------------------------------------- Nodes: (3) // there are three nodes (node 0, 1 and 2) Edges: (3)
1
14215
by: Glenton | last post by:
Hi All Here is a very simple little class for finding a shortest route on a network, following Dijkstra's Algorithm: #!/usr/bin/env python #This is meant to solve a maze with Dijkstra's algorithm from numpy import inf from copy import copy
0
9719
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10618
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10371
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10110
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3008
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.