![]() Printf("sizeof(long double) %ld\n", sizeof(long double)) The following function (which you can skip if you find it too boring!) just prints out a few pieces of information from float.h on the long double type. The rest is very straightforward - just some function prototypes and calls to those functions in main, all of which are commented out at the moment so we can implement and run them one at a time. (The 19th digit is 4 therefore the 18th doesn't need to be rounded up.) There are also a few #defines for printing in colour which I'll use later. Puts("-\n") //long_double_info() //fractions() //francois_viete() //john_wallis() //john_machin() //gregory_leibniz() //nilakantha() Īfter the #includes I have #defined π as a string to 18dp, which will be used later to check results digit by digit. Open estimatingpi.c and type or paste this code. You can download the code as a zip or clone/download from Github if you prefer. That's what Charles Babbage did - this punch card is his equivalent of #define M_PI.įor the full story go here Starting to CodeĬreate a new folder and within it create a single file called estimatingpi.c, which will contain all the code for this project. Needless to say, if you actually need to use π don't mess around calculating it, just make sure it is #defined somewhere. If you don't take a look here to fix the problem. There is a common but non-standard #define called M_PI which you might have in your math.h file. When we get to that you'll see the maximum guaranteed precision is 18dp so that is what I'll use as the gold standard, and we will see how close to that we can get using various algorithms within the limits of the data type used. (On a 32 bit machine 32 x 3 = 96 bits might be used.) I will start off the code with a simple function to output a few interesting facts about long double before getting stuck into the π-calculating stuff. Exactly how much precision that gives is dependent on the compiler, but GCC on x86 uses 80 bits, although if you run sizeof on the type it will give 16 (128 bits) to use a multiple of the data size. The long double TypeĪll the calculations in this project will be carried out using the long double type. If you want to do something rather more serious than playing with my code you can download the application called y-cruncher used to break the record here. In this project I will code a few of the simpler methods to give a decidedly non-rigorous introduction to what is actually a vast topic. Trillions of digits aren't therefore useful in their own right but the effort put in to their calculation no doubt benefits mathematics and computer science. You can calculate the circumference of the Universe to the accuracy of a hydrogen atom using just 40dp which is the number reached over three centuries ago! And you only need 62 digits to calculate the circumference to the Planck length, the smallest meaningful unit of length which you can think of as a quantum of space. ![]() There is plenty of information around regarding the number of digits of π which are actually necessary for practical purposes. These generally calculate all the digits up to a certain point but there are also a few so-called spigot algorithms which calculate a given digit without calculating all the preceding ones first. ![]() OK, that's a slight exaggeration but there are many ways of getting the job done, ranging from the ancient 22/7 to recently discovered fast but complex methods such as the Chudnovsky Algorithm. So far π has been calculated to 22,459,157,718,361 digits (nearly twenty two and a half trillion) which, by coincidence, is the number of different ways there are of calculating it. Therefore it cannot be calculated as such, just estimated to (in principle) any number of digits. Pi is an irrational number starting off 3.14159 and then carrying on for an infinite number of digits with no pattern which anybody has ever discovered. I started off calling this project "Calculating Pi" but soon realised that I needed to rename it "Estimating Pi". ![]()
0 Comments
Leave a Reply. |