Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] Calculate Pi in one line of code

Author
Message
AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 29th Jan 2009 00:39 Edited at: 29th Jan 2009 12:27
I know this is totally useless as DBP already has a Pi() function, but I've made a little function which will calculate pi to 6 places:



The only parameter is Acc, which is the accuracy. An accuracy of at least 4 or 5 thousand is required to aproximate Pi. Unfortunately the inaccuracy and digit-limit of floats means that this function can only calculate about 6 significant digits, regardless of the accuracy. Theoretically though it could probably calculate it to at least a couple dozen more places.

Anyways if anyone is curious as to what it does, basically it finds the apothem of a polygon with 'Acc'-number of sides (the higher Acc is, the closer it is to a circle), then divides the perimeter by it.

CuCuMBeR
21
Years of Service
User Offline
Joined: 11th Jan 2003
Location: Turkey
Posted: 29th Jan 2009 08:27
why dont you just use 3.14 anyways?

There is always one more imbecile than you counted on.
HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 29th Jan 2009 11:48
Quote: "DBP already has a Pi() function"


I've never found a Pi function in DBP, how do you get it, find it?

Dark Physics makes any hot drink go cold.
AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 29th Jan 2009 12:24 Edited at: 29th Jan 2009 12:29
Quote: "why dont you just use 3.14 anyways?"


Well I was more just testing out Pi formulas, I know that this function is pretty useless but it works.

Quote: "I've never found a Pi function in DBP, how do you get it, find it?"


By typing in 'pi()'.

HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 29th Jan 2009 12:45
pi() ?
you must have a plugin that has it, as it no go on my version.

Dark Physics makes any hot drink go cold.
Kohaku
20
Years of Service
User Offline
Joined: 3rd May 2004
Location: The not very United Kingdom
Posted: 29th Jan 2009 19:39
Pi() is a function from IanM's Matrix1 plugin 7.



AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 30th Jan 2009 00:23
HowDo:

Kohaku's right, sorry, I should've checked first.

Monk
16
Years of Service
User Offline
Joined: 25th Sep 2008
Location: Standing in the snow =D
Posted: 31st Jan 2009 23:28 Edited at: 31st Jan 2009 23:29
My turtle Pancho will, my love, pick up my new mover ginger...
3. 1 415 9 26 5 3 5 8 9 7 9 3 2 3 3 8 4 6 26 4

who needs a pi function???

Ps i didnt make up the mnemonic, i just remember it...

Siddy
15
Years of Service
User Offline
Joined: 16th Dec 2008
Location:
Posted: 7th Feb 2009 10:32
If you can make the decimal place accuracy based on input by user, it would be very useful as a cpu burn in program.

Calculating large numbers stresses cpus and if you overclock too much (or incorrectly) a program such as this (with more decimal places, like thousands or millions) will cause the cpu to make errors, leading to tell tale signs of unstable cpu.

However, in your program, after the 16th decimal place you just get zero's
Xlaydos
20
Years of Service
User Offline
Joined: 26th Mar 2004
Location:
Posted: 7th Feb 2009 15:01
Monk you got that slighty wrong! There should only be one 3 before the 8.

3.14159265358979323846264338327950288419716939937510...

*feels geeky*
Monk
16
Years of Service
User Offline
Joined: 25th Sep 2008
Location: Standing in the snow =D
Posted: 7th Feb 2009 19:56
Lol how many places is that??
If you is wanting a geeky contest...
3.14159265358979323846264338327950288419716939937510582097494459230781640628620

Sorry couldnt resist... (didnt copy, honest...)

flashing snall
19
Years of Service
User Offline
Joined: 8th Oct 2005
Location: Boston
Posted: 9th Feb 2009 01:45
hmm... The kid who sat next to me on the bus in 5th Grade had it up to 30 I think.

PGDO. it consumes my every waking moment, that is not already being eaten by work / school / sleeping
AndrewT
17
Years of Service
User Offline
Joined: 11th Feb 2007
Location: MI, USA
Posted: 9th Feb 2009 01:58
I had it up to 80 back in 6th grade. As well as a little book I printed out of the first 100,000 digits in 4 pt font.

Kohaku
20
Years of Service
User Offline
Joined: 3rd May 2004
Location: The not very United Kingdom
Posted: 9th Feb 2009 11:02
PI IS EXACTLY 3!





HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 9th Feb 2009 11:11


Dark Physics makes any hot drink go cold.
fik
20
Years of Service
User Offline
Joined: 28th Sep 2004
Location:
Posted: 9th Feb 2009 11:16
I think this is the best method to calulate pi.
http://www.ehow.com/how_2164461_calculate-pi-throwing-frozen-food.html
cguy
17
Years of Service
User Offline
Joined: 3rd Nov 2007
Location: In my Computer!
Posted: 4th Mar 2009 05:53
Technically you all are spelling pie wrong, without the e. And the value of pie is dependent on how much you eat.
Niels Henriksen
20
Years of Service
User Offline
Joined: 27th Sep 2004
Location: Behind you breathing heavely
Posted: 11th Mar 2009 23:57
The best way is 22/7

