Programming for mathematical calculations


By Zhidong Leong (P3545) on Wednesday, January 17, 2001 - 12:27 pm :

May I know the formula for pi?


By James Myatt (Jem50) on Wednesday, January 17, 2001 - 06:32 pm :
pi (p) is the ratio of the diameter of any circle to its diameter.

However if you want to know the value of pi, then that's a different matter. The value of pi can be found by taking sufficient terms of an infinite series. It is possible to obtain a Taylor series expansion of many functions such as tan x, sin x, arctan x, etc.

arctan x = x - x3 /3 + x5 /5 - x7 /7 ¼for -1 < x £ 1

It just so happens that tan p/4 = 1, hence p/4 = arctan 1 = 1 - 1/3 + 1/5 - 1/7 + ¼

By taking a large number of terms then this will converge on p/4 hence you can calculate the value to as much accuracy as you need. However this particular expansion converges very slowly.

Having just done 10000 terms (on my computer), you can be sure of 1 dp after 25 terms, 2dp after 1255, 3dp after 2464, and after 10000 you still don't have 4 dp of accuracy. So as you can see this is a particular series is very inefficient, as the difference between successive terms gets very small very quickly, that is 1/n - 1/(n+1).

There are many much faster algorithms, but I can't remember them, but are often work along similar lines.

If you want to know the value of pi, it's best looked up, and there are a variety of online resources, which can probably be located by sticking "pi" and "value" into any good search engine.

Of course, what is pi? I would say pi is the ratio of the diameter of any circle to its diameter, someone else might say the sum of an infinite series, but an engineer or scientist might say pi is 3.14, or a school child might say it's a number that helps them calculate the area of a circle.

James


By Hal 2001 (P3046) on Wednesday, January 17, 2001 - 06:52 pm :

James, what program language did you write it in?
If so may I have look at your source?

Hal


By James Myatt (Jem50) on Thursday, January 18, 2001 - 01:25 am :

erm ... I used an Excel spreadsheet. It's not necessary to use anything else, but it did fall over when I tried to expand it to 30000 terms.

However, [he says grabbing an envelope]

