Pi Calculator...How can it be done...

Permabanned
Joined
28 Feb 2006
Posts
1,604
Hi,

Im making a pi calculator for my 1st ever "hands on vb.net" practice.

Unltimately I want a Box with a start button , when I click the start button it will print on screen every 1 second in a text box pi..
So, 3.142 etc... (It will keep on bringing the next number each second)

I believe the text box way is the easiest...am I correct?

Also, when it clicks start I will need a timer for each second and a loop, how can this be done?

Thanks, Im aiming for it to be done by Sunday :)

Thanks in advance
 
it sound's to me like your plan will work..

timer... create an instance of the timer class and set it's interval to the one you want. iirc it fires a TimerClick event or something like that you want to create a method that is regiastered to be the event handler for that event

timer class documentation is here: http://msdn.microsoft.com/library/d...ref/html/frlrfSystemTimersTimerClassTopic.asp

a text box will be fine and should work.. a label might be appropriate as well.

make sure you don't get too caught in threading nightmares calculating PI, just get the value for it and then round it to the correct number of digits each time.

rememeber that although Pi to 3 digits is 3.142 the actual value is 3.141875 or something like that so you'll need to round it correctly each time :D

it'll be much easier to sort it with a timer and an event. the other option is just to use a for/while loop and put a delay in it but this will cause your UI components to stop being displayed and generally isn't a good thing to do.

While your program is pausing for 0.989654 of a second nothing else can do anything.

HT
 
**pre-claimer - I'm a c# man, my code snippets may be in c#, and not vb.**

Ok, there's two bits, the UI stuff, and the actual figuring out the next decimal point of pi. I'm assuming you've got a method/code/some way of figuring out the next pi digit, so I'll look at the UI.

Textbox or label is fine for this. A textbox usually implies that it's requesting user input, which you're not, so a label might be better. So you just drag a label, button andalso a timer object onto your form. Click on the timer and look at it's properties. You can set the interval that it "ticks" (note, it's in milliseconds). Set the timer to disabled.

Then, very simply, you just double click on the button. This will give the code that's associated with the "event" of pressing the button - ie, what the hell happens when the button is pressed. So you can do anything here from just flash up a messagebox (MessageBox.Show("hi there");) What you probably want to do is get that to start a timer. So that'll just be a case of doing:
Code:
timer1.Enabled=true;
The timer will then fire off a "tick" event every time it ticks - you set the interval in the properties. Switch back to the form view, click on the timer, and click on the "events" button (lighting icon) on the properties window. The only entry in this window, there's only the tick event. Double click ont he tick word to create the tick event handler. This will take you to some code, where you can put what you want to happen when the timer ticks. Assuming you've got the pi calculating code in a method called "GiveMeTheNthDigitOfPi(int n)", you'ld just go:
Code:
label1.Text+=GiveMeTheNthDigitOfPi(n);
n+=1;
You should probably declare n to be 0 at some point further up the code file.

And that's it, you're done. :)
 
Thanks for that Growse and happytechie very much appreciated..


Growse you know on yours, how can I FIND how to calculate pi in a conveniant way for vb.net?

Then I can just stick the code in and make it look pretty lol


Edit :

Just got the following done :

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub StartBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartBtn.Click
'You are about to calculate Pi
MessageBox.Show(" Starting Calculation")
Sectime.Enabled = True
End Sub

Private Sub Ex_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ex.Click
'Thanks the user
MessageBox.Show("Thanks for using the Pi Calculator")
'Exits Program
Me.Close()

End Sub


Private Sub Sectime_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Sectime.Tick
Label1.Text += GiveMeTheNthDigitOfPi(n)
n += 1
End Sub
End Class
 
Last edited:
There are quite a few different methods of calculating PI. To save typing them all up ill just give you a link to this, http://www.cygnus-software.com/misc/pidigits.htm

There is even some example code on there incase you get stuck. How you decide to do it depends on how quick/accurate you need it to be.
 
Pi formulae are usually series (ie, you just keep calculating the terms of the serious to get more and more precise values for pi). To get the nth digit, there's a formula out there that gets it in hex notation, but again, that's a series...
 
Thanks,


Erm Growse ^^ Check out the code above, what needs changing?

There is one error left in vb.net...About that givemethedigitofpi is not decalred...

Then the "Givemethedigitofpi" bit as its not declared for obvious reasons.

Also, the Pi calculation goes in that part you put in the code, thats all whats needed right, shall I jsut find the "series" one?
 
Last edited:
You might need to experiment as to exactly what algorithm you use. You don't want the calculation really taking more time than you have the counter set up on. Otherwise, the timer will tick whent the calculation method is still going. Don't quite know if it'll just wait and lag behind or whether it'll spark off another thread to go and do the calculation. Either way is doom, but the second is more doom than the first.

Either way, you'll have something like:

Code:
Private Sub GiveMeTheNthDigitOfPi(ByVal n As System.Integer)

'go round loopything here with n as your argument

End Sub
 
As I said, start reading the book. How do you expect anyone to tell you "how" to convert an entire C++ app into VB.NET? The only thing you can do is rewrite it from scratch.
 
Back
Top Bottom