# PureBasic Forum

 It is currently Mon Jun 01, 2020 5:04 pm

 All times are UTC + 1 hour

 Page 1 of 2 [ 23 posts ] Go to page 1, 2  Next
 Print view Previous topic | Next topic
Author Message
 Post subject: Calculating and printing the many digits of PIPosted: Sun Nov 14, 2010 3:53 am

Joined: Sat Jul 11, 2009 4:57 am
Posts: 936
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. 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?

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

Top

 Post subject: Re: Calculating and printing the many digits of PIPosted: Sun Nov 14, 2010 11:15 am
 Always Here

Joined: Mon Sep 22, 2003 6:45 pm
Posts: 7446
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

 Post subject: Re: Calculating and printing the many digits of PIPosted: Sun Nov 14, 2010 11:57 am

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

Top

 Post subject: Re: Calculating and printing the many digits of PIPosted: Mon Nov 15, 2010 11:00 am

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

 Post subject: Re: Calculating and printing the many digits of PIPosted: Mon Nov 15, 2010 12:00 pm
 Always Here

Joined: Mon Sep 22, 2003 6:45 pm
Posts: 7446
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

 Post subject: Re: Calculating and printing the many digits of PIPosted: Mon Nov 15, 2010 12:16 pm

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

 Post subject: Re: Calculating and printing the many digits of PIPosted: Mon Nov 15, 2010 12:29 pm

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

 Post subject: Re: Calculating and printing the many digits of PIPosted: Mon Nov 15, 2010 12:57 pm
 Always Here

Joined: Mon Sep 22, 2003 6:45 pm
Posts: 7446
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

 Post subject: Re: Calculating and printing the many digits of PIPosted: Mon Nov 15, 2010 2:27 pm

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

Top

 Post subject: Re: Calculating and printing the many digits of PIPosted: Mon Nov 15, 2010 2:57 pm

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

 Post subject: Re: Calculating and printing the many digits of PIPosted: Mon Nov 15, 2010 6:18 pm
 PureBasic Expert

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

 Post subject: Re: Calculating and printing the many digits of PIPosted: Mon Nov 15, 2010 6:26 pm

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 3690
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.

_________________

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

Top

 Post subject: Re: Calculating and printing the many digits of PIPosted: Mon Nov 15, 2010 8:11 pm

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

 Post subject: Re: Calculating and printing the many digits of PIPosted: Mon Nov 15, 2010 8:32 pm

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

Top

 Post subject: Re: Calculating and printing the many digits of PIPosted: Mon Nov 15, 2010 8:56 pm
 PureBasic Expert

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

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 2 [ 23 posts ] Go to page 1, 2  Next

 All times are UTC + 1 hour

#### Who is online

Users browsing this forum: IdeasVacuum and 26 guests

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

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - IDE    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite