Mathematical Operations: Difference between revisions
m (Removed protection from "Mathematical Operations") |
No edit summary |
||
(20 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
{| align="right" style="max-width:30%;" | |||
{|align="right" | | __TOC__ | ||
|__TOC__ | |||
|} | |} | ||
== Basic and QB64 Numerical Types == | |||
==Basic and QB64 Numerical Types== | |||
<center>'''QBasic Number Types'''</center> | <center>'''QBasic Number Types'''</center> | ||
* [[INTEGER]] ['''%''']: 2 Byte signed whole number values from -32768 to 32767. 0 to 65535 unsigned. (not checked in QB64) | * [[INTEGER]] ['''%''']: 2 Byte signed whole number values from -32768 to 32767. 0 to 65535 unsigned. (not checked in QB64) | ||
* [[LONG]] ['''&''']: 4 byte signed whole number values from -2147483648 to 2147483647. 0 to 4294967295 unsigned. | * [[LONG]] ['''&''']: 4 byte signed whole number values from -2147483648 to 2147483647. 0 to 4294967295 unsigned. | ||
* [[SINGLE]] ['''!''']: 4 byte signed floating decimal point values of up to 7 decimal place accuracy. '''Cannot be unsigned.''' | * [[SINGLE]] ['''!''']: 4 byte signed floating decimal point values of up to 7 decimal place accuracy. '''Cannot be unsigned.''' | ||
* [[DOUBLE]] [#]: 8 byte signed floating decimal point values of up to 15 decimal place accuracy. '''Cannot be unsigned.''' | * [[DOUBLE]] [#]: 8 byte signed floating decimal point values of up to 15 decimal place accuracy. '''Cannot be unsigned.''' | ||
* To get '''one byte''' values, can use an [[ASCII]] [[STRING]] character to represent values from 0 to 255 as in [[BINARY]] files. | * To get '''one byte''' values, can use an [[ASCII]] [[STRING]] character to represent values from 0 to 255 as in [[BINARY]] files. | ||
Line 19: | Line 17: | ||
* [[_BYTE]] ['''%%''']: 1 byte signed whole number values from -128 to 127. Unsigned values from 0 to 255. | * [[_BYTE]] ['''%%''']: 1 byte signed whole number values from -128 to 127. Unsigned values from 0 to 255. | ||
* [[_INTEGER64]] ['''&&''']: 8 byte signed whole number values from -9223372036854775808 to 9223372036854775807 | * [[_INTEGER64]] ['''&&''']: 8 byte signed whole number values from -9223372036854775808 to 9223372036854775807 | ||
* [[_FLOAT]] [##]: currently set as 10 byte signed floating decimal point values up to 1. | * [[_FLOAT]] [##]: currently set as 10 byte signed floating decimal point values up to +/-1.189731495357231765F+4932. '''Cannot be unsigned.''' | ||
* [[_OFFSET]] [%&]: undefined flexable length integer offset values used in [[DECLARE | * [[_OFFSET]] [%&]: undefined flexable length integer offset values used in [[DECLARE LIBRARY]] declarations. | ||
Line 37: | Line 35: | ||
<center>[[#toc|Return to Top]]</center> | <center>[[#toc|Return to Top]]</center> | ||
==Mathematical Operation Symbols== | |||
== Mathematical Operation Symbols == | |||
Most of the BASIC math operators are ones that require no introduction. The addition, subtraction, multplication and division operators are ones commonly used as shown below: | Most of the BASIC math operators are ones that require no introduction. The addition, subtraction, multplication and division operators are ones commonly used as shown below: | ||
{| align="center" border=1 | {| align="center" border=1 | ||
! Symbol | ! Symbol | ||
! Procedure Type | ! Procedure Type | ||
! Example Usage | ! Example Usage | ||
! Operation Order | ! Operation Order | ||
|- | |- | ||
| align="center" |[[+]] || Addition || align="center" | c = a + b || align="center" | Last | | align="center" |[[+]] || Addition || align="center" | c = a + b || align="center" | Last | ||
|- | |- | ||
Line 51: | Line 50: | ||
|- | |- | ||
| align="center" |[[-]] || Negation || align="center" | c = - a || align="center" | Last | | align="center" |[[-]] || Negation || align="center" | c = - a || align="center" | Last | ||
|- | |- | ||
| align="center" |[[*]] || Multiplication || align="center" | c = a * b || align="center" | Second | | align="center" |[[*]] || Multiplication || align="center" | c = a * b || align="center" | Second | ||
|- | |- | ||
Line 65: | Line 64: | ||
!Procedure Type | !Procedure Type | ||
!Example Usage | !Example Usage | ||
!Operation Order | !Operation Order | ||
|- | |- | ||
| align="center" |[[\]] || Integer division || align="center" | c = a \ b || align="center" | Second | | align="center" |[[\]] || Integer division || align="center" | c = a \ b || align="center" | Second | ||
|- | |- | ||
| align="center" |[[MOD]] || Remainder division || align="center" | c = a MOD b || align="center" | Second | | align="center" |[[MOD]] || Remainder division || align="center" | c = a MOD b || align="center" | Second | ||
Line 76: | Line 75: | ||
There is also an operator for '''exponential''' calculations. The exponential operator is used to raise a number's value to a designated exponent of itself. In QB the exponential return values are [[DOUBLE]] values. The [[SQR]] function can return a number's Square Root. For other '''exponential roots''' the operator can be used with fractions such as (1 / 3) designating the cube root of a number. | There is also an operator for '''exponential''' calculations. The exponential operator is used to raise a number's value to a designated exponent of itself. In QB the exponential return values are [[DOUBLE]] values. The [[SQR]] function can return a number's Square Root. For other '''exponential roots''' the operator can be used with fractions such as (1 / 3) designating the cube root of a number. | ||
Line 82: | Line 81: | ||
!Symbol | !Symbol | ||
!Procedure | !Procedure | ||
!Example Usage | !Example Usage | ||
!Operation Order | !Operation Order | ||
|- | |- | ||
| align="center" |[[^]] || Exponent || align="center" | c = a [[^]] (1 / 2) || align="center" | First | | align="center" |[[^]] || Exponent || align="center" | c = a [[^]] (1 / 2) || align="center" | First | ||
|- | |- | ||
| align="center" | [[SQR]] || Square Root || align="center" | c = [[SQR]](a [[^]] 2 + b [[^]] 2) || align="center" | First | | align="center" | [[SQR]] || Square Root || align="center" | c = [[SQR]](a [[^]] 2 + b [[^]] 2) || align="center" | First | ||
|} | |} | ||
=== Notes === | |||
===Notes=== | |||
* Exponent fractions should be enclosed in () brackets in order to be treated as a root rather than as division. | * Exponent fractions should be enclosed in () brackets in order to be treated as a root rather than as division. | ||
* Negative exponential values must be enclosed in () brackets in QB64. | * Negative exponential values must be enclosed in () brackets in QB64. | ||
Line 98: | Line 96: | ||
<center>[[#toc|Return to Top]]</center> | <center>[[#toc|Return to Top]]</center> | ||
==Basic's Order of Operations== | |||
== Basic's Order of Operations == | |||
When a normal calculation is made, BASIC works from left to right, but it does certain calculations in the following order: | When a normal calculation is made, BASIC works from left to right, but it does certain calculations in the following order: | ||
Line 113: | Line 112: | ||
Sometimes a calculation may need BASIC to do them in another order or the calculation will return bad results. BASIC allows the programmer to decide the order of operations by using [[parenthesis]] around parts of the equation. BASIC will do the calculations inside of the [[parenthesis]] brackets first and the others from left to right in the normal operation order. | Sometimes a calculation may need BASIC to do them in another order or the calculation will return bad results. BASIC allows the programmer to decide the order of operations by using [[parenthesis]] around parts of the equation. BASIC will do the calculations inside of the [[parenthesis]] brackets first and the others from left to right in the normal operation order. | ||
==Basic's Mathematical Functions== | |||
== Basic's Mathematical Functions == | |||
{| align=center border=1 | {| align=center border=1 | ||
Line 120: | Line 120: | ||
|- | |- | ||
| [[ABS]](n) || returns the absolute (positive) value of n: ABS(-5) = 5 | | [[ABS]](n) || returns the absolute (positive) value of n: ABS(-5) = 5 | ||
|- | |- | ||
| [[ATN]](angle*) || returns the arctangent of an angle in radians: π = 4 * ATN(1) | | [[ATN]](angle*) || returns the arctangent of an angle in radians: π = 4 * ATN(1) | ||
|- | |- | ||
| [[COS]](angle*) || returns the cosine of an angle in radians. (horizontal component) | | [[COS]](angle*) || returns the cosine of an angle in radians. (horizontal component) | ||
|- | |- | ||
| [[EXP]](n) || returns e | | [[EXP]](n) || returns e ^ x, '''(n <= 88.02969)''': e = EXP(1) ' (e = 2.718281828459045) | ||
|- | |- | ||
| [[LOG]](n) || returns the base e natural logarithm of n. '''(n > 0)''' | | [[LOG]](n) || returns the base e natural logarithm of n. '''(n > 0)''' | ||
Line 138: | Line 138: | ||
|} | |} | ||
<center> '''* angles measured in radians'''</center> | <center>'''* angles measured in radians'''</center> | ||
Line 151: | Line 151: | ||
'''Logarithm to base n''' | '''Logarithm to base n''' | ||
FUNCTION LOGN (X, n) | FUNCTION LOGN (X, n) | ||
IF n > 0 AND n <> 1 AND X > 0 THEN LOGN = {{Cb|LOG}}(X) / {{Cb|LOG}}(n) ELSE BEEP | IF n > 0 AND n <> 1 AND X > 0 THEN LOGN = {{Cb|LOG}}(X) / {{Cb|LOG}}(n) ELSE BEEP | ||
END FUNCTION | END FUNCTION | ||
Line 157: | Line 157: | ||
FUNCTION LOG10 (X) 'base 10 logarithm | FUNCTION LOG10 (X) 'base 10 logarithm | ||
IF X > 0 THEN LOG10 = {{Cb|LOG}}(X) / {{Cb|LOG}}(10) ELSE BEEP | IF X > 0 THEN LOG10 = {{Cb|LOG}}(X) / {{Cb|LOG}}(10) ELSE BEEP | ||
END FUNCTION | END FUNCTION | ||
{{TextEnd}} | {{TextEnd}} | ||
Line 170: | Line 170: | ||
<center>[[#toc|Return to Top]]</center> | <center>[[#toc|Return to Top]]</center> | ||
== Derived Mathematical Functions == | |||
The following Trigonometric functions can be derived from the '''BASIC Mathematical Functions''' listed above. Each function checks that certain values can be used without error or a [[BEEP]] will notify the user that a value could not be returned. An error handling routine can be substituted if desired. '''Note:''' Functions requiring '''π''' use 4 * [[ATN]](1) for [[SINGLE]] accuracy. Use [[ATN]](1.#) for [[DOUBLE]] accuracy. | The following Trigonometric functions can be derived from the '''BASIC Mathematical Functions''' listed above. Each function checks that certain values can be used without error or a [[BEEP]] will notify the user that a value could not be returned. An error handling routine can be substituted if desired. '''Note:''' Functions requiring '''π''' use 4 * [[ATN]](1) for [[SINGLE]] accuracy. Use [[ATN]](1.#) for [[DOUBLE]] accuracy. | ||
{{CodeStart}} | |||
{{Cl|FUNCTION}} {{Text|SEC|#55FF55}} (x) {{Text|<nowiki>'Secant</nowiki>|#919191}} | |||
{{Cl|IF}} {{Cl|COS}}(x) <> {{Text|0|#F580B1}} {{Cl|THEN}} {{Text|SEC|#55FF55}} = {{Text|1|#F580B1}} / {{Cl|COS}}(x) {{Cl|ELSE}} {{Cl|BEEP}} | |||
{{Cl|END FUNCTION}} | |||
{{ | {{Cl|FUNCTION}} {{Text|CSC|#55FF55}} (x) {{Text|<nowiki>'CoSecant</nowiki>|#919191}} | ||
{{Cl|IF}} {{Cl|SIN}}(x) <> {{Text|0|#F580B1}} {{Cl|THEN}} {{Text|CSC|#55FF55}} = {{Text|1|#F580B1}} / {{Cl|SIN}}(x) {{Cl|ELSE}} {{Cl|BEEP}} | |||
IF | {{Cl|END FUNCTION}} | ||
END FUNCTION | |||
FUNCTION | {{Cl|FUNCTION}} {{Text|COT|#55FF55}} (x) {{Text|<nowiki>'CoTangent</nowiki>|#919191}} | ||
IF | {{Cl|IF}} {{Cl|TAN}}(x) <> {{Text|0|#F580B1}} {{Cl|THEN}} {{Text|COT|#55FF55}} = {{Text|1|#F580B1}} / {{Cl|TAN}}(x) {{Cl|ELSE}} {{Cl|BEEP}} | ||
END FUNCTION | {{Cl|END FUNCTION}} | ||
FUNCTION | {{Cl|FUNCTION}} {{Text|ARCSIN|#55FF55}} (x) {{Text|<nowiki>'Inverse Sine</nowiki>|#919191}} | ||
IF | {{Cl|IF}} x < {{Text|1|#F580B1}} {{Cl|THEN}} {{Text|ARCSIN|#55FF55}} = {{Cl|ATN}}(x / {{Cl|SQR}}({{Text|1|#F580B1}} - (x * x))) {{Cl|ELSE}} {{Cl|BEEP}} | ||
END FUNCTION | {{Cl|END FUNCTION}} | ||
FUNCTION | {{Cl|FUNCTION}} {{Text|ARCCOS|#55FF55}} (x) {{Text|<nowiki>' Inverse Cosine</nowiki>|#919191}} | ||
IF x < 1 THEN | {{Cl|IF}} x < {{Text|1|#F580B1}} {{Cl|THEN}} {{Text|ARCCOS|#55FF55}} = ({{Text|2|#F580B1}} * {{Cl|ATN}}({{Text|1|#F580B1}})) - {{Cl|ATN}}(x / {{Cl|SQR}}({{Text|1|#F580B1}} - x * x)) {{Cl|ELSE}} {{Cl|BEEP}} | ||
END FUNCTION | {{Cl|END FUNCTION}} | ||
FUNCTION | {{Cl|FUNCTION}} {{Text|ARCSEC|#55FF55}} (x) {{Text|<nowiki>' Inverse Secant</nowiki>|#919191}} | ||
IF x < 1 THEN | {{Cl|IF}} x < {{Text|1|#F580B1}} {{Cl|THEN}} {{Text|ARCSEC|#55FF55}} = {{Cl|ATN}}(x / {{Cl|SQR}}({{Text|1|#F580B1}} - x * x)) + ({{Cl|SGN}}(x) - {{Text|1|#F580B1}}) * ({{Text|2|#F580B1}} * {{Cl|ATN}}({{Text|1|#F580B1}})) {{Cl|ELSE}} {{Cl|BEEP}} | ||
END FUNCTION | {{Cl|END FUNCTION}} | ||
FUNCTION | {{Cl|FUNCTION}} {{Text|ARCCSC|#55FF55}} (x) {{Text|<nowiki>' Inverse CoSecant</nowiki>|#919191}} | ||
IF x < 1 THEN | {{Cl|IF}} x < {{Text|1|#F580B1}} {{Cl|THEN}} {{Text|ARCCSC|#55FF55}} = {{Cl|ATN}}({{Text|1|#F580B1}} / {{Cl|SQR}}({{Text|1|#F580B1}} - x * x)) + ({{Cl|SGN}}(x) - {{Text|1|#F580B1}}) * ({{Text|2|#F580B1}} * {{Cl|ATN}}({{Text|1|#F580B1}})) {{Cl|ELSE}} {{Cl|BEEP}} | ||
END FUNCTION | {{Cl|END FUNCTION}} | ||
FUNCTION | {{Cl|FUNCTION}} {{Text|ARCCOT|#55FF55}} (x) {{Text|<nowiki>' Inverse CoTangent</nowiki>|#919191}} | ||
{{Text|ARCCOT|#55FF55}} = ({{Text|2|#F580B1}} * {{Cl|ATN}}({{Text|1|#F580B1}})) - {{Cl|ATN}}(x) | |||
{{Cl|END FUNCTION}} | |||
ARCCOT = (2 * {{ | |||
END FUNCTION | |||
FUNCTION SINH (x) | {{Cl|FUNCTION}} {{Text|SINH|#55FF55}} (x) {{Text|<nowiki>' Hyperbolic Sine</nowiki>|#919191}} | ||
IF x <= 88.02969 THEN SINH = ({{ | {{Cl|IF}} x <= {{Text|88.02969|#F580B1}} {{Cl|THEN}} {{Text|SINH|#55FF55}} = ({{Cl|EXP}}(x) - {{Cl|EXP}}(-x)) / {{Text|2|#F580B1}} {{Cl|ELSE}} {{Cl|BEEP}} | ||
END FUNCTION | {{Cl|END FUNCTION}} | ||
FUNCTION COSH (x) | {{Cl|FUNCTION}} {{Text|COSH|#55FF55}} (x) {{Text|<nowiki>' Hyperbolic CoSine</nowiki>|#919191}} | ||
IF x <= 88.02969 THEN COSH = (EXP(x) + EXP(-x)) / 2 ELSE BEEP | {{Cl|IF}} x <= {{Text|88.02969|#F580B1}} {{Cl|THEN}} {{Text|COSH|#55FF55}} = ({{Cl|EXP}}(x) + {{Cl|EXP}}(-x)) / {{Text|2|#F580B1}} {{Cl|ELSE}} {{Cl|BEEP}} | ||
END FUNCTION | {{Cl|END FUNCTION}} | ||
FUNCTION TANH (x) | {{Cl|FUNCTION}} {{Text|TANH|#55FF55}} (x) {{Text|<nowiki>' Hyperbolic Tangent or SINH(x) / COSH(x)</nowiki>|#919191}} | ||
IF 2 * x <= 88.02969 AND EXP(2 * x) + 1 <> 0 THEN | {{Cl|IF}} {{Text|2|#F580B1}} * x <= {{Text|88.02969|#F580B1}} {{Cl|AND (boolean)|AND}} {{Cl|EXP}}({{Text|2|#F580B1}} * x) + {{Text|1|#F580B1}} <> {{Text|0|#F580B1}} {{Cl|THEN}} | ||
{{Text|TANH|#55FF55}} = ({{Cl|EXP}}({{Text|2|#F580B1}} * x) - {{Text|1|#F580B1}}) / ({{Cl|EXP}}({{Text|2|#F580B1}} * x) + {{Text|1|#F580B1}}) | |||
ELSE BEEP | {{Cl|ELSE}} {{Cl|BEEP}} | ||
END IF | {{Cl|END IF}} | ||
END FUNCTION | {{Cl|END FUNCTION}} | ||
FUNCTION SECH (x) | {{Cl|FUNCTION}} {{Text|SECH|#55FF55}} (x) {{Text|<nowiki>' Hyperbolic Secant or (COSH(x)) ^ -1</nowiki>|#919191}} | ||
IF x <= 88.02969 AND (EXP(x) + EXP(-x)) <> 0 THEN SECH = 2 / ({{ | {{Cl|IF}} x <= {{Text|88.02969|#F580B1}} {{Cl|AND (boolean)|AND}} ({{Cl|EXP}}(x) + {{Cl|EXP}}(-x)) <> {{Text|0|#F580B1}} {{Cl|THEN}} {{Text|SECH|#55FF55}} = {{Text|2|#F580B1}} / ({{Cl|EXP}}(x) + {{Cl|EXP}}(-x)) {{Cl|ELSE}} {{Cl|BEEP}} | ||
END FUNCTION | {{Cl|END FUNCTION}} | ||
FUNCTION CSCH (x) | {{Cl|FUNCTION}} {{Text|CSCH|#55FF55}} (x) {{Text|<nowiki>' Hyperbolic CoSecant or (SINH(x)) ^ -1</nowiki>|#919191}} | ||
IF x <= 88.02969 AND (EXP(x) - EXP(-x)) <> 0 THEN CSCH = 2 / ({{ | {{Cl|IF}} x <= {{Text|88.02969|#F580B1}} {{Cl|AND (boolean)|AND}} ({{Cl|EXP}}(x) - {{Cl|EXP}}(-x)) <> {{Text|0|#F580B1}} {{Cl|THEN}} {{Text|CSCH|#55FF55}} = {{Text|2|#F580B1}} / ({{Cl|EXP}}(x) - {{Cl|EXP}}(-x)) {{Cl|ELSE}} {{Cl|BEEP}} | ||
END FUNCTION | {{Cl|END FUNCTION}} | ||
FUNCTION COTH (x) | {{Cl|FUNCTION}} {{Text|COTH|#55FF55}} (x) {{Text|<nowiki>' Hyperbolic CoTangent or COSH(x) / SINH(x)</nowiki>|#919191}} | ||
IF 2 * x <= 88.02969 AND EXP(2 * x) - 1 <> 0 THEN | {{Cl|IF}} {{Text|2|#F580B1}} * x <= {{Text|88.02969|#F580B1}} {{Cl|AND (boolean)|AND}} {{Cl|EXP}}({{Text|2|#F580B1}} * x) - {{Text|1|#F580B1}} <> {{Text|0|#F580B1}} {{Cl|THEN}} | ||
{{Text|COTH|#55FF55}} = ({{Cl|EXP}}({{Text|2|#F580B1}} * x) + {{Text|1|#F580B1}}) / ({{Cl|EXP}}({{Text|2|#F580B1}} * x) - {{Text|1|#F580B1}}) | |||
ELSE BEEP | {{Cl|ELSE}} {{Cl|BEEP}} | ||
END IF | {{Cl|END IF}} | ||
END FUNCTION | {{Cl|END FUNCTION}} | ||
FUNCTION ARCSINH (x) | {{Cl|FUNCTION}} {{Text|ARCSINH|#55FF55}} (x) {{Text|<nowiki>' Inverse Hyperbolic Sine</nowiki>|#919191}} | ||
IF (x * x) + 1 >= 0 AND x + SQR((x * x) + 1) > 0 THEN | {{Cl|IF}} (x * x) + {{Text|1|#F580B1}} >= {{Text|0|#F580B1}} {{Cl|AND (boolean)|AND}} x + {{Cl|SQR}}((x * x) + {{Text|1|#F580B1}}) > {{Text|0|#F580B1}} {{Cl|THEN}} | ||
ARCSINH = {{ | {{Text|ARCSINH|#55FF55}} = {{Cl|LOG}}(x + {{Cl|SQR}}(x * x + {{Text|1|#F580B1}})) | ||
ELSE BEEP | {{Cl|ELSE}} {{Cl|BEEP}} | ||
END IF | {{Cl|END IF}} | ||
END FUNCTION | {{Cl|END FUNCTION}} | ||
FUNCTION ARCCOSH (x) | {{Cl|FUNCTION}} {{Text|ARCCOSH|#55FF55}} (x) {{Text|<nowiki>' Inverse Hyperbolic CoSine</nowiki>|#919191}} | ||
IF x >= 1 AND x * x - 1 >= 0 AND x + SQR(x * x - 1) > 0 THEN | {{Cl|IF}} x >= {{Text|1|#F580B1}} {{Cl|AND (boolean)|AND}} x * x - {{Text|1|#F580B1}} >= {{Text|0|#F580B1}} {{Cl|AND (boolean)|AND}} x + {{Cl|SQR}}(x * x - {{Text|1|#F580B1}}) > {{Text|0|#F580B1}} {{Cl|THEN}} | ||
ARCCOSH = {{ | {{Text|ARCCOSH|#55FF55}} = {{Cl|LOG}}(x + {{Cl|SQR}}(x * x - {{Text|1|#F580B1}})) | ||
ELSE BEEP | {{Cl|ELSE}} {{Cl|BEEP}} | ||
END IF | {{Cl|END IF}} | ||
END FUNCTION | {{Cl|END FUNCTION}} | ||
FUNCTION ARCTANH (x) | {{Cl|FUNCTION}} {{Text|ARCTANH|#55FF55}} (x) {{Text|<nowiki>' Inverse Hyperbolic Tangent</nowiki>|#919191}} | ||
IF x < 1 THEN ARCTANH = {{ | {{Cl|IF}} x < {{Text|1|#F580B1}} {{Cl|THEN}} {{Text|ARCTANH|#55FF55}} = {{Cl|LOG}}(({{Text|1|#F580B1}} + x) / ({{Text|1|#F580B1}} - x)) / {{Text|2|#F580B1}} {{Cl|ELSE}} {{Cl|BEEP}} | ||
END FUNCTION | {{Cl|END FUNCTION}} | ||
FUNCTION ARCSECH (x) | {{Cl|FUNCTION}} {{Text|ARCSECH|#55FF55}} (x) {{Text|<nowiki>' Inverse Hyperbolic Secant</nowiki>|#919191}} | ||
IF x > 0 AND x <= 1 THEN ARCSECH = {{ | {{Cl|IF}} x > {{Text|0|#F580B1}} {{Cl|AND (boolean)|AND}} x <= {{Text|1|#F580B1}} {{Cl|THEN}} {{Text|ARCSECH|#55FF55}} = {{Cl|LOG}}(({{Cl|SGN}}(x) * {{Cl|SQR}}({{Text|1|#F580B1}} - x * x) + {{Text|1|#F580B1}}) / x) {{Cl|ELSE}} {{Cl|BEEP}} | ||
END FUNCTION | {{Cl|END FUNCTION}} | ||
FUNCTION ARCCSCH (x) | {{Cl|FUNCTION}} {{Text|ARCCSCH|#55FF55}} (x) {{Text|<nowiki>' Inverse Hyperbolic CoSecant</nowiki>|#919191}} | ||
IF x <> 0 AND x * x + 1 >= 0 AND (SGN(x) * SQR(x * x + 1) + 1) / x > 0 THEN | {{Cl|IF}} x <> {{Text|0|#F580B1}} {{Cl|AND (boolean)|AND}} x * x + {{Text|1|#F580B1}} >= {{Text|0|#F580B1}} {{Cl|AND (boolean)|AND}} ({{Cl|SGN}}(x) * {{Cl|SQR}}(x * x + {{Text|1|#F580B1}}) + {{Text|1|#F580B1}}) / x > {{Text|0|#F580B1}} {{Cl|THEN}} | ||
{{Text|ARCCSCH|#55FF55}} = {{Cl|LOG}}(({{Cl|SGN}}(x) * {{Cl|SQR}}(x * x + {{Text|1|#F580B1}}) + {{Text|1|#F580B1}}) / x) | |||
ELSE BEEP | {{Cl|ELSE}} {{Cl|BEEP}} | ||
END IF | {{Cl|END IF}} | ||
END FUNCTION | {{Cl|END FUNCTION}} | ||
FUNCTION ARCCOTH (x) | {{Cl|FUNCTION}} {{Text|ARCCOTH|#55FF55}} (x) {{Text|<nowiki>' Inverse Hyperbolic CoTangent</nowiki>|#919191}} | ||
IF x > 1 THEN ARCCOTH = {{ | {{Cl|IF}} x > {{Text|1|#F580B1}} {{Cl|THEN}} {{Text|ARCCOTH|#55FF55}} = {{Cl|LOG}}((x + {{Text|1|#F580B1}}) / (x - {{Text|1|#F580B1}})) / {{Text|2|#F580B1}} {{Cl|ELSE}} {{Cl|BEEP}} | ||
END FUNCTION | {{Cl|END FUNCTION}} | ||
{{ | {{CodeEnd}} | ||
{{ | {{FixedStart}} | ||
'''Hyperbolic Function Relationships:''' | '''Hyperbolic Function Relationships:''' | ||
COSH(-x) = COSH(x) | COSH(-x) = COSH(x) | ||
SINH(-x) = -SINH(x) | SINH(-x) = -SINH(x) | ||
SECH(-x) = SECH(x) | SECH(-x) = SECH(x) | ||
CSCH(-x) = -CSCH(x) | CSCH(-x) = -CSCH(x) | ||
Line 285: | Line 284: | ||
'''Inverse Hyperbolic Function Relatonships:''' | '''Inverse Hyperbolic Function Relatonships:''' | ||
ARSECH(x) = ARCOSH(x) ^ -1 | ARSECH(x) = ARCOSH(x) ^ -1 | ||
ARCSCH(x) = ARSINH(x) ^ -1 | ARCSCH(x) = ARSINH(x) ^ -1 | ||
ARCOTH(x) = ARTANH(x) ^ -1 | ARCOTH(x) = ARTANH(x) ^ -1 | ||
'''Hyperbolic sine and cosine satisfy the Pythagorean trig. identity:''' | '''Hyperbolic sine and cosine satisfy the Pythagorean trig. identity:''' | ||
Line 293: | Line 292: | ||
(COSH(x) ^ 2) - (SINH(x) ^ 2) = 1 | (COSH(x) ^ 2) - (SINH(x) ^ 2) = 1 | ||
{{ | {{FixedEnd}} | ||
<center>[http://support.microsoft.com/kb/28249 Microsoft's Derived BASIC Functions (KB 28249)]</center> | <center>[http://support.microsoft.com/kb/28249 Microsoft's Derived BASIC Functions (KB 28249)]</center> | ||
Line 300: | Line 299: | ||
<center>[[#toc|Return to Top]]</center> | <center>[[#toc|Return to Top]]</center> | ||
==Mathematical Logical Operators== | |||
== Mathematical Logical Operators == | |||
The following logical operators compare numerical values using bitwise operations. The two numbers are compared by the number's [[Binary]] bits on and the result of the operation determines the value returned in decimal form. [[NOT]] checks one value and returns the opposite. It returns 0 if a value is not 0 and -1 if it is 0. See [[Binary]] for more on bitwise operations. | The following logical operators compare numerical values using bitwise operations. The two numbers are compared by the number's [[Binary]] bits on and the result of the operation determines the value returned in decimal form. [[NOT]] checks one value and returns the opposite. It returns 0 if a value is not 0 and -1 if it is 0. See [[Binary]] for more on bitwise operations. | ||
Line 308: | Line 308: | ||
{{ | {{LogicalTruthPlugin}} | ||
<center>BASIC can accept any + or - value that is not 0 to be True when used in an evaluation.</center> | <center>BASIC can accept any + or - value that is not 0 to be True when used in an evaluation.</center> | ||
Line 315: | Line 315: | ||
<center>[[#toc|Return to Top]]</center> | <center>[[#toc|Return to Top]]</center> | ||
==Relational | |||
== Relational Operations == | |||
Relational Operations are used to compare values in a Conditional [[IF...THEN]], [[SELECT CASE]], [[UNTIL]] or [[WHILE]] statement. | Relational Operations are used to compare values in a Conditional [[IF...THEN]], [[SELECT CASE]], [[UNTIL]] or [[WHILE]] statement. | ||
{{RelationalOperationsPlugin}} | |||
<center>[[#toc|Return to Top]]</center> | |||
== Basic's Rounding Functions == | |||
==Basic's Rounding Functions== | |||
: Rounding is used when the program needs a certain number value or type. There are 4 [[INTEGER]] or [[LONG]] Integer functions and one function each for closest [[SINGLE]] and closest [[DOUBLE]] numerical types. Closest functions use "bankers" rounding which rounds up if the decimal point value is over one half. Variable types should match the return value. | : Rounding is used when the program needs a certain number value or type. There are 4 [[INTEGER]] or [[LONG]] Integer functions and one function each for closest [[SINGLE]] and closest [[DOUBLE]] numerical types. Closest functions use "bankers" rounding which rounds up if the decimal point value is over one half. Variable types should match the return value. | ||
Line 348: | Line 348: | ||
|} | |} | ||
=== | === Notes === | ||
* Each of the above functions define the value's type in addition to rounding the values. | * Each of the above functions define the value's type in addition to rounding the values. | ||
<center>[[#toc|Return to Top]]</center> | <center>[[#toc|Return to Top]]</center> | ||
==Base Number Systems== | |||
== Base Number Systems == | |||
Line 361: | Line 362: | ||
'''Decimal (base 10) Binary (base 2) Hexadecimal (base 16) Octal (base 8)''' | '''Decimal (base 10) Binary (base 2) Hexadecimal (base 16) Octal (base 8)''' | ||
''' [[&B]] [[&H]] [[HEX$]](n) [[&O]] [[OCT$]](n)''' | ''' [[&B]] [[&H]] [[HEX$]](n) [[&O]] [[OCT$]](n)''' | ||
0 0000 0 0 | 0 0000 0 0 | ||
Line 380: | Line 381: | ||
15 ------------- 1111 <--- Match ---> F ---------------- 17 -- max 2 | 15 ------------- 1111 <--- Match ---> F ---------------- 17 -- max 2 | ||
16 10000 10 20 | 16 10000 10 20 | ||
When the Decimal value is 15, the other 2 base systems are all maxed out! | When the Decimal value is 15, the other 2 base systems are all maxed out! | ||
The Binary values can be compared to all of the HEX value digit values so | The Binary values can be compared to all of the HEX value digit values so | ||
Line 386: | Line 387: | ||
value to Binary just add the 4 binary digits for each HEX digit place so: | value to Binary just add the 4 binary digits for each HEX digit place so: | ||
F A C E | F A C E | ||
&HFACE = 1111 + 1010 + 1100 + 1101 = &B1111101011001101 | &HFACE = 1111 + 1010 + 1100 + 1101 = &B1111101011001101 | ||
Line 392: | Line 393: | ||
sections of four digits starting from the right(LSB) end. If one has less | sections of four digits starting from the right(LSB) end. If one has less | ||
than 4 digits on the left end you could add the leading zeros like below: | than 4 digits on the left end you could add the leading zeros like below: | ||
&B101011100010001001 = 0010 1011 1000 1000 1001 | &B101011100010001001 = 0010 1011 1000 1000 1001 | ||
hexadecimal = 2 + B + 8 + 8 + 9 = &H2B889 | hexadecimal = 2 + B + 8 + 8 + 9 = &H2B889 | ||
See the Decimal to Binary conversion function that uses '''[[HEX$]]''' on the '''[[&H]]''' page. | See the Decimal to Binary conversion function that uses '''[[HEX$]]''' on the '''[[&H]]''' page. | ||
{{TextEnd}} | {{TextEnd}} | ||
Line 418: | Line 419: | ||
<center>[[#toc|Return to Top]]</center> | <center>[[#toc|Return to Top]]</center> | ||
==Bits and Bytes== | |||
== Bits and Bytes == | |||
<center>'''[[_BIT|BITS]]'''</center> | <center>'''[[_BIT|BITS]]'''</center> | ||
Line 425: | Line 427: | ||
:* '''"Big-endian"''': MSB is the first bit encountered, decreasing to the LSB as the last bit by position, memory address or time. | :* '''"Big-endian"''': MSB is the first bit encountered, decreasing to the LSB as the last bit by position, memory address or time. | ||
:* '''"Little-endian"''': LSB is the first bit encountered, increasing to the MSB as the last bit by position, memory address or time. | :* '''"Little-endian"''': LSB is the first bit encountered, increasing to the MSB as the last bit by position, memory address or time. | ||
{{ | {{FixedStart}} | ||
'''Offset or Position: 0 1 2 3 4 5 6 7 Example: 11110000''' | '''Offset or Position: 0 1 2 3 4 5 6 7 Example: 11110000''' | ||
---------------------------------- -------- | ---------------------------------- -------- | ||
'''Big-Endian Bit On Value:''' 128 64 32 16 8 4 2 1 240 | '''Big-Endian Bit On Value:''' 128 64 32 16 8 4 2 1 240 | ||
'''Little-Endian Bit On Value:''' 1 2 4 8 16 32 64 128 15 | '''Little-Endian Bit On Value:''' 1 2 4 8 16 32 64 128 15 | ||
{{ | {{FixedEnd}} | ||
::The big-endian method compares exponents of 2 | ::The big-endian method compares exponents of 2 ^ 7 down to 2 ^ 0 while the little-endian method does the opposite. | ||
<center>'''[[_BYTE|BYTES]]'''</center> | <center>'''[[_BYTE|BYTES]]'''</center> | ||
* [[INTEGER]] values consist of 2 bytes called the '''HI''' and '''LO''' bytes. Anytime that the number of binary digits is a multiple of 16 (2bytes, 4 bytes, etc.) and the HI byte's MSB is on(1), the value returned will be negative, even with [[SINGLE]] or [[DOUBLE]] values. | * [[INTEGER]] values consist of 2 bytes called the '''HI''' and '''LO''' bytes. Anytime that the number of binary digits is a multiple of 16 (2bytes, 4 bytes, etc.) and the HI byte's MSB is on(1), the value returned will be negative, even with [[SINGLE]] or [[DOUBLE]] values. | ||
{{ | {{FixedStart}} '''16 BIT INTEGER OR REGISTER''' | ||
'''AH (High Byte Bits) AL (Low Byte Bits)''' | '''AH (High Byte Bits) AL (Low Byte Bits)''' | ||
BIT: 15 14 13 12 11 10 9 8 | 7 6 5 4 3 2 1 0 | BIT: 15 14 13 12 11 10 9 8 | 7 6 5 4 3 2 1 0 | ||
---------------------------------------|-------------------------------------- | ---------------------------------------|-------------------------------------- | ||
HEX: 8000 4000 2000 1000 800 400 200 100 | 80 40 20 10 8 4 2 1 | HEX: 8000 4000 2000 1000 800 400 200 100 | 80 40 20 10 8 4 2 1 | ||
| | |||
DEC: -32768 16384 8192 4096 2048 1024 512 256 | 128 64 32 16 8 4 2 1 | DEC: -32768 16384 8192 4096 2048 1024 512 256 | 128 64 32 16 8 4 2 1 | ||
{{ | {{FixedEnd}} | ||
::The HI byte's '''MSB''' is often called the '''sign''' bit! When the highest bit is on, the signed value returned will be negative. | ::The HI byte's '''MSB''' is often called the '''sign''' bit! When the highest bit is on, the signed value returned will be negative. | ||
''Example:'' Program displays the bits on for any integer value between -32768 and 32767 or &H80000 and &H7FFF. | ''Example:'' Program displays the bits on for any integer value between -32768 and 32767 or &H80000 and &H7FFF. | ||
{{CodeStart}} | {{CodeStart}} | ||
{{Cl|DEFINT}} A-Z | {{Cl|DEFINT}} A-Z | ||
{{Cl | {{Cl|SCREEN}} 12 | ||
{{Cl|COLOR}} 11: {{Cl|LOCATE}} 10, 2 | {{Cl|COLOR}} 11: {{Cl|LOCATE}} 10, 2 | ||
{{Cl|PRINT}} " AH (High Register Byte Bits) AL (Low Register Byte Bits)" | {{Cl|PRINT}} " AH (High Register Byte Bits) AL (Low Register Byte Bits)" | ||
Line 467: | Line 469: | ||
{{Cl|IF}} (Num {{Cl|AND}} 2 ^ i) {{Cl|THEN}} | {{Cl|IF}} (Num {{Cl|AND}} 2 ^ i) {{Cl|THEN}} | ||
{{Cl|PAINT}} (640 - (37 * (i + 1)), 189), 12, 9 | {{Cl|PAINT}} (640 - (37 * (i + 1)), 189), 12, 9 | ||
BinStr$ = BinStr$ + "1" | |||
{{Cl|ELSE}} | {{Cl|ELSE}} | ||
{{Cl|PAINT}} (640 - (37 * (i + 1)), 189), 0, 9 | {{Cl|PAINT}} (640 - (37 * (i + 1)), 189), 0, 9 | ||
BinStr$ = BinStr$ + "0" | |||
{{Cl|END IF}} | {{Cl|END IF}} | ||
{{Cl|NEXT}} | {{Cl|NEXT}} | ||
{{Cl|COLOR}} 10: {{Cl|LOCATE}} 16, 50: {{Cl|PRINT}} "Binary ="; {{Cl|VAL}}( | {{Cl|COLOR}} 10: {{Cl|LOCATE}} 16, 50: {{Cl|PRINT}} "Binary ="; {{Cl|VAL}}(BinStr$) | ||
{{Cl|COLOR}} 9: {{Cl|LOCATE}} 16, 10: {{Cl|PRINT}} "Decimal ="; Num;: {{Cl|COLOR}} 13: {{Cl|PRINT}} " Hex = "; Hexa$ | {{Cl|COLOR}} 9: {{Cl|LOCATE}} 16, 10: {{Cl|PRINT}} "Decimal ="; Num;: {{Cl|COLOR}} 13: {{Cl|PRINT}} " Hex = "; Hexa$ | ||
Hexa$ = "": | Hexa$ = "": BinStr$ = "" | ||
{{Cl|END IF}} | {{Cl|END IF}} | ||
{{Cl|COLOR}} 14: {{Cl|LOCATE}} 17, 15: {{Cl|INPUT}} "Enter a decimal or HEX({{Cl|&H}}) value (0 Quits): ", frst$ | {{Cl|COLOR}} 14: {{Cl|LOCATE}} 17, 15: {{Cl|INPUT}} "Enter a decimal or HEX({{Cl|&H}}) value (0 Quits): ", frst$ | ||
first = {{Cl|VAL}}(frst$) | first = {{Cl|VAL}}(frst$) | ||
{{Cl|IF}} first {{Cl|THEN}} | {{Cl|IF}} first {{Cl|THEN}} | ||
{{Cl|LOCATE}} 17, 15: {{Cl|PRINT}} {{Cl|SPACE$}}(55) | {{Cl|LOCATE}} 17, 15: {{Cl|PRINT}} {{Cl|SPACE$}}(55) | ||
Line 490: | Line 492: | ||
{{Cl|COLOR}} 11: {{Cl|LOCATE}} 28, 30: {{Cl|PRINT}} "Press any key to exit!"; | {{Cl|COLOR}} 11: {{Cl|LOCATE}} 28, 30: {{Cl|PRINT}} "Press any key to exit!"; | ||
{{Cl|SLEEP}} | {{Cl|SLEEP}} | ||
{{Cl|SYSTEM}} | {{Cl|SYSTEM}} | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{ | {{Small|Code by Ted Weissgerber}} | ||
<center>[[#toc|Return to Top]]</center> | <center>[[#toc|Return to Top]]</center> | ||
==OFFSET== | |||
== OFFSET == | |||
* [[_OFFSET (function)]] returns the memory offset position as a flexible sized value for a designated variable. See [[Using _OFFSET]]. | * [[_OFFSET (function)]] returns the memory offset position as a flexible sized value for a designated variable. See [[Using _OFFSET]]. | ||
Line 505: | Line 508: | ||
<center>'''[[_OFFSET]] values can only be used in conjunction with [[_MEM]]ory and [[DECLARE | <center>'''[[_OFFSET]] values can only be used in conjunction with [[_MEM]]ory and [[DECLARE LIBRARY]] procedures.'''</center> | ||
{{PageSeeAlso}} | |||
* [[_OFFSET]], [[_MEM]] | * [[_OFFSET]], [[_MEM]] | ||
* [[DIM]], [[_DEFINE]] | * [[DIM]], [[_DEFINE]] | ||
* [[TYPE]] | * [[TYPE]] | ||
{{ | |||
{{PageReferences}} |
Latest revision as of 13:58, 20 November 2024
Basic and QB64 Numerical Types
- INTEGER [%]: 2 Byte signed whole number values from -32768 to 32767. 0 to 65535 unsigned. (not checked in QB64)
- LONG [&]: 4 byte signed whole number values from -2147483648 to 2147483647. 0 to 4294967295 unsigned.
- SINGLE [!]: 4 byte signed floating decimal point values of up to 7 decimal place accuracy. Cannot be unsigned.
- DOUBLE [#]: 8 byte signed floating decimal point values of up to 15 decimal place accuracy. Cannot be unsigned.
- To get one byte values, can use an ASCII STRING character to represent values from 0 to 255 as in BINARY files.
- _BIT [`]: 1 bit signed whole number values of 0 or -1 signed or 0 or 1 unsigned. _BIT * 8 can hold a signed or unsigned byte value.
- _BYTE [%%]: 1 byte signed whole number values from -128 to 127. Unsigned values from 0 to 255.
- _INTEGER64 [&&]: 8 byte signed whole number values from -9223372036854775808 to 9223372036854775807
- _FLOAT [##]: currently set as 10 byte signed floating decimal point values up to +/-1.189731495357231765F+4932. Cannot be unsigned.
- _OFFSET [%&]: undefined flexable length integer offset values used in DECLARE LIBRARY declarations.
Negative (signed) numerical values can affect calculations when using any of the BASIC operators. SQR cannot use negative values! There may be times that a calculation error is made using those negative values. The SGN function returns the sign of a value as -1 for negative, 0 for zero and 1 for unsigned positive values. ABS always returns an unsigned value.
_UNSIGNED integer, byte and bit variable values can use the tilde ~ suffix before the type suffix to define the type.
Mathematical Operation Symbols
Most of the BASIC math operators are ones that require no introduction. The addition, subtraction, multplication and division operators are ones commonly used as shown below:
Symbol | Procedure Type | Example Usage | Operation Order |
---|---|---|---|
+ | Addition | c = a + b | Last |
- | Subtraction | c = a - b | Last |
- | Negation | c = - a | Last |
* | Multiplication | c = a * b | Second |
/ | Division | c = a / b | Second |
BASIC can also use two other operators for INTEGER division. Integer division returns only whole number values. MOD remainder division returns a value only if an integer division cannot divide a number exactly. Returns 0 if a value is exactly divisible.
Symbol | Procedure Type | Example Usage | Operation Order |
---|---|---|---|
\ | Integer division | c = a \ b | Second |
MOD | Remainder division | c = a MOD b | Second |
There is also an operator for exponential calculations. The exponential operator is used to raise a number's value to a designated exponent of itself. In QB the exponential return values are DOUBLE values. The SQR function can return a number's Square Root. For other exponential roots the operator can be used with fractions such as (1 / 3) designating the cube root of a number.
Symbol | Procedure | Example Usage | Operation Order |
---|---|---|---|
^ | Exponent | c = a ^ (1 / 2) | First |
SQR | Square Root | c = SQR(a ^ 2 + b ^ 2) | First |
Notes
- Exponent fractions should be enclosed in () brackets in order to be treated as a root rather than as division.
- Negative exponential values must be enclosed in () brackets in QB64.
Basic's Order of Operations
When a normal calculation is made, BASIC works from left to right, but it does certain calculations in the following order:
Sometimes a calculation may need BASIC to do them in another order or the calculation will return bad results. BASIC allows the programmer to decide the order of operations by using parenthesis around parts of the equation. BASIC will do the calculations inside of the parenthesis brackets first and the others from left to right in the normal operation order.
Basic's Mathematical Functions
Function | Description |
---|---|
ABS(n) | returns the absolute (positive) value of n: ABS(-5) = 5 |
ATN(angle*) | returns the arctangent of an angle in radians: π = 4 * ATN(1) |
COS(angle*) | returns the cosine of an angle in radians. (horizontal component) |
EXP(n) | returns e ^ x, (n <= 88.02969): e = EXP(1) ' (e = 2.718281828459045) |
LOG(n) | returns the base e natural logarithm of n. (n > 0) |
SGN(n) | returns -1 if n < 0, 0 if n = 0, 1 if n > 0: SGN(-5) = -1 |
SIN(angle*) | returns the sine of an angle in radians. (vertical component) |
SQR(n) | returns the square root of a number. (n >= 0) |
TAN(angle*) | returns the tangent of an angle in radians |
Degree to Radian Conversion: FUNCTION Radian (degrees) Radian = degrees * (4 * ATN(1)) / 180 END FUNCTION FUNCTION Degree (radians) Degree = radians * 180 / (4 * ATN(1)) END FUNCTION Logarithm to base n FUNCTION LOGN (X, n) IF n > 0 AND n <> 1 AND X > 0 THEN LOGN = LOG(X) / LOG(n) ELSE BEEP END FUNCTION FUNCTION LOG10 (X) 'base 10 logarithm IF X > 0 THEN LOG10 = LOG(X) / LOG(10) ELSE BEEP END FUNCTION |
Derived Mathematical Functions
The following Trigonometric functions can be derived from the BASIC Mathematical Functions listed above. Each function checks that certain values can be used without error or a BEEP will notify the user that a value could not be returned. An error handling routine can be substituted if desired. Note: Functions requiring π use 4 * ATN(1) for SINGLE accuracy. Use ATN(1.#) for DOUBLE accuracy.
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 |
Hyperbolic Function Relationships: COSH(-x) = COSH(x) SINH(-x) = -SINH(x) SECH(-x) = SECH(x) CSCH(-x) = -CSCH(x) TANH(-x) = -TANH(x) COTH(-x) = -COTH(x) Inverse Hyperbolic Function Relatonships: ARSECH(x) = ARCOSH(x) ^ -1 ARCSCH(x) = ARSINH(x) ^ -1 ARCOTH(x) = ARTANH(x) ^ -1 Hyperbolic sine and cosine satisfy the Pythagorean trig. identity: (COSH(x) ^ 2) - (SINH(x) ^ 2) = 1 |
Mathematical Logical Operators
The following logical operators compare numerical values using bitwise operations. The two numbers are compared by the number's Binary bits on and the result of the operation determines the value returned in decimal form. NOT checks one value and returns the opposite. It returns 0 if a value is not 0 and -1 if it is 0. See Binary for more on bitwise operations.
Table 4: The logical operations and its results. In this table, A and B are the Expressions to invert or combine. Both may be results of former Boolean evaluations. ┌────────────────────────────────────────────────────────────────────────┐ │ Logical Operations │ ├───────┬───────┬───────┬─────────┬────────┬─────────┬─────────┬─────────┤ │ A │ B │ NOT B │ A AND B │ A OR B │ A XOR B │ A EQV B │ A IMP B │ ├───────┼───────┼───────┼─────────┼────────┼─────────┼─────────┼─────────┤ │ true │ true │ false │ true │ true │ false │ true │ true │ ├───────┼───────┼───────┼─────────┼────────┼─────────┼─────────┼─────────┤ │ true │ false │ true │ false │ true │ true │ false │ false │ ├───────┼───────┼───────┼─────────┼────────┼─────────┼─────────┼─────────┤ │ false │ true │ false │ false │ true │ true │ false │ true │ ├───────┼───────┼───────┼─────────┼────────┼─────────┼─────────┼─────────┤ │ false │ false │ true │ false │ false │ false │ true │ true │ └───────┴───────┴───────┴─────────┴────────┴─────────┴─────────┴─────────┘ Note: In most BASIC languages incl. QB64 these are bitwise operations, hence the logic is performed for each corresponding bit in both operators, where true or false indicates whether a bit is set or not set. The outcome of each bit is then placed into the respective position to build the bit pattern of the final result value. As all Relational Operations return negative one (-1, all bits set) for true and zero (0, no bits set) for false, this allows us to use these bitwise logical operations to invert or combine any relational checks, as the outcome is the same for each bit and so always results into a true (-1) or false (0) again for further evaluations. |
Relational Operations
Relational Operations are used to compare values in a Conditional IF...THEN, SELECT CASE, UNTIL or WHILE statement.
Table 3: The relational operations for condition checking. In this table, A and B are the Expressions to compare. Both must represent the same general type, i.e. they must result into either numerical values or STRING values. If a test succeeds, then true (-1) is returned, false (0) if it fails, which both can be used in further Boolean evaluations. ┌─────────────────────────────────────────────────────────────────────────┐ │ Relational Operations │ ├────────────┬───────────────────────────────────────────┬────────────────┤ │ Operation │ Description │ Example usage │ ├────────────┼───────────────────────────────────────────┼────────────────┤ │ A = B │ Tests if A is equal to B. │ IF A = B THEN │ ├────────────┼───────────────────────────────────────────┼────────────────┤ │ A <> B │ Tests if A is not equal to B. │ IF A <> B THEN │ ├────────────┼───────────────────────────────────────────┼────────────────┤ │ A < B │ Tests if A is less than B. │ IF A < B THEN │ ├────────────┼───────────────────────────────────────────┼────────────────┤ │ A > B │ Tests if A is greater than B. │ IF A > B THEN │ ├────────────┼───────────────────────────────────────────┼────────────────┤ │ A <= B │ Tests if A is less than or equal to B. │ IF A <= B THEN │ ├────────────┼───────────────────────────────────────────┼────────────────┤ │ A >= B │ Tests if A is greater than or equal to B. │ IF A >= B THEN │ └────────────┴───────────────────────────────────────────┴────────────────┘ The operations should be very obvious for numerical values. For strings be aware that all checks are done case sensitive (i.e. "Foo" <> "foo"). The equal/not equal check is pretty much straight forward, but for the less/greater checks the ASCII value of the first different character is used for decision making: E.g. "abc" is less than "abd", because in the first difference (the 3rd character) the "c" has a lower ASCII value than the "d". This behavior may give you some subtle results, if you are not aware of the ASCII values and the written case: E.g. "abc" is greater than "abD", because the small letters have higher ASCII values than the capital letters, hence "c" > "D". You may use LCASE$ or UCASE$ to make sure both strings have the same case. |
Basic's Rounding Functions
- Rounding is used when the program needs a certain number value or type. There are 4 INTEGER or LONG Integer functions and one function each for closest SINGLE and closest DOUBLE numerical types. Closest functions use "bankers" rounding which rounds up if the decimal point value is over one half. Variable types should match the return value.
Name | Description |
---|---|
INT(n) | rounds down to lower Integer value whether positive or negative |
FIX(n) | rounds positive values lower and negative to a less negative Integer value |
CINT(n) | rounds to closest Integer. Rounds up for decimal point values over one half. |
CLNG(n) | rounds Integer or Long values to closest value like CINT.(values over 32767) |
CSNG(n) | rounds Single values to closest last decimal point value. |
CDBL(n) | rounds Double values to closest last decimal point value. |
_ROUND | rounds to closest numerical integer value in QB64 only. |
Notes
- Each of the above functions define the value's type in addition to rounding the values.
Base Number Systems
Comparing the INTEGER Base Number Systems Decimal (base 10) Binary (base 2) Hexadecimal (base 16) Octal (base 8) &B &H HEX$(n) &O OCT$(n) 0 0000 0 0 1 0001 1 1 2 0010 2 2 3 0011 3 3 4 0100 4 4 5 0101 5 5 6 0110 6 6 7 0111 7 7 -- maxed 8 1000 8 10 maxed-- 9 1001 9 11 10 1010 A 12 11 1011 B 13 12 1100 C 14 13 1101 D 15 14 1110 E 16 15 ------------- 1111 <--- Match ---> F ---------------- 17 -- max 2 16 10000 10 20 When the Decimal value is 15, the other 2 base systems are all maxed out! The Binary values can be compared to all of the HEX value digit values so it is possible to convert between the two quite easily. To convert a HEX value to Binary just add the 4 binary digits for each HEX digit place so: F A C E &HFACE = 1111 + 1010 + 1100 + 1101 = &B1111101011001101 To convert a Binary value to HEX you just need to divide the number into sections of four digits starting from the right(LSB) end. If one has less than 4 digits on the left end you could add the leading zeros like below: &B101011100010001001 = 0010 1011 1000 1000 1001 hexadecimal = 2 + B + 8 + 8 + 9 = &H2B889 See the Decimal to Binary conversion function that uses HEX$ on the &H page. |
- VAL reads the string from left to right and converts numerical string values, - and . to decimal values until it finds a character other than those 3 characters. Commas are not read.
- HEXadecimal and OCTal base values can be read with &H or &O.
- STR$ converts numerical values to string characters for PRINT or variable strings. It also removes the right number PRINT space.
Bits and Bytes
- The MSB is the most significant(largest) bit value and LSB is the least significant bit of a binary or register memory address value. The order in which the bits are read determines the binary or decimal byte value. There are two common ways to read a byte:
- "Big-endian": MSB is the first bit encountered, decreasing to the LSB as the last bit by position, memory address or time.
- "Little-endian": LSB is the first bit encountered, increasing to the MSB as the last bit by position, memory address or time.
Offset or Position: 0 1 2 3 4 5 6 7 Example: 11110000 ---------------------------------- -------- Big-Endian Bit On Value: 128 64 32 16 8 4 2 1 240 Little-Endian Bit On Value: 1 2 4 8 16 32 64 128 15 |
- The big-endian method compares exponents of 2 ^ 7 down to 2 ^ 0 while the little-endian method does the opposite.
- INTEGER values consist of 2 bytes called the HI and LO bytes. Anytime that the number of binary digits is a multiple of 16 (2bytes, 4 bytes, etc.) and the HI byte's MSB is on(1), the value returned will be negative, even with SINGLE or DOUBLE values.
16 BIT INTEGER OR REGISTER AH (High Byte Bits) AL (Low Byte Bits) BIT: 15 14 13 12 11 10 9 8 | 7 6 5 4 3 2 1 0 ---------------------------------------|-------------------------------------- HEX: 8000 4000 2000 1000 800 400 200 100 | 80 40 20 10 8 4 2 1 |
DEC: -32768 16384 8192 4096 2048 1024 512 256 | 128 64 32 16 8 4 2 1 |
- The HI byte's MSB is often called the sign bit! When the highest bit is on, the signed value returned will be negative.
Example: Program displays the bits on for any integer value between -32768 and 32767 or &H80000 and &H7FFF.
DEFINT A-Z SCREEN 12 COLOR 11: LOCATE 10, 2 PRINT " AH (High Register Byte Bits) AL (Low Register Byte Bits)" COLOR 14: LOCATE 11, 2 PRINT " 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0" COLOR 13: LOCATE 14, 2 PRINT " &H8000 4000 2000 1000 800 400 200 100 80 40 20 10 8 4 2 &H1" COLOR 11: LOCATE 15, 2 PRINT "-32768 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1" FOR i = 1 TO 16 CIRCLE (640 - (37 * i), 189), 8, 9 'place bit circles NEXT LINE (324, 160)-(326, 207), 11, BF 'line splits bytes DO IF Num THEN FOR i = 15 TO 0 STEP -1 IF (Num AND 2 ^ i) THEN PAINT (640 - (37 * (i + 1)), 189), 12, 9 BinStr$ = BinStr$ + "1" ELSE PAINT (640 - (37 * (i + 1)), 189), 0, 9 BinStr$ = BinStr$ + "0" END IF NEXT COLOR 10: LOCATE 16, 50: PRINT "Binary ="; VAL(BinStr$) COLOR 9: LOCATE 16, 10: PRINT "Decimal ="; Num;: COLOR 13: PRINT " Hex = "; Hexa$ Hexa$ = "": BinStr$ = "" END IF COLOR 14: LOCATE 17, 15: INPUT "Enter a decimal or HEX(&H) value (0 Quits): ", frst$ first = VAL(frst$) IF first THEN LOCATE 17, 15: PRINT SPACE$(55) COLOR 13: LOCATE 17, 15: INPUT "Enter a second value: ", secnd$ second = VAL(secnd$) LOCATE 17, 10: PRINT SPACE$(69) END IF Num = first + second Hexa$ = "&H" + HEX$(Num) LOOP UNTIL first = 0 OR Num > 32767 OR Num < -32767 COLOR 11: LOCATE 28, 30: PRINT "Press any key to exit!"; SLEEP SYSTEM |
OFFSET
- _OFFSET (function) returns the memory offset position as a flexible sized value for a designated variable. See Using _OFFSET.
See also