Jump to content

QBasic/Advanced Maths

From Wikibooks, open books for an open world

Complex mathematical functions

[edit | edit source]

There are a few trigonometrical operations in QBasic such as:

A = 90
PRINT SIN(A)
PRINT COS(A)
PRINT TAN(A)
PRINT ATN(A)

'not trigonometrical
PRINT LOG(A)

Most of the other complex calculations do not have appropriate functions in QBasic and hence you have to use functions and make them yourselves.

FUNCTION SEC (x)  'Secant
IF COS(x) <> 0 THEN SEC = 1 / COS(x) ELSE BEEP
END FUNCTION

FUNCTION CSC (x)  'CoSecant 
IF SIN(x) <> 0 THEN CSC = 1 / SIN(x) ELSE BEEP
END FUNCTION 

FUNCTION COT (x)  'CoTangent 
IF TAN(x) <> 0 THEN COT = 1 / TAN(x) ELSE BEEP
END FUNCTION 

FUNCTION ARCSIN (x)   'Inverse Sine           
IF x < 1 THEN ARCSIN = ATN(x / SQR(1 - (x * x))) ELSE BEEP
END FUNCTION 

FUNCTION ARCCOS (x) ' Inverse Cosine
IF x < 1 THEN ARCCOS = (2 * ATN(1)) - ATN(x / SQR(1 - x * x)) ELSE BEEP
END FUNCTION

FUNCTION ARCSEC (x)   ' Inverse Secant        
IF x < 1 THEN ARCSEC = ATN(x / SQR(1 - x * x)) + (SGN(x) - 1) * (2 * ATN(1)) ELSE BEEP
END FUNCTION 

FUNCTION ARCCSC (x)  ' Inverse CoSecant 
IF x < 1 THEN ARCCSC = ATN(1 / SQR(1 - x * x)) + (SGN(x)-1) * (2 * ATN(1)) ELSE BEEP 
END FUNCTION 

FUNCTION ARCCOT (x)  ' Inverse CoTangent 
ARCCOT = (2 * ATN(1)) - ATN(x)
END FUNCTION  

FUNCTION SINH (x)  ' Hyperbolic Sine 
IF x <= 88.02969 THEN SINH = (EXP(x) - EXP(-x)) / 2 ELSE BEEP
END FUNCTION  

FUNCTION COSH (x)  ' Hyperbolic CoSine 
IF x <= 88.02969 THEN COSH = (EXP(x) + EXP(-x)) / 2 ELSE BEEP
END FUNCTION  

FUNCTION TANH (x)  ' Hyperbolic Tangent or SINH(x) / COSH(x)
IF 2 * x <= 88.02969 AND EXP(2 * x) + 1 <> 0 THEN 
     TANH = (EXP(2 * x) - 1) / (EXP(2 * x) + 1) 
ELSE BEEP 
END IF 
END FUNCTION  

FUNCTION SECH (x)  ' Hyperbolic Secant or (COSH(x)) ^ -1
IF x <= 88.02969 AND (EXP(x) + EXP(-x)) <> 0 THEN SECH = 2 / (EXP(x) + EXP(-x)) ELSE BEEP  
END FUNCTION  

FUNCTION CSCH (x)  ' Hyperbolic CoSecant or (SINH(x)) ^ -1
IF x <= 88.02969 AND (EXP(x) - EXP(-x)) <> 0 THEN CSCH = 2 / (EXP(x) - EXP(-x)) ELSE BEEP  
END FUNCTION  

FUNCTION COTH (x)  ' Hyperbolic CoTangent or COSH(x) / SINH(x)
IF 2 * x <= 88.02969 AND EXP(2 * x) - 1 <> 0 THEN 
     COTH = (EXP(2 * x) + 1) / (EXP(2 * x) - 1) 
ELSE BEEP  
END IF
END FUNCTION  

FUNCTION ARCSINH (x)  ' Inverse Hyperbolic Sine 
IF (x * x) + 1 >= 0 AND x + SQR((x * x) + 1) > 0 THEN 
ARCSINH = LOG(x + SQR(x * x + 1)) 
ELSE BEEP
END IF
END FUNCTION  

FUNCTION ARCCOSH (x)  ' Inverse Hyperbolic CoSine 
IF x >= 1 AND x * x - 1 >= 0 AND x + SQR(x * x - 1) > 0 THEN 
ARCCOSH = LOG(x + SQR(x * x - 1)) 
ELSE BEEP
END IF
END FUNCTION  

FUNCTION ARCTANH (x)  ' Inverse Hyperbolic Tangent 
IF x < 1 THEN ARCTANH = LOG((1 + x) / (1 - x)) / 2 ELSE BEEP
END FUNCTION 

FUNCTION ARCSECH (x)  ' Inverse Hyperbolic Secant 
IF x > 0 AND x <= 1 THEN ARCSECH = LOG((SGN(x) * SQR(1 - x * x) + 1) / x) ELSE BEEP  
END FUNCTION  

FUNCTION ARCCSCH (x)  ' Inverse Hyperbolic CoSecant 
IF x <> 0 AND x * x + 1 >= 0 AND (SGN(x) * SQR(x * x + 1) + 1) / x > 0 THEN 
     ARCCSCH = LOG((SGN(x) * SQR(x * x + 1) + 1) / x) 
ELSE BEEP
END IF
END FUNCTION  

FUNCTION ARCCOTH (x)  ' Inverse Hyperbolic CoTangent 
IF x > 1 THEN ARCCOTH = LOG((x + 1) / (x - 1)) / 2 ELSE BEEP
END FUNCTION

Some examples taken from the QB64 IDE help page.