Calculating and printing the many digits of PI

Just starting out? Need help? Post your questions and find answers here.
Nituvious
Addict
Addict
Posts: 999
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Calculating and printing the many digits of PI

Post by Nituvious »

How does one get passed the 10th digit in PureBasic? I've always wondered how the bigwigs make those programs that calculate the 2390 billionth digit of PI and thought it would be a lot of fun to make(or try to make) a program that counts them. However I don't have any idea what science is used to create such a program. :mrgreen: I looked at wikipedia but it had funny symbols and big words on it. :oops:
Has anyone on the forum written a program that does this yet?
▓▓▓▓▓▒▒▒▒▒░░░░░
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Calculating and printing the many digits of PI

Post by Trond »

You would need a datatype that supports arbitrary precision to do that. To do it in C you would use gmp, for example. See: http://gmplib.org/
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Calculating and printing the many digits of PI

Post by Michael Vogel »

A great tool for doing integer (!) calculations is Aribas. It has a pascal/basic like language and the tiny package includes also some examples to calculate pi - so no math symbols, only computer language :wink:

==> pi_chud(100).
-: 3_14159_26535_89793_23846_26433_83279_50288_41971_69399_37510_58209_74944_
59230_78164_06286_20899_86280_34825_34211_70679
Derek
Addict
Addict
Posts: 2356
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Re: Calculating and printing the many digits of PI

Post by Derek »

There is a program written in QuickBasic about a third of the way down the page on this link

http://www.boo.net/~jasonp/pipage.html

It uses simple variable types and would be easy to translate to PureBasic. If you do convert it then bear in mind that QBasic does a strange thing when rounding numbers, if a number is x.5 then it rounds to the nearest even number, it doesn't round up, strange. So 3.5 and 4.5 are both rounded to 4 for example.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Calculating and printing the many digits of PI

Post by Trond »

It uses simple variable types and would be easy to translate to PureBasic. If you do convert it then bear in mind that QBasic does a strange thing when rounding numbers, if a number is x.5 then it rounds to the nearest even number, it doesn't round up, strange. So 3.5 and 4.5 are both rounded to 4 for example.
PB does that, too. It's standard on the x86 fpu. It's called banker's rounding, and it's done this way to minimize rounding bias over time (if always rounding up, it would always go in favour of either the bank or the accountee).

Code: Select all

b.f = 3.5
c.f = 4.5

a.i = b
Debug a
a.i = c
Debug a
Derek
Addict
Addict
Posts: 2356
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Re: Calculating and printing the many digits of PI

Post by Derek »

In the program to calculate PI there is a defint a-z at the start so there is no conversion from float to integer so if you run the following example I think it would be different on Qbasic to Pure, I haven't tried it on Qbasic though so don't quote me. :wink:

Code: Select all

a=4.5
b=3.5
Debug "rounded up "+Str(a)+", I think Qbasic would round down!"
Debug "rounded up "+Str(b)

a=9/2
b=7/2
Debug "rounded down "+Str(a)
Debug "rounded down "+Str(b)+", I think Qbasic would round up!"
Without floats, Pure rounds the .5's up regardless of where the nearest even number is.

Edit. Ran it under Qbasic and got four 4's as the answer whereas Pure gives us 5,4,4,3 so there is a difference between the two versions and should be accounted for if converting the program.

@Trond, thanks for sharing that. I assumed rounding was always up when you got to .5 or above.
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Calculating and printing the many digits of PI

Post by Michael Vogel »

Trond wrote:PB does that, too. It's standard on the x86 fpu. It's called banker's rounding, and it's done this way to minimize rounding bias over time (if always rounding up, it would always go in favour of either the bank or the accountee).

Code: Select all

