It is currently Mon Dec 09, 2013 10:22 am

 All times are UTC

 Page 1 of 1 [ 5 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Approximating PiPosted: Tue Jul 07, 2009 8:33 pm
 Level 1 Cleric

Joined: Tue Jul 07, 2009 8:17 pm
Posts: 12
So I wrote a program using Delphi to approximate Pi using the formula (Pi^2)/12 = 1 - 1/4 + 1/9 - 1/16 + 1/25 - ... but the answer it gives me isn't right. No matter how many terms I add it gives me 3.00000000000. Here's the code:

Code:
program Pi_Estimator;

{Program for estimating Pi. Written by Daniel Young.}

{\$APPTYPE CONSOLE}

uses
SysUtils;

var
Pi: real;
n: longint;
b: longint;
i: longint;
a: longint;

begin
writeln('Welcome to the Pi approximator!');
write('Enter how many terms you would like to use ');
write('to estimate Pi:');
writeln;
writeln;
writeln('Now enter how many decimal places you would ');
write('like to output Pi to:');

i := 2;
Pi := 0;
a := 4;
while (i <> n) do
begin
if (i mod 2 = 0) then
begin
a := abs(a);
a := a+2*i + 1
end
else
a := -1*(2*i + 1)-a;
Pi := Pi + (1 div a);
i := i + 1;
end;

Pi := sqrt(12*(Pi + 0.75));
writeln;
writeln;
writeln(Pi:0:b);
end.

So if this looks confusing I'll try to explain how I wrote it (I only took one semester of programming, so it might not be the most efficient code). First the program gets the number of terms to use from the user, as well as how many decimal places to print Pi to. Set i to 2 and a to 4 so that the numbers come out right on the denominator of the terms and it goes through the loop adding terms 1/9 - 1/16 + 1/25... so on using the if then statement to see if the term should be positive or negative. After i gets to n it stops. Since Pi still needs the 1 and - 1/4 terms (the first two terms of the series), I add those (which equals .75), solved for Pi in the formula, and printed to b decimal places.

If you don't understand part of the code I wrote just let me know and I'll explain it. I'm not very good at writing clear precise code yet, so bear with me. Hope you guys can shed some light on the problem. Thanks!

Top

 Post subject: Posted: Tue Jul 07, 2009 9:07 pm
 Corpse Bride

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2221
Location: England
div does integer division.

'1 div a' will return zero for all a>1.

_________________
I ain't pushing no moon buttons.

Top

 Post subject: Posted: Tue Jul 07, 2009 10:46 pm
 Level 1 Cleric

Joined: Tue Jul 07, 2009 8:17 pm
Posts: 12
Jasmine wrote:
div does integer division.

'1 div a' will return zero for all a>1.

Egad! I would have NEVER figured that out. Thank you!...What could I do to get a division that doesn't yield 0?

Top

 Post subject: Posted: Tue Jul 07, 2009 11:41 pm
 Corpse Bride

Joined: Tue Jul 01, 2008 11:44 pm
Posts: 2221
Location: England
Code:
Pi: = Pi + 1 / a;

_________________
I ain't pushing no moon buttons.

Top

 Post subject: Posted: Wed Jul 08, 2009 12:23 am
 Level 1 Cleric

Joined: Tue Jul 07, 2009 8:17 pm
Posts: 12
Omg, I thought the / wasn't allowed in Pascal for division. Hah! Thanks!

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 5 posts ]

 All times are UTC

#### Who is online

Users browsing this forum: No registered users and 1 guest

 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 ------------------ Forums    Forum Rules and Posting Guidelines Wiki Discussion    Help    Content Issues Game Programming Discussion    C and C++ Game Programming    Java Game Programming    Language Agnostic Programming    .NET Game Programming    VB Game Programming    Mobile Game Programming    Web-Based Game Programming    Other Languages    OpenGL Development    Direct X Development Game Development Discussion    Game Design    Game Media Off-Topic Discussion    Announcements    Off-Topic    Community Projects    News