Niels Henriksen
www.tales-of-the-realms.com
if Microsoft can sell software with bugs, so can I.
cguy
17
Years of Service
User Offline
Joined: 3rd Nov 2007
Location: In my Computer!
Posted: 13th Mar 2009 00:31
@Henriksen
22/7 is not pi: 22/7 = 3.142857143...
pi = 3.141592654...(I didn't feel the need to go 78 places)
They are close and that could work depending on what the purpose is, but if you use 22/7 you might as well just use 3.14 because that is the closest these two numbers are.

Do we all have to put stupid jokes here?
calcyman
17
Years of Service
User Offline
Joined: 31st Aug 2007
Location: The Uncertainty Principle
Posted: 13th Mar 2009 09:21
355/113 is sufficiently accurate for most applications, especially since it is correct to 6 decimal places.

However, in your program it is best to use 3.141592654, so that no calculations are needed. Similarly, I always type 1.414... instead of sqrt(2), since sqrt() is so time-consuming. Sorry for lecturing you about computational efficiency:

a# = FindPi(1000000.0)*r#*r# : `area of a circle

Can be improved to:

a# = 3.141592654*r#*r#

Instead of dividing a number by a constant, multiply it by its inverse. This makes calculations even faster:

a# = b#*h#/2.0 : `area of a triangle

Can be improved to:

a# = b#*h#*0.5

If you have an expression with a recurring sub-expression, calculate the value of the sub-expression first:

y# = (EXP(x#)-EXP(-x#))/(EXP(x#)+EXP(-x#)) : `hyperbolic tangent

can be improved to:

pexp# = EXP(x#)
nexp# = 1.0/pexp#
y# = (pexp#-nexp#)/(pexp#+nexp#)

All of these will make your code run considerably faster, so you might be able to squeeze a higher FPS rate.

The optimist's right, The pessimist's right.
feiting shadow
18
Years of Service
User Offline
Joined: 12th Sep 2006
Location:
Posted: 19th Mar 2009 12:08 Edited at: 19th Mar 2009 12:33
So, anyone find the document about the kid who figured out the pattern to Pi? I bought a Scientific American while working at retail (and go figure, a coworker stole the only intelligent thing in the breakroom) and this genius math whiz, younger than most of us, even me, learned it's not one pattern, but a series of patterns, and could predict Pi.

This will probably sound bogus, but he did similar to language, and can receive half of a document and mathematically predict the next half of what it will say. The kid got a noble prize.

The reason I'm posting this here is, anyone find the pattern document anywhere? We could make a sweet Pi() function if we knew what he does.

EDIT: I can't find the article, but I remember this is the magazine I bought: http://www.sciamdigital.com/index.cfm?fa=Products.ViewIssue&ISSUEID_CHAR=49C423D8-2B35-221B-67C73E742E0385A0 as I read pseudogenes, the space flight one, and the expert mind article (which are all free now on SA's main site). Gah, I remember everything the kid did but can't find a trace of it posted now.

Signed
------
n008
17
Years of Service
User Offline
Joined: 18th Apr 2007
Location: Chernarus
Posted: 25th Mar 2009 22:17 Edited at: 25th Mar 2009 22:20
Not sure if this has been posted, but...

Pi = 4 (∞ )S(n=0) (-1)^n/(2n+1)

About 3 billion summations will give you Pi to ~ 17 places.

4 or 5 million for 7 places.



Not very efficient in DarkBASIC at all, though.

On the subject of Pi though it's never made any sense to me how an irrational number can be a ratio. ???

I looked up the proof but I haven't taken calculus yet so it didn't mean anything to me

"I have faith, that I shall win the race, even though I have no legs, and am tied to a tree." ~Mark75
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 16th Apr 2009 14:55
Quote: "So, anyone find the document about the kid who figured out the pattern to Pi?"


Intriguing - but are you sure it wasn't an April Fool's joke? There is no pattern to Pi - it's a proven irrational number which means there is no recurrence, and a pattern by definition implies recurrence.

That's not to say it's not predictable - it is obviously predictable because it can be calculated.

feiting shadow
18
Years of Service
User Offline
Joined: 12th Sep 2006
Location:
Posted: 24th Apr 2009 09:45
Haven't checked this in awhile, but yeah, probably was. Recently posted in geek culture about how much I enjoy Media's bogus info... either way, I do know that if you find the absolute value of the difference between about 20 values of Pi's decimal string, ignoring signs, you will end up with 1-1-1-1-1-1-1-1-1-1 as the change, which basically indicates complete randomness.

Though, the fact I screwed up with the signs when I did that kinda invalidates my testing...

Basically I did the geometry thing where you shove arrows between each number and write down the change, then did it again, and again, and again... found nothing...

Signed
------

Login to post a reply

Server time is: 2024-11-22 01:13:11
Your offset time is: 2024-11-22 01:13:11