' *****************************************************
' PROGRAM:      trig.bac
' PURPOSE:      to provide some extended trig functions
' AUTHOR:               vovchik (Puppy Linux forum)
' DEPENDS:      bacon, gcc
' PLATFORM:     Puppy Linux (actually, any *nix)
' DATE:         29-10-2010
' *****************************************************


' *********************
' DECLARATIONS
' *********************

GLOBAL X, Y, Z TYPE FLOATING
GLOBAL N TYPE NUMBER

' *********************
' END DECLARATIONS
' *********************


' *********************
' INIT VARS
' *********************

X = 10.67
Y = 0.345
Z = 1.123
N = 10

' *********************
' END INIT VARS
' *********************


' *********************
' SUBS & FUNCTIONS
' *********************

' --------------------
FUNCTION SEC(FLOATING X)
' --------------------
        ' Secant
        LOCAL MyNum TYPE float
        MyNum = 1 / COS(X)
        RETURN MyNum
END FUNCTION

' --------------------
FUNCTION COSEC(FLOATING X)
' --------------------
        ' Cosecant
        LOCAL MyNum TYPE float
        MyNum = 1 / SIN(X)
        RETURN MyNum
END FUNCTION

' --------------------
FUNCTION COTAN(FLOATING X)
' --------------------
        ' Cotangent
        LOCAL MyNum TYPE float
        MyNum = 1 / TAN(X)
        RETURN MyNum
END FUNCTION

' --------------------
FUNCTION ARCSIN(FLOATING X)
' --------------------
        ' Inverse sine
        LOCAL MyNum TYPE float
        MyNum = ATN(X / SQR(-X * X + 1))
        RETURN MyNum
END FUNCTION

' --------------------
FUNCTION ARCCOS(FLOATING X)
' --------------------
        ' Inverse cosine
        LOCAL MyNum TYPE float
        MyNum = ATN(-X / SQR(-X * X + 1)) + 2 * ATN(1)
        RETURN MyNum
END FUNCTION

' --------------------
FUNCTION ARCSEC(FLOATING X)
' --------------------
        ' Inverse secant
        LOCAL MyNum TYPE float
        MyNum = ATN(X / SQR(X * X - 1)) + SGN(X - 1) * (2 * ATN(1))
        RETURN MyNum
END FUNCTION

' --------------------
FUNCTION ARCCOSEC(FLOATING X)
' --------------------
        ' Inverse cosecant
        LOCAL MyNum TYPE float
        MyNum = ATN(X / SQR(X * X - 1)) + (SGN(X) - 1) * (2 * ATN(1))
        RETURN MyNum
END FUNCTION

' --------------------
FUNCTION ARCCOTAN(FLOATING X)
' --------------------
        ' Inverse cotangent
        LOCAL MyNum TYPE float
        MyNum = ATN(X) + 2 * ATN(1)
        RETURN MyNum
END FUNCTION

' --------------------
FUNCTION HSIN(FLOATING X)
' --------------------
        ' Hyperbolic sine
        LOCAL MyNum TYPE float
        MyNum = (EXP(X) - EXP(-X)) / 2
        RETURN MyNum
END FUNCTION

' --------------------
FUNCTION HCOS(FLOATING X)
' --------------------
        ' Hyperbolic cosine
        LOCAL MyNum TYPE float
        MyNum = (EXP(X) + EXP(-X)) / 2
        RETURN MyNum
END FUNCTION

' --------------------
FUNCTION HTAN(FLOATING X)
' --------------------
        ' Hyperbolic tangent
        LOCAL MyNum TYPE float
        MyNum = (EXP(X) - EXP(-X)) / (EXP(X) + EXP(-X))
        RETURN MyNum
END FUNCTION

' --------------------
FUNCTION HSEC(FLOATING X)
' --------------------
        ' Hyperbolic secant
        LOCAL MyNum TYPE float
        MyNum = 2 / (EXP(X) + EXP(-X))
        RETURN MyNum
END FUNCTION

