473,797 Members | 3,148 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

algorithm for finding Pi in C

42 3833
aa*****@gmail.c om wrote:
Hi all,

see:- http://mathforum.org/library/drmath/view/54456.html
This works for me:

#include <float.h>
/*
** pi == (atan(1.0 / 3) + atan(1.0 / 2)) * 4
*/
static double fs_pi(void);

static double fs_pi(void)
{
unsigned n;
double a, b;
static double p;
static int initialized;

if (!initialized) {
initialized = 1;
n = 1;
a = 3;
do {
a /= 9;
b = a / n;
n += 2;
a /= 9;
b -= a / n;
n += 2;
p += b;
} while (b DBL_EPSILON / 4);
n = 1;
a = 2;
do {
a /= 4;
b = a / n;
n += 2;
a /= 4;
b -= a / n;
n += 2;
p += b;
} while (b DBL_EPSILON / 2);
p *= 4;
}
return p;
}

--
pete
Jun 27 '08 #2
aa*****@gmail.c om said:
Hi all,

see:- http://mathforum.org/library/drmath/view/54456.html
Presumably you want Pi? Easy. It's about 3.

--
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 #3
On May 24, 2:44*pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
aark...@gmail.c om said:
Hi all,
see:-http://mathforum.org/library/drmath/view/54456.html

Presumably you want Pi? Easy. It's about 3.
So we doan' need no arbitrary "unlimited" "limited only by time and
memory space" precision?

Wouldn't 3.14 be a better answer in all cases? That's what most
"normal geeks" remember from math class.

And isn't usually a #define const?
>
--
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 #4
spinoza1111 said:
On May 24, 2:44 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
>aark...@gmail. com said:
Hi all,
see:-http://mathforum.org/library/drmath/view/54456.html

Presumably you want Pi? Easy. It's about 3.

So we doan' need no arbitrary "unlimited" "limited only by time and
memory space" precision?
It depends. Sometimes we do, and sometimes we don't.
Wouldn't 3.14 be a better answer in all cases?
No, not in all cases. In some cases, "about 3" is far superior, although
admittedly "just over 3" is even better. Case in point: you're parked by
the lake, looking at a map thereof. Using your thumb against the map scale
indicator and then against the lake, you can see that it's about a mile
across, and roughly circularish. There's a path all the way round. In this
kind of terrain (reasonably flat, for obvious reasons) you can manage,
say, 4mph. Your time, however, is not unlimited. Have you got time to walk
around the lake? In such a situation, taking pi as "three-and-a-bit" is
far more appropriate than the more pernickety 3.14.

Note that, as an estimate of pi, 3 is only about 4.5+% short. That's not
bad for a single digit.
And isn't usually a #define const?
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).

--
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 #5

see:- http://mathforum.org/library/drmath/view/54456.html
Wow it gives all the worst ways to calculate pi, and it has nothing specific
to C. Its win-win!
Jun 27 '08 #6
pete ha scritto:
static d
Why all these 'static'?
Jun 27 '08 #7
nembo kid wrote:
pete ha scritto:
>static d

Why all these 'static'?
That's to prevent the objects from having external linkage, which is
mainly to control namespace pollution. IOW these identifiers are
visible only in this translation unit, from where they are defined
until the end of the unit, so you can reuse these identifiers for other
purposes elsewhere. The default linkage for functions and file scope
objects is external. The static qualifier in these cases suppress this.

Jun 27 '08 #8
nembo kid wrote:
pete ha scritto:
>static d

Why all these 'static'?
There is no reason to calculate that one,
more than once.

--
pete
Jun 27 '08 #9
Richard Heathfield <rj*@see.sig.in validwrites:
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.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.)

--
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 #10

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

Similar topics

22
9936
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
5637
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
14871
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
6772
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
32085
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
4662
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
9685
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
10245
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10021
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
9063
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
7559
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
6802
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5458
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...
1
4131
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
3
2933
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.