SHOP TRIGONOMETRY

using GW-BASIC

Go to the Index of "The Last Book of GW-BASIC"
This program shows some methods of establishing values for the sine, cosine and tangent expressed in degrees and not radians. Results from all three different methods are shown. This program is just an example program showing the three different methods. 1 'Ways of returning the Sine, Cosine & Tangent in degree values 2 ' 3 'Method #1: ***Create Variables*** 4 'PI=355/113 5 ' or 6 PI=4*ATN(1) 'establish PI - probably the best method 7 ' then 8 ' r2d= 2*pi/360 'since there are 2PI radians in a circle, we simply divide 9 ' 360 into (2 x 3.1415926535) to get the value for 1 degree 10 ' or reducing the fraction, pi/180 11 ' so 12 R2D=PI/180 'convert from Radians to Degrees 13 ' 14 'Method #2: ***Define FN functions*** 15 DEF FNSINE(X)=SIN(X*4*ATN(1)/180) 'or use the def fn function 16 DEF FNCOSINE(X)=COS(X*4*ATN(1)/180) 'to create your own translation method 17 DEF FNTANGENT(X)=TAN(X*4*ATN(1)/180)'fnsine, fncosine, fntangent 18 ' 19 'Method #3: Straight Declaration 20 'PI=3.1415926535 21 'R2D= pi/180 22 ' 23 CLS:INPUT "Input Degrees: ",X:PRINT " Values are in Degrees" 24 ' 25 PRINT " sine cosine tangent" 26 ' 27 PRINT FNSINE(X), FNCOSINE(X), FNTANGENT(X) 'sin/cos/tan using FN 28 ' 29 PRINT SIN(X*PI/180),COS(X*PI/180), TAN(X*PI/180) 'sin/cos/tan varibles 30 ' 31 PRINT SIN(X*R2D),COS(X*R2D), TAN(X*R2D) 'sin/cos/tan w r2d variable 32 ' save "trig.bas",a 33 ' or you could just buy a cheap calculator :-)
This this snippit of code gives all six (6) transcendental functions plus their reciprocals for a given degree value EXPRESSED IN DECIMAL DEGREES. So if you want the sine of 30 degrees and 20 minutes you have to imput 30.3333 degrees as 20 minutes is 1/3 of a degree. 15 minutes is 1/4 of a degree and so on... Finally a 5" sinebar setting is offered as well. 10 CLS: PRINT TAB(30)"The Trig Book" 20 PI=3.141592654# 30 R2D=PI/180 40 INPUT "Input degrees ",DEGREES 50 PRINT"The Sine of";DEGREES;"degrees is: ";USING "###.######";SIN(DEGREES*R2D) 60 PRINT"The Cosine of";DEGREES;"degrees is: ";USING "###.######";COS(DEGREES*R2D) 70 PRINT"The Tangent of";DEGREES;"degrees is: ";USING "###.######";TAN(DEGREES*R2D) 80 PRINT"The Cotangent of";DEGREES;"degrees is: ";USING "###.######";1/TAN(DEGREES*R2D) 90 PRINT"The Secant of";DEGREES;"degrees is: ";USING "###.######";1/COS(DEGREES*R2D) 100 PRINT"The Cosecant of";DEGREES;"degrees is: ";USING "###.######";1/SIN(DEGREES*R2D) 110 PRINT"5 inch sinebar setting is: ";USING "##.####";SIN(DEGREES*R2D)*5;:PRINT " inches" 120 ' save"trigbook.bas",a
The next six programs deliver the inverse function of the sine, cosine, tangent, cotangent, secant and cosecant in radians, decimal degrees, and degrees, minutes, seconds. giving you a complete six dollar calculator. nifty huh?
10 'Delivers the arcsine of a value. 20 ' 30 'VariableAssignments: 40 LOCATE,,,0,7 'block cursor 50 PI=4*ATN(1) 'establish the value for PI 60 R2D=PI/180 'convert from radians to degrees 70 ' 80 'Ask4SineValue: 90 INPUT "input sine value: ";SINE 100 ' 110 'EstablishValueLimits: 120 IF SINE>1 THEN PRINT "Sine Value too high":GOTO 90 130 IF SINE=1 THEN PRINT "the angle whose sine is 1 is 90 degrees":END 140 IF SINE<0 THEN PRINT "Sine Value too low":GOTO 90 150 ' 160 'Do all the DMS monkey motion: 170 DECIMALDEGREE=ATN(SINE/SQR(-SINE*SINE+1))/R2D 180 DEGREES=INT(DECIMALDEGREE) 190 ' 200 DECIMALMINUTES=DECIMALDEGREE-INT(DECIMALDEGREE) 210 MINUTES=INT(DECIMALMINUTES*60) 220 ' 230 DECIMALSECONDS=DECIMALMINUTES*60-MINUTES 240 SECONDS=INT(DECIMALSECONDS*60) 250 ' 260 'PrintTheMess: 270 PRINT"the angle whose sine is:"; 280 PRINT SINE;"is"; 290 PRINT TAB(25) ATN(SINE/SQR(-SINE*SINE+1));"Radians" 300 PRINT TAB(22)"or: "DECIMALDEGREE;"decimal degrees" 310 PRINT TAB(22)"or: "DEGREES;"degrees";MINUTES;"minutes";SECONDS;"seconds" 320 ' 330 ' save"asindms.bas",a
10 'Delivers the arccosine of a value 20 ' 30 'VariableAssignments: 40 LOCATE,,,0,7 'block cursor 50 PI=4*ATN(1) 'establish the value for PI 60 R2D=PI/180 'convert from radians to degrees 70 ' 80 'Ask4CosineValue: 90 INPUT "input cosine value: ";COSINE 100 ' 110 'EstablishValueLimits: 120 IF COSINE>1 THEN PRINT"Cosine value too high":GOTO 90 130 IF COSINE=1 THEN PRINT"The cosine value of 1 is 90 degrees":END 140 IF COSINE<0 THEN PRINT"Cosine value too low":GOTO 90 150 ' 160 'Do all the DMS monkey motion: 170 DECIMALDEGREE=ATN(COSINE/SQR(-COSINE*COSINE+1))/R2D 180 DEGREES=INT(DECIMALDEGREE) 190 ' 200 DECIMALMINUTES=DECIMALDEGREE-INT(DECIMALDEGREE) 210 MINUTES=INT(DECIMALMINUTES*60) 220 ' 230 DECIMALSECONDS=DECIMALMINUTES*60-MINUTES 240 SECONDS=INT(DECIMALSECONDS*60) 250 ' 260 'PrintTheMess: 270 PRINT"the angle whose cosine is:"; 280 PRINT COSINE;"is: " 290 PRINT TAB(28) ATN(COSINE/SQR(-COSINE*COSINE+1));"Radians" 300 PRINT TAB(22)"or: "DECIMALDEGREE;"Decimal Degrees" 310 PRINT TAB(22)"or: "DEGREES;"Degrees";MINUTES;"Minutes";SECONDS;"Seconds" 320 ' 330 ' save"acosdms.bas",a
10 'returns the arctangent of a value 20 ' 30 'Variable Assignments: 40 LOCATE,,,0,7 'block cursor 50 PI=4*ATN(1) 'establish the value for PI 60 R2D=PI/180 'convert from radians to degrees 70 ' 80 'Ask4TangentValue: 90 INPUT "input tangent value: ";TANGENT 100 ' 110 'Do all the DMS monkey motion: 120 DECIMALDEGREES=ATN(TANGENT)/R2D 130 DEGREES=INT(DECIMALDEGREES) 140 ' 150 DECIMALMINUTES=DECIMALDEGREES-INT(DECIMALDEGREES) 160 MINUTES=INT(DECIMALMINUTES*60) 170 ' 180 DECIMALSECONDS=DECIMALMINUTES*60-MINUTES 190 SECONDS=INT(DECIMALSECONDS*60) 200 ' 210 'PrintTheMess: 220 PRINT"the angle whose tangent is:"; 230 PRINT TANGENT;"is: " 240 PRINT TAB(28) ATN(TANGENT);"Radians" 250 PRINT TAB(22)"or: ";ATN(TANGENT)/R2D;"Decimal Degrees" 260 PRINT TAB(22)"or: ";DEGREES;"Degrees";MINUTES;"Minutes";SECONDS;"Seconds" 270 ' 280 ' save"atandms.bas",a
This one is a little more complete 10 'returns the arccotangent of a value 20 ' 30 'VariableAssignments: 40 LOCATE,,,0,7 'block cursor 50 PI=4*ATN(1) 'establish the value for PI 60 R2D=PI/180 'convert from radians to degrees 70 ' 80 'Ask4CotangentValue: 90 INPUT "input cotangent value: ";COTANGENT 100 ' 110 'Establish all the values and tuck them into variables: 120 ' 130 'Radians first: 140 RADIANS=(-ATN(COTANGENT)+PI/2) 150 ' 160 'Then the decimal degrees: 170 DECIMALDEGREES=(-ATN(COTANGENT)+PI/2)/R2D 180 ' 190 'Degrees: 200 DEGREES=INT(DECIMALDEGREES) 210 ' 220 'Minutes: 230 DECIMALMINUTES=DECIMALDEGREES-INT(DECIMALDEGREES) 240 MINUTES=INT(DECIMALMINUTES*60) 250 ' 260 'Seconds: 270 DECIMALSECONDS=DECIMALMINUTES*60-MINUTES 280 SECONDS=INT(DECIMALSECONDS*60) 290 ' 300 'PrintTheMess: 310 PRINT"the angle whose cotangent is:"; 320 PRINT COTANGENT;"is: " 330 PRINT TAB(28) RADIANS;"Radians" 340 PRINT TAB(22)"or: "DECIMALDEGREES;"Decimal Degrees" 350 PRINT TAB(22)"or: "DEGREES;"Degrees";MINUTES;"Minutes";SECONDS;"Seconds" 360 ' 370 ' save"acotdms.bas",a
i've never, ever needed the arcsecant, but here it is anyway. 10 'returns the arcsecant of a value 20 ' 30 ' As a personal note, keep the input over 1.0" 40 ' After thrashing for 2 days swearing at the formulas offered 50 ' i realized that my low input was causing all the grief. 60 ' And this with a table of secant values right in front of me. 70 ' Apologies to every source that was sworn at. :-) sigh... 80 ' 90 'VariableAssignments: 100 LOCATE,,,0,7 'block cursor 110 PI=4*ATN(1) 'establish the value for PI 120 R2D=PI/180 'convert from radians to degrees 130 ' 140 'Ask4SecantValue: 150 INPUT "input secant value: ";SECANT 160 ' 170 'CheckEntry4CorrectValues: 180 IF SECANT < 1 THEN PRINT "Secant is always >= 1.000":GOTO 150 190 ' 200 'Establish all the values and tuck them into variables: 210 ' 220 'Radians first: 230 RADIANS=ATN(SECANT/SQR(SECANT*SECANT-1))+SGN(SGN(SECANT)-1)*PI/2 240 RADIANS=ATN(SQR(SECANT*SECANT-1))+(X<0)*PI 250 ' 260 'Then the decimal degrees: 270 DECIMALDEGREES=RADIANS/R2D 280 ' 290 'Degrees: 300 DEGREES=INT(DECIMALDEGREES) 310 ' 320 'Minutes: 330 DECIMALMINUTES=DECIMALDEGREES-INT(DECIMALDEGREES) 340 MINUTES=INT(DECIMALMINUTES*60) 350 ' 360 'Seconds: 370 DECIMALSECONDS=DECIMALMINUTES*60-MINUTES 380 SECONDS=INT(DECIMALSECONDS*60) 390 ' 400 'PrintTheMess: 410 PRINT"the angle whose secant is:"; 420 PRINT SECANT;"is: " 430 PRINT TAB(28) RADIANS;"Radians" 440 PRINT TAB(22)"or: "DECIMALDEGREES;"Decimal Degrees" 450 PRINT TAB(22)"or: "DEGREES;"Degrees";MINUTES;"Minutes";SECONDS;"Seconds" 460 ' 470 ' save"asecdms.bas",a
Oh my! aren't we getting advanced... yet another "never used it" function. 10 'returns the arccosecant of a value 20 ' 30 'VariableAssignments: 40 LOCATE,,,0,7 'block cursor 50 PI=4*ATN(1) 'establish the value for PI 60 R2D=PI/180 'convert from radians to degrees 70 ' 80 'Ask4CosecantValue: 90 PRINT"Keep the input greater then 1.0" 100 INPUT "input cosecant value: ";COSECANT 110 ' 120 'CheckEntry4CorrectValues: 130 IF COSECANT < 1 THEN PRINT "Cosecant is always >= 1.000":GOTO 100 140 ' 150 'Establish all the values and tuck them into variables: 160 ' 170 'Radians first: 'Either of these algorithms seem to work okay. 180 ' RADIANS=ATN(1/SQR(COSECANT*COSECANT-1))+(SGN(COSECANT)-1)*PI/2 190 RADIANS=ATN(1/SQR(COSECANT*COSECANT-1))+(X<0)*PI 200 ' 210 'Then the decimal degrees: 220 DECIMALDEGREES=RADIANS/R2D 230 ' 240 'Degrees: 250 DEGREES=INT(DECIMALDEGREES) 260 ' 270 'Minutes: 280 DECIMALMINUTES=DECIMALDEGREES-INT(DECIMALDEGREES) 290 MINUTES=INT(DECIMALMINUTES*60) 300 ' 310 'Seconds: 320 DECIMALSECONDS=DECIMALMINUTES*60-MINUTES 330 SECONDS=INT(DECIMALSECONDS*60) 340 ' 350 'PrintTheMess: 360 PRINT"the angle whose cosecant is:"; 370 PRINT COSECANT;"is: " 380 PRINT TAB(28) RADIANS;"Radians" 390 PRINT TAB(22)"or: "DECIMALDEGREES;"Decimal Degrees" 400 PRINT TAB(22)"or: "DEGREES;"Degrees";MINUTES;"Minutes";SECONDS;"Seconds" 410 ' 420 ' save"acscdms.bas",a
Here are some shorties if you don't need all the goobblygook
10 'Delivers the arcsine of a value 20 LOCATE,,,0,7 'block cursor 30 PI=4*ATN(1) 'establish the value for PI 40 R2D=PI/180 'convert from radians to degrees 50 INPUT "input sine value: ";SINE 60 IF SINE>1 THEN PRINT "Sine Value too high":GOTO 50 70 IF SINE=1 THEN PRINT "the angle whose sine is 1 is 90 degrees":END 80 IF SINE<0 THEN PRINT "Sine Value too low":GOTO 50 90 ARCSIN=ATN(SINE/SQR(-SINE*SINE+1)) 100 PRINT"the angle whose sine is";SINE;"is";ARCSIN/R2D;"decimal degrees" 110 ' save"arcsin.bas",a
10 'Delivers the arccosine of a value 20 LOCATE,,,0,7 'block cursor 30 PI=4*ATN(1) 'establish the value for PI 40 R2D=PI/180 'convert from radians to degrees 50 INPUT "input cosine value: ";COSINE 60 IF COSINE>1 THEN PRINT"Cosine value too high":GOTO 50 70 IF COSINE=1 THEN PRINT"The cosine value of 1 is 90 degrees":END 80 IF COSINE<0 THEN PRINT"Cosine value too low":GOTO 50 90 ARCCOSINE=ATN(COSINE/SQR(-COSINE*COSINE+1)) 100 PRINT"The cosine value of";COSINE;"is";ARCCOSINE/R2D;"decimal degrees" 110 ' save"arccos.bas",a
10 'returns the arctangent of a value 20 LOCATE,,,0,7 'block cursor 30 PI=4*ATN(1) 'establish the value for PI 40 R2D=PI/180 'convert from radians to degrees 50 INPUT "input tangent value: ";TANGENT 60 PRINT "The angle whose tangent is ";TANGENT;"is ";ATN(TANGENT)/R2D;"decimal degrees" 70 ' save"arctan.bas",a
Here is a little fun program of just two lines that does the same thing as the above program. 1 PI=4*ATN(1):R2D=PI/180:INPUT"input tangent value: ";TANGENT 2 PRINT "The angle whose tangent is ";TANGENT;"is ";ATN(TANGENT)/R2D;"decimal degrees"
Here are three little programs for the Sine, Cosine and Tangent that you can use if you only have a three dollar four function calculator instead of a six dollar calculator. i used sine, cosine and tangent for about 55 years almost daily and never knew that a series expansion was available that didn't need a scientific calculator or slide rule. Talk about dumb... :-( Anyhow here they are:
10 'Sine Series Expansion 20 'The expansion series for the Sine is: 30 ' SinZ = Z-Z^3/3!+Z^5/5!-Z^7/7!+Z^9/9!... 40 ' 50 'Housekeeping: 60 LOCATE,,,0,7:COLOR 14,4:CLS 70 ' 80 'VariableAssignments: 90 PI=4#*ATN(1#) 100 R2D=PI/180# 110 ' 120 'ScreenPainter: 130 PRINT:PRINT 140 PRINT TAB(30)"Sine Series Expansion":PRINT TAB(36)"using" 150 PRINT TAB(18)"Sine of X = X-X^3/3!+X^5/5!-X^7/7!+X^9/9!..." 160 ' 170 'InputRoutine: 180 LOCATE 9,20:INPUT "Degrees: ";DEGREES 190 IF DEGREES=90 THEN LOCATE 13,15:PRINT "The sine of 90 degrees is 1.00000":END 200 IF DEGREES>90 OR DEGREES<0 THEN GOTO 60 210 RADIANS=DEGREES*R2D 'convert degree input to radians 220 ' 230 'DoTheSeriesExpansions: 240 SINE=RADIANS-RADIANS^3/6+RADIANS^5/120-RADIANS^7/5040+RADIANS^9/362880!-RADIANS^11/(1*2*3*4*5*6*7*8*9*10*11) 250 ' 260 'PrintTheResults: 270 LOCATE 13, 15:PRINT "The sine of";DEGREES;"degrees is: "; 280 PRINT USING "#.#######";SINE 290 ' 300 'EmbeddedCommands: 310 SAVE"SINEXPAN.BAS",A
10 'Cosine Series Expansion 20 'The expansion series for the Cosine is: 30 ' CosZ = 1-Z^2/2!+Z^4/4!-Z^6/6!+Z^8/8!... 40 ' 50 'Housekeeping: 60 LOCATE,,,0,7:COLOR 14,4:CLS 70 ' 80 'VariableAssignments: 90 PI=4#*ATN(1#) 100 R2D=PI/180# 110 ' 120 'ScreenPainter: 130 PRINT:PRINT 140 PRINT TAB(30)"Cosine Series Expansion":PRINT TAB(36)"using" 150 PRINT TAB(18)"Cosine of X = 1-X^2/2!+X^4/4!-X^6/6!+X^8/8!..." 160 ' 170 'InputRoutine: 180 LOCATE 9,20:INPUT "Degrees: ";DEGREES 190 IF DEGREES=0 THEN LOCATE 13,15:PRINT "The cosine of 0 degrees is 1.00000":END 200 IF DEGREES=90 THEN LOCATE 13,15:PRINT"The cosine of 90 degress is zero":END 210 IF DEGREES>90 OR DEGREES<0 THEN GOTO 60 220 RADIANS=DEGREES*R2D 'convert degree input to radians 230 ' 240 'DoTheSeriesExpansions: 250 COSINE=1-RADIANS^2/2+RADIANS^4/24-RADIANS^6/720+RADIANS^8/40320!-RADIANS^10/(1*2*3*4*5*6*7*8*9*10) 'this is just 10 factorial 260 ' 270 'PrintTheResults: 280 LOCATE 13, 15:PRINT "The cosine of";DEGREES;"degrees is: "; 290 PRINT USING "#.#######";COSINE 300 ' 310 'EmbeddedCommands: 320 SAVE"COSEXPAN.BAS",A
A lot of math books present this series Tangent algorithm in an unclear fashion for a joseph_sixpack. Simply don't understand it correctly. (turned out to be limits) operator error again... i never could get the tangent function to work correctly so i used an old fashioned approach of combining the two formulas, sine & cosine. sorry, maybe i can find a correct algorithm later on. anyhow, this one seems to work okay. Be aware however that most of the time, my problems with an algorithm can be traced back to operator error. Now my method of sin/cos to find the tangent is good up to 90 degrees. The fancy formula method just goes up to 45 degrees and then you gotta go the cofunction of the complementary angle routine so here i use both sine and cosine series expansion. it works good. 