Newbie Programming Confusion
08-22-2015, 06:44 PM
Post: #1
 smp Senior Member Posts: 444 Joined: Jul 2015
Newbie Programming Confusion
Hello all,

As a newbie with my Prime, I am slowly attempting different things to learn how to do programming on it (I'm having great fun with this!).

For most of my programs so far, I have dug out old BASIC programs that I have had running before on other vintage computer systems that I have.

My latest program is a simple one that calculates the value of Pi by adding up the sides of a polygon inscribed within a circle. This program comes from an old article from Creative Computing back in 1978:

Anyway, I am surprised that the author's original method of building up the equation to find the length of a side of the polygon does not work properly in my program. I have built a couple of more equations to compute the side length that work properly, but I remain confused why the original method used by the author of the article does not work properly.

Here's my code:
Code:
EXPORT CalcPi() BEGIN // Calculate Pi by inscribed ploygons // Original author: // George W. Ball, Alfred University   R:=10;  // Use a circle of radius 10   N:=4;   S:=R*√(2);   PRINT();   PRINT("Machine value for Pi = " +(4*ATAN(1)));   PRINT("Machine value for Pi = " +PI);   PRINT(" ");   PRINT("Sides      Perimiter");   FOR K FROM 1 TO 28 DO     PRINT(N +"      " +((S*N)/(2*R))); //    Y:= S*S/4; //    H=√(R*R-Y); //    X:=R-H; //    S:=√(X*X+Y);  // does not work //    S:=√((R-(√(R*R-(S*S/4))))*(R-(√(R*R-S*S/4)))+(S*S/4));  // works     S:=√((R-√(R*R-(S/2)^2))^2+(S/2)^2);  // works     N:=2*N;   END; END;

Please take a look (if you want) and if there is something obvious that I'm missing, please let me know.

TIA,
smp
08-22-2015, 08:10 PM
Post: #2
 Thomas_Sch Senior Member Posts: 377 Joined: Dec 2013
RE: Newbie Programming Confusion
(08-22-2015 06:44 PM)smp Wrote:
Code:
EXPORT CalcPi() BEGIN // Calculate Pi by inscribed ploygons // Original author: // George W. Ball, Alfred University   R:=10;  // Use a circle of radius 10   N:=4;   S:=R*√(2);   PRINT();   PRINT("Machine value for Pi = " +(4*ATAN(1)));   PRINT("Machine value for Pi = " +PI);   PRINT(" ");   PRINT("Sides      Perimiter");   FOR K FROM 1 TO 28 DO     PRINT(N +"      " +((S*N)/(2*R))); //    Y:= S*S/4; //    H=√(R*R-Y);   <--- there is the ":" missing //    X:=R-H; //    S:=√(X*X+Y);  // does not work //    S:=√((R-(√(R*R-(S*S/4))))*(R-(√(R*R-S*S/4)))+(S*S/4));  // works     S:=√((R-√(R*R-(S/2)^2))^2+(S/2)^2);  // works     N:=2*N;   END; END;

Please take a look (if you want) and if there is something obvious that I'm missing, please let me know.

TIA,
smp

H=√(R*R-Y); <--- there is the ":" missing
08-22-2015, 08:18 PM
Post: #3
 Tim Wessman Senior Member Posts: 2,280 Joined: Dec 2013
RE: Newbie Programming Confusion
(08-22-2015 06:44 PM)smp Wrote:  Please take a look (if you want) and if there is something obvious that I'm missing, please let me know.

Have you clicked the "DEBUG" menu key in the program catalog? You can then step through one command at a time and you will probably discover where it goes wrong...

TW

Although I work for HP, the views and opinions I post here are my own.
08-22-2015, 09:30 PM (This post was last modified: 08-23-2015 10:53 AM by Thomas Klemm.)
Post: #4
 Thomas Klemm Senior Member Posts: 1,625 Joined: Dec 2013
RE: Newbie Programming Confusion
You could use double angle formulas for cosine and sine to come up with simpler formulas that lead to this Python program:

Code:
from math import sqrt c, s = 0.0, 2.0 for i in range(20):     c = sqrt((1 + c)/2)     s = s / c     print s

This short program runs on most older HP-calculators:

Code:
1 + 2 / SQRT / LASTX

2 ENTER
0

And then run the program until you reach 1.
The resulting approximation of $$\pi$$ can then be displayed with:

X<>Y

Kind regards
Thomas

PS: I haven't seen that font of the heading in the linked article for a long time.

We might use it in this forum in titles:
08-22-2015, 09:35 PM
Post: #5
 smp Senior Member Posts: 444 Joined: Jul 2015
RE: Newbie Programming Confusion
(08-22-2015 08:10 PM)Thomas_Sch Wrote:  H=√(R*R-Y); <--- there is the ":" missing

Hi Thomas,

I see it now! Thank you for pointing out my mistake!

smp
08-22-2015, 09:43 PM
Post: #6
 smp Senior Member Posts: 444 Joined: Jul 2015
RE: Newbie Programming Confusion
(08-22-2015 08:18 PM)Tim Wessman Wrote:  Have you clicked the "DEBUG" menu key in the program catalog? You can then step through one command at a time and you will probably discover where it goes wrong...

Hi Tim,