' --------------------
FUNCTION HCOSEC(FLOATING X)
' --------------------
        ' Hyperbolic cosecant
        LOCAL MyNum TYPE float
        MyNum = 2 / (EXP(X) - EXP(-X))
        RETURN MyNum
END FUNCTION

' --------------------
FUNCTION HCOTAN(FLOATING X)
' --------------------
        ' Hyperbolic cotangent
        LOCAL MyNum TYPE float
        MyNum = (EXP(X) + EXP(-X)) / (EXP(X) - EXP(-X))
        RETURN MyNum
END FUNCTION

' --------------------
FUNCTION HARCSIN(FLOATING X)
' --------------------
        ' Inverse hyperbolic sine
        LOCAL MyNum TYPE float
        MyNum = LOG(X + SQR(X * X + 1))
        RETURN MyNum
END FUNCTION

' --------------------
FUNCTION HARCCOS(FLOATING X)
' --------------------
        ' Inverse hyperbolic cosine
        LOCAL MyNum TYPE float
        MyNum = LOG(X + SQR(X * X - 1))
        RETURN MyNum
END FUNCTION

' --------------------
FUNCTION HARCTAN(FLOATING X)
' --------------------
        ' Inverse hyperbolic tangent
        LOCAL MyNum TYPE float
        MyNum = LOG((1 + X) / (1 - X)) / 2
        RETURN MyNum
END FUNCTION

' --------------------
FUNCTION HARCSEC(FLOATING X)
' --------------------
        ' Inverse hyperbolic secant
        LOCAL MyNum TYPE float
        MyNum = LOG((SQR(-X * X + 1) + 1) / X)
        RETURN MyNum
END FUNCTION

' --------------------
FUNCTION HARCCOSEC(FLOATING X)
' --------------------
        ' Inverse hyperbolic cosecant
        LOCAL MyNum TYPE float
        MyNum = LOG((SGN(X) * SQR(X * X + 1) + 1) / X)
        RETURN MyNum
END FUNCTION

' --------------------
FUNCTION HARCCOTAN(FLOATING X)
' --------------------
        ' Inverse hyperbolic cotangent
        LOCAL MyNum TYPE float
        MyNum = LOG((X + 1) / (X - 1)) / 2
        RETURN MyNum
END FUNCTION

' --------------------
FUNCTION LOGN(FLOATING X, NUMBER N)
' --------------------
        ' Logarithm to base N
        LOCAL MyNum TYPE float
        MyNum = LOG(X) / LOG(N)
        RETURN MyNum
END FUNCTION

' *********************
' END SUBS & FUNCTIONS
' *********************


' *********************
' MAIN
' *********************

CLEAR
' test of (mostly) trig functions
PRINT
PRINT "   *** TEST OF BACON TRIG FUNCTIONS ***   "
PRINT "------------------------------------------"
PRINT "X = 10.67, Y = 0.345, Z = 1.123, N = 10"
PRINT "------------------------------------------"
PRINT "01. ", ARCCOSEC(X)
PRINT "02. ", ARCCOS(Y)
PRINT "03. ", ARCCOTAN(X)
PRINT "04. ", ARCSEC(X)
PRINT "05. ", ARCSIN(Y)
PRINT "06. ", COSEC(X)
PRINT "07. ", COTAN(X)
PRINT "08. ", HARCCOSEC(X)
PRINT "09. ", HARCCOS(X)
PRINT "10. ", HARCCOTAN(X)
PRINT "11. ", HARCSEC(Y)
PRINT "12. ", HARCSIN(X)
PRINT "13. ", HARCTAN(Y)
PRINT "14. ", HCOSEC(Y)
PRINT "15. ", HCOS(X)
PRINT "16. ", HCOTAN(Y)
PRINT "17. ", HSEC(Y)
PRINT "18. ", HSIN(X)
PRINT "19. ", HTAN(Y)
PRINT "20. ", LOGN(X, N)
PRINT "21. ", SEC(X)
PRINT "------------------------------------------"
PRINT

' *********************
' END MAIN
' *********************