Calculating and printing the many digits of PI
Calculating and printing the many digits of PI
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. I looked at wikipedia but it had funny symbols and big words on it.
Has anyone on the forum written a program that does this yet?
Has anyone on the forum written a program that does this yet?
▓▓▓▓▓▒▒▒▒▒░░░░░
Re: Calculating and printing the many digits of PI
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/
- Michael Vogel
- Addict
- Posts: 2680
- Joined: Thu Feb 09, 2006 11:27 pm
- Contact:
Re: Calculating and printing the many digits of PI
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
==> pi_chud(100).
-: 3_14159_26535_89793_23846_26433_83279_50288_41971_69399_37510_58209_74944_
59230_78164_06286_20899_86280_34825_34211_70679
==> pi_chud(100).
-: 3_14159_26535_89793_23846_26433_83279_50288_41971_69399_37510_58209_74944_
59230_78164_06286_20899_86280_34825_34211_70679
Re: Calculating and printing the many digits of PI
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.
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.
Re: Calculating and printing the many digits of PI
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).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.
Code: Select all
b.f = 3.5
c.f = 4.5
a.i = b
Debug a
a.i = c
Debug a
Re: Calculating and printing the many digits of PI
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.
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.
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!"
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.
- Michael Vogel
- Addict
- Posts: 2680
- Joined: Thu Feb 09, 2006 11:27 pm
- Contact:
Re: Calculating and printing the many digits of PI
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.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 :
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
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
Re: Calculating and printing the many digits of PI
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.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.
9/2 and 7/2 are not floating point numbers, you are doing integer division, so the correct result of 7/2 is 3.
I don't think kaufmännisches can be translated to banker?Crazy things, "kaufmännisches Runden" (banker's rounding)
It's a bug, it should be the same as when assigning from a float.To make it a little bit complicate, guess what will be displayed here:
- Michael Vogel
- Addict
- Posts: 2680
- Joined: Thu Feb 09, 2006 11:27 pm
- Contact:
Re: Calculating and printing the many digits of PI
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: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.
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" )Trond wrote:I don't think kaufmännisches can be translated to banker?Crazy things, "kaufmännisches Runden" (banker's rounding)
Re: Calculating and printing the many digits of PI
All manor of weirdness!!
I guess it's just something to bear in mind when working with floats or integers.
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
Re: Calculating and printing the many digits of PI
Is that so?Derek wrote: All manor of weirdness!!
I guess it's just something to bear in mind when working with floats or integers.
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... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
Re: Calculating and printing the many digits of PI
@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.
@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.
Re: Calculating and printing the many digits of PI
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.
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
Re: Calculating and printing the many digits of PI
that's scary
Re: Calculating and printing the many digits of PI
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... )
( The path to enlightenment and the PureBasic Survival Guide right here... )