The Editors and Readers of COMPUTE!

A Piece Of Pi

The use of angular measurement in radians mentioned in one of your recent letters (COMPUTE!, December 1984) brings up another point. Where pi is not an intrinsic function of your computer, it is important how you define it in the program, especially when it is evaluated in sines and cosines and the result is compared to one or zero. Consider the following portion of a program:

30
B=SIN(A*(PI/180))

40 IF B=0 THEN GOTO 100

50 GOTO 10

40 IF B=0 THEN GOTO 100

50 GOTO 10

where the value of the variable PI is defined earlier in the program and A is some variable you are interested in. If A reaches the value 180, we have SIN(PI)=0 or B=0 unless PI is not precisely equal to the value of pi as defined by your computer. This is a question of your computer's accuracy. PI should be defined as

5
PI=4*ATN(1)

where ATN is the arctangent function, which is present in almost every dialect of BASIC. This technique always defines PI to the accuracy of your machine by using an intrinsic function, whereas

PI
= 3.1416

or especially

PI
=22/7

may not give B=0 (still assuming A=180). If you are unsure about the accuracy of your computer, always define PI as in line 5. If you do not, you may never exit a loop, or even worse, lose control of the program and get back the worst of all possible results-reasonable-looking garbage.

Kendall B. Smith