uses Console
uses ParseUtil

def crlf cr
def waitforuser wait

Function CalculatePhi(X As Double, Y As Double) As Double
    ' These corrections are needed to make Phi take a full
    ' circle i.e. 360 degrees.
    ' Without the corrections Phi will only vary 180 degrees
    ' (-90 to 90) corresponding to the range for arcus tangens (ATN).
    Local Ph As Double
    If Abs(X) < 1E-1000 Then X = 1E-1000
    Ph = Atn(Y / X)
    If Ph < 0 Then Ph = Ph + 3.14159265358979324
    If Y < 0 Then Ph = Ph + 3.14159265358979324
    If Y = 0 And X < 0 Then
        Ph = Ph + 3.14159265358979324
    End If
    Function = Ph
End Function

Sub Main()
    local A As Double, t As String, X As Double, Y As Double, i As Double
    For i = 0 To 7.9 * Atn(1) Step Atn(1) * 0.5
        X = Cos(i) : Y = Sin(i)
        A = CalculatePhi(X, Y)
        t = t + "X=" + Format(X, "##.####") + "    Y=" + Format(Y, "##.####") + _
            "       true angle=" + format(i * 57.29577951308232, "###.######") + _
            "       angle from X and Y=" + Format(A * 57.29577951308232, "###.####") + CRLF
    Next
    Print(t)
    WaitForUser()
End Sub

Main()