begin{displaymath}pi = 4 sum\_{a=0}^infty Biggl(frac{1}{4a+1} - frac{1}{4a... ...sum\_{a=0}^infty Biggl(frac{4}{4a+1} - frac{4}{4a+3}Biggr) end{displaymath}


so a simple FOR loop in some appropriate language would do it.

I'll try hacking some VB or VC++ code together once I've had some sleep (and learnt enough VB/VC++) and post it here. However, if anyone does get some code together before I do then please let me know/have it.

James
By James Lingard (Jchl2) on Thursday, January 18, 2001 - 08:33 am :
How about (in C):

#include

void main()

{

double sum = 0.0;

int i;

for(i = 0; i< 10000; i++)

sum += (4.0/(4.0*a+1.0)-4.0/(4.0*a+3.0));

printf(''Pi = %fn'', sum);

}

James.


By Hal 2001 (P3046) on Thursday, January 18, 2001 - 09:14 am :

Thanks James M & L,
What's the maximum of floating points it can hold. Anyone tried anything Perl?

By the way, what programming language is the best to (or designed from ground up) to handle mathematical calculations/programming? Preferably open source language.


By James Myatt (Jem50) on Friday, January 19, 2001 - 09:57 am :

Thanks James, I thought that the code would look something like that. I was also looking for a more all singing all dancing pi calculating program, but that's great.

Hal,
Perl is a really cool programming language, or so I've been told. Your program would say "I want you to go through this list and take out all thw ones which are something like this ... yep that's perfect" and I believe it's quite fast. Perl is usually used on *nix systems. I don't know any Perl whatsoever, but I suspect there will be someone around who does.

If you're looking for pure speed then you need a compiled language like C/C++ as opposed to an interpreted one like BASIC, which is slower even after it's compiled. I like C/C++, but I only know the basics, and I grew up with BASIC, admittedly I haven't done any/much for a long time, but it's what I know. BASIC is of course a beginners language and interpreted, which makes it easy to use, but slow.

If you're looking for a really quick and easy way of making lots of calculations, then use an Excel spreadsheet as I did, which is more than sufficient.

There are also more mathematical packages such as Maple and stuff which will solve stuff for you, which may require you to learn something, but can be very useful.

I'll come back to open source after my lectures.

James


By Hal 2001 (P3046) on Friday, January 19, 2001 - 02:35 pm :

Yep, I am into Open Source stuff. I used to program in ColdFusion, but gave it up to learn languages that are Open Source and non-proprietary.

Perl is indeed a good language, you can do so many things in so many different ways. If you don't use Linux/Unix you can still do Perl, go to ActivePerl.com or .org I thing, there is a Win32 version if it, but you also neee Pws (personal web server) i think. I use it in Linux mainly to experient, but still consider my self to me a beginner. I guess I would need couple o years of hacking away at Perl to call me self a proper Perl hacker.

I too learned BASIC, but it was a long time ago.... on the old BBC MICRO's, anyone remember them???? That got me hooked onto computing and maths I guess.

Is Nrich run on apache/linux of something from MS?
[Apache/linux. - The Editor]

Have you tried and assembly languages? I learned a bit of emulation in Z80 before , but only a little though. I might return to that once I feel comfortable and mastered linux and its zillions of FREE apps.


By James Myatt (Jem50) on Friday, January 19, 2001 - 06:14 pm :

That's pretty much what I was going to say.

Open Source:
Linux comes with hundreds of open source apps including C/C++ compilers, Perl stuff, Pascal stuff, anything you could care to name, and if it's not on the distribution CDs then there's some ftp server where you can find it.

Another advantage of Perl is that you can interface it directly with gnuplot and stuff like that, or so I've heard, for a graphical output ActivePerl is, as Hal says, available free and for Win32. I have a copy, but I'm too lazy and I've too much important stuff to do to play with it, and Linux, which I haven't installed yet either. Perl is very useful to learn, because it's so easy to hack code together once you've learnt the basics, as long as you've the O'Reilly book to hand.

It doesn't need to be open source of course; freeware is just as good and much more widely availible. There are a selection first-class resources including TUCOWS , or UK Mirror Service .
However, you can get a students license for Microsoft Visual Studio if you prefer that, which will not cost you anywhere near as much as a normal one.

Programming Languages:
I learnt GWBASIC, which progressed to QBASIC, then QuickBASIC, and so now I can do some VB. I never liked BBCs, but we had a PC at home from about 1988/9 or so and so I started using BASIC. Since I've done tiny bits of Assembly Language, back in the days when Pentiums couldn't divide, and a little C/C++, which I'm trying to resurrect, and wean myself off BASIC, so I can do stuff in Linux too.

Assembly Language is about as low level as you can get. This makes it completely unsuitable for writing code to do mathematical stuff. And hence, noone uses it anymore, unless they're programming PICs or other similar processors. I think that the priority for anyone wanting to learn computer languages is to learn C/C++, because the code is so much more efficient and better structured and prettier. So that's my advice for anyone intending to start. It's also difficult learning a new language when you know one which will suffice.

CATAM (Computer Aided Teaching of All Mathematics) used to be done in Pascal, but it's now in C++. This shows just how powerful it is.

Servers:
I'd be surprised if Nrich isn't on a UNIX server, as it's part of the Maths faculty here at Cambridge, and that's the kind of thing that happens here. There are also a selection of free web servers for Windows and Linux etc availible also from TUCOWS , or UK Mirror Service .

James


By Dan Goodman (Dfmg2) on Friday, January 19, 2001 - 06:58 pm :

Hal, the precision of James' program is roughly 17 significant figures at best, because it uses the 64 bit "double" type in C++. The 32 bit "float" type given about 7 or 8 sig figs. You can download free libraries on the internet to do arbitary precision maths (APM), or if you're using Java you can use the "BigDecimal" package. In fact, you might be able to do this using just JavaScript and your browser, anyone know if you can?


By Hal 2001 (P3046) on Saturday, January 20, 2001 - 09:35 am :

Thanks James, Dan.


By Dan Goodman (Dfmg2) on Sunday, January 21, 2001 - 10:08 pm :

Here is a link to an arbitrary precision C library: http://spanky.triumf.ca/www/fractint/arb-prec.html


By Hal 2001 (P3046) on Monday, January 22, 2001 - 04:45 pm :

Thanks Dan. I'll keep that under my bookmarks, and will use after I learn a bit more C.