It is currently Tue Nov 19, 2019 7:01 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Calculating and printing the many digits of PI
PostPosted: Sun Nov 14, 2010 3:53 am 
Offline
Addict
Addict

Joined: Sat Jul 11, 2009 4:57 am
Posts: 933
Location: United States
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?

_________________
▓▓▓▓▓▒▒▒▒▒░░░░░


Top
 Profile  
Reply with quote  
 Post subject: Re: Calculating and printing the many digits of PI
PostPosted: Sun Nov 14, 2010 11:15 am 
Offline
Always Here
Always Here

Joined: Mon Sep 22, 2003 6:45 pm
Posts: 7439
Location: Norway
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/


Top
 Profile  
Reply with quote  
 Post subject: Re: Calculating and printing the many digits of PI
PostPosted: Sun Nov 14, 2010 11:57 am 
Offline
Addict
Addict
User avatar

Joined: Thu Feb 09, 2006 11:27 pm
Posts: 2445
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Calculating and printing the many digits of PI
PostPosted: Mon Nov 15, 2010 11:00 am 
Offline
Addict
Addict

Joined: Wed Apr 07, 2004 12:51 am
Posts: 2357
Location: England
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Calculating and printing the many digits of PI
PostPosted: Mon Nov 15, 2010 12:00 pm 
Offline
Always Here
Always Here

Joined: Mon Sep 22, 2003 6:45 pm
Posts: 7439
Location: Norway
Quote:
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:
b.f = 3.5
c.f = 4.5

a.i = b
Debug a
a.i = c
Debug a


Top
 Profile  
Reply with quote  
 Post subject: Re: Calculating and printing the many digits of PI
PostPosted: Mon Nov 15, 2010 12:16 pm 
Offline
Addict
Addict

Joined: Wed Apr 07, 2004 12:51 am
Posts: 2357
Location: England
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:
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Calculating and printing the many digits of PI
PostPosted: Mon Nov 15, 2010 12:29 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Feb 09, 2006 11:27 pm
Posts: 2445
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:
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:
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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Calculating and printing the many digits of PI
PostPosted: Mon Nov 15, 2010 12:57 pm 
Offline
Always Here
Always Here

Joined: Mon Sep 22, 2003 6:45 pm
Posts: 7439
Location: Norway
Quote:
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.

Quote:
Crazy things, "kaufmännisches Runden" (banker's rounding)
I don't think kaufmännisches can be translated to banker?

Quote:
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Calculating and printing the many digits of PI
PostPosted: Mon Nov 15, 2010 2:27 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Feb 09, 2006 11:27 pm
Posts: 2445
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:
Quote:
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: )


Top
 Profile  
Reply with quote  
 Post subject: Re: Calculating and printing the many digits of PI
PostPosted: Mon Nov 15, 2010 2:57 pm 
Offline
Addict
Addict

Joined: Wed Apr 07, 2004 12:51 am
Posts: 2357
Location: England
All manor of weirdness!!
Code:
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Calculating and printing the many digits of PI
PostPosted: Mon Nov 15, 2010 6:18 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Sat May 17, 2003 11:31 am
Posts: 6073
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?

_________________
( PB5.xx Win10 x64 Asrock AB350 Pro4 Ryzen 1600X 32GB RAM Evo 840 GTX1060 )
( The path to enlightenment and the PureBasic Survival Guide right here... )


Top
 Profile  
Reply with quote  
 Post subject: Re: Calculating and printing the many digits of PI
PostPosted: Mon Nov 15, 2010 6:26 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 3591
Location: Utah, USA
@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.

_________________
Image


Last edited by Demivec on Mon Nov 15, 2010 9:06 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Calculating and printing the many digits of PI
PostPosted: Mon Nov 15, 2010 8:11 pm 
Offline
Addict
Addict

Joined: Wed Apr 07, 2004 12:51 am
Posts: 2357
Location: England
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:
a.f = 3
b.f = 1.5

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

Debug c
Debug d


Top
 Profile  
Reply with quote  
 Post subject: Re: Calculating and printing the many digits of PI
PostPosted: Mon Nov 15, 2010 8:32 pm 
Offline
Addict
Addict

Joined: Thu Aug 30, 2007 11:54 pm
Posts: 1132
Location: right here
that's scary :shock:


Top
 Profile  
Reply with quote  
 Post subject: Re: Calculating and printing the many digits of PI
PostPosted: Mon Nov 15, 2010 8:56 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Sat May 17, 2003 11:31 am
Posts: 6073
Derek, to me that looks like a bug, to be honest.

_________________
( PB5.xx Win10 x64 Asrock AB350 Pro4 Ryzen 1600X 32GB RAM Evo 840 GTX1060 )
( The path to enlightenment and the PureBasic Survival Guide right here... )


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 23 posts ]  Go to page 1, 2  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 15 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye