Here's a four quadrant inverse tangent subroutine named ATAN3. It determines angles between 0 and 2 PI radians.

- Code: Select all

' demo_atan3.bas

' June 24, 2015

' demonstrates how to intereact with the ATAN3 subroutine

dtr = PI / 180.0

rtd = 180.0 / PI

angle_deg = 222.25678

' convert angle to radians

angle_rad = dtr * angle_deg

' compute sin and cosine of angle

a = sin(angle_rad)

b = cos(angle_rad)

' call four quadrant inverse tangent

GOSUB ATAN3:

' print results

print " "

print "user-defined angle = ", angle_deg, " degrees"

print " "

print "a = ", a

print "b = ", b

print " "

print "angle from ATAN3 = ", rtd * c, " degrees"

END

ATAN3:

' four quadrant inverse tangent function

' input

' a = sine of angle

' b = cosine of angle

' output

' c = angle (0 =< c <= 2 * pi : radians)

'''''''''''''''''''''''''''''''''''''''''

pidiv2 = PI / 2.0

x = a

GOSUB SIGN:

sgn_a = sgn

x = b

GOSUB SIGN:

sgn_b = sgn

IF (ABS(a) < 1.0e-10) THEN

c = (1.0 - sgn_b) * pidiv2

RETURN

ELSE

c = (2.0 - sgn_a) * pidiv2

ENDIF

IF (ABS(b) < 1.0e-10) THEN

RETURN

ELSE

c = c + sgn_a * sgn_b * (ABS(ATAN(a / b)) - pidiv2)

ENDIF

RETURN

'''''''''''''''

'''''''''''''''

SIGN:

' sign of a variable subroutine

' input

' x = argument

' output

' sgn = sign of x

''''''''''''''''''

IF (x > 0.0) THEN

sgn = 1.0

ENDIF

IF (abs(x) <= 1.0e-12) THEN

sgn = 0.0

ENDIF

IF (x < 0.0) THEN

sgn = -1.0

ENDIF

RETURN

Here are the results from the demo;

Start program

user-defined angle = 222.25678 degrees

a = -0.672454395

b = -0.7401385589

angle from ATAN3 = 222.25678 degrees

Program Ended.