b.f = 3.5
:
Crazy things, "kaufmännisches Runden" (banker's rounding) means here in middle europe the same like rounding in standard math: if the cipher which will be removed on rounding is "5", the (absolute) value will be increased.

To make it a little bit complicate, guess what will be displayed here:

Code: Select all

b = 3.5
c = 4.5

Debug b
Debug c
Did not knew, that the fpu is using non-math rounding, what a shame :? -- I have to use explicit functions to do the right thing:

Code: Select all

bf.f=3.5
cf.f=4.5
a=Round(bf,#PB_Round_Nearest)
Debug a
a=Round(cf,#PB_Round_Nearest)
Debug a

a=Int(bf+0.5*Sign(bf))
Debug a
a=Int(cf+0.5*Sign(cf))
Debug a
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Calculating and printing the many digits of PI

Post by Trond »

Edit. Ran it under Qbasic and got four 4's as the answer whereas Pure gives us 5,4,4,3 so there is a difference between the two versions and should be accounted for if converting the program.
Because of a bug in PB with floating point constants. This was reported and fixed in the 32-bit windows version, but I still have it in the 64-bit linux version. It's supposed to give 4, 4, 4, 3.
9/2 and 7/2 are not floating point numbers, you are doing integer division, so the correct result of 7/2 is 3.
Crazy things, "kaufmännisches Runden" (banker's rounding)
I don't think kaufmännisches can be translated to banker?
To make it a little bit complicate, guess what will be displayed here:
It's a bug, it should be the same as when assigning from a float.
User avatar
Michael Vogel
Addict
Addict
Posts: 2666
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Calculating and printing the many digits of PI

Post by Michael Vogel »

Trond wrote:Because of a bug in PB with floating point constants. This was reported and fixed in the 32-bit windows version, but I still have it in the 64-bit linux version. It's supposed to give 4, 4, 4, 3.
9/2 and 7/2 are not floating point numbers, you are doing integer division, so the correct result of 7/2 is 3.
Hm, saw your thread - PB4.51 uses #constant=Round(variable,#PB_Round_Nearest) in PB4.51Win32 and because this is math I can live with it. 4.5 is 4.5 when being assigned to a constant and not any other expression, like 9/2 or 0.5+0.5+0.5+0.5+0.5+0.5+0.5+0.5+0.5...
Trond wrote:
Crazy things, "kaufmännisches Runden" (banker's rounding)
I don't think kaufmännisches can be translated to banker?
You're right, it's not the same word, but means trading in general - and the method of rounding is also used by each bank in germany, austria, italy etc. (not sure about switzerland, they should know, what's "correct" :wink: )
Derek
Addict
Addict
Posts: 2356
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Re: Calculating and printing the many digits of PI

Post by Derek »

All manor of weirdness!!

Code: Select all

a=11/2
Debug a
a=5.5
Debug a
aa.f=11
b=2
Debug aa/b
Debug Int(aa/b)
c=aa/b
Debug c
d=aa
Debug d/b
Debug 11/2
I guess it's just something to bear in mind when working with floats or integers.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6161
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Calculating and printing the many digits of PI

Post by blueznl »

Derek wrote: All manor of weirdness!!
I guess it's just something to bear in mind when working with floats or integers.
Is that so?

http://www.xs4all.nl/~bluez/purebasic/p ... evaluation

... or have the rules yet changed again?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Calculating and printing the many digits of PI

Post by Demivec »

@blueznl: Your Survival Guide entry on banker's rounding is incorrect. The numbers should be rounded to the nearest even number under the conditions specified, not rounded to the nearest number. See Trond's first post above.


@Edit: corrected discription by adding 'should be' so that an unintended ambiguity of meaning was removed.
Last edited by Demivec on Mon Nov 15, 2010 9:06 pm, edited 1 time in total.
Derek
Addict
Addict
Posts: 2356
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Re: Calculating and printing the many digits of PI

Post by Derek »

Like Trond said, when using floating point, the fpu rounds to the nearest even number, not always up!

Or at least it does when using variables.

Code: Select all

a.f = 3
b.f = 1.5

c.l = 3.0 + 1.5
d.l = a + b

Debug c
Debug d
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Calculating and printing the many digits of PI

Post by #NULL »

that's scary :shock:
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6161
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Calculating and printing the many digits of PI

Post by blueznl »

Derek, to me that looks like a bug, to be honest.
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Post Reply