* To get '''one byte''' values, can use an [[ASCII]] [[STRING]] character to represent values from 0 to 255 as in [[BINARY]] files.
<center>'''QB64 Number Types'''</center>
* [[_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|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
<center>'''Signed and Unsigned Integer Values'''</center>
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.
::::* [[SGN]](n) returns the value's sign as -1 if negative, 0 if zero or 1 if positive.
::::* [[ABS]](n) changes negative values to the equivalent positive values.
::::* '''QB64:''' [[_UNSIGNED]] in a [[DIM]], [[AS]] or [[_DEFINE]] statement for only positive [[INTEGER]] values.
<center>[[#toc|Return to Top]]</center>
==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:
{| align="center" border=1
! Symbol
! Procedure Type
! Example Usage
! Operation Order
|-
| align="center" |[[+]] || Addition || align="center" | c = a + b || align="center" | Last
|-
| align="center" |[[-]] || Subtraction || align="center" | c = a - b || 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" |[[/]] || Division || align="center" | c = a / b || align="center" | 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.
{| align="center" border=1
!Symbol
!Procedure Type
!Example Usage
!Operation Order
|-
| 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
|}
<center>'''''It is an [[ERROR|error]] to divide by zero or to take the remainder modulo zero.'''''</center>
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.
{| align="center" border=1
!Symbol
!Procedure
!Example Usage
!Operation Order
|-
| 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
<center>[[#toc|Return to Top]]</center>
==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:
:::# Exponential and exponential Root calculations including [[SQR]].
:::# Negation (Note that this means that ''- 3 ^ 2'' is treated as ''-(3 ^ 2)'' and not as ''(-3) ^ 2.)''
:::# Multiplication, normal Division, [[INTEGER]] Division and Remainder([[MOD]]) Division calculations
:::# Addition and Subtraction calculations
<center>'''Using Parenthesis to Define the Operation Order'''</center>
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==
{| align=center border=1
! 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<sup>x</sup>, '''(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
|}
<center> '''* angles measured in radians'''</center>
{{TextStart}} '''Degree to Radian Conversion:'''
FUNCTION Radian (degrees)
Radian = degrees * (4 * {{Cb|ATN}}(1)) / 180
END FUNCTION '' ''
FUNCTION Degree (radians)
Degree = radians * 180 / (4 * {{Cb|ATN}}(1))
END FUNCTION
'''Logarithm to base n'''
FUNCTION LOGN (X, n)
IF n > 0 AND n <> 1 AND X > 0 THEN LOGN = {{Cb|LOG}}(X) / {{Cb|LOG}}(n) ELSE BEEP
END FUNCTION '' ''
FUNCTION LOG10 (X) 'base 10 logarithm
IF X > 0 THEN LOG10 = {{Cb|LOG}}(X) / {{Cb|LOG}}(10) ELSE BEEP
<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.
{{TextStart}}'' ''
FUNCTION SEC (x) 'Secant
IF COS(x) <> 0 THEN SEC = 1 / {{Cb|COS}}(x) ELSE BEEP
END FUNCTION
FUNCTION CSC (x) 'CoSecant
IF SIN(x) <> 0 THEN CSC = 1 / {{Cb|SIN}}(x) ELSE BEEP
END FUNCTION
FUNCTION COT (x) 'CoTangent
IF TAN(x) <> 0 THEN COT = 1 / {{Cb|TAN}}(x) ELSE BEEP
END FUNCTION
FUNCTION ARCSIN (x) 'Inverse Sine
IF x < 1 THEN ARCSIN = {{Cb|ATN}}(x / {{Cb|SQR}}(1 - (x * x))) ELSE BEEP
END FUNCTION
FUNCTION ARCCOS (x) ' Inverse Cosine
IF x < 1 THEN ARCCOS = (2 * ATN(1)) - {{Cb|ATN}}(x / {{Cb|SQR}}(1 - x * x)) ELSE BEEP
END FUNCTION
FUNCTION ARCSEC (x) ' Inverse Secant
IF x < 1 THEN ARCSEC = {{Cb|ATN}}(x / {{Cb|SQR}}(1 - x * x)) + ({{Cb|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 * {{Cb|ATN}}(1)) - {{Cb|ATN}}(x)
END FUNCTION '' ''
FUNCTION SINH (x) ' Hyperbolic Sine
IF x <= 88.02969 THEN SINH = ({{Cb|EXP}}(x) - {{Cb|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
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.
<center>'''Truth table of the 6 BASIC Logical Operators'''</center>
{{Template:LogicalTruthTable}}
<center>BASIC can accept any + or - value that is not 0 to be True when used in an evaluation.</center>
<center>[[#toc|Return to Top]]</center>
==Relational Operators==
Relational Operations are used to compare values in a Conditional [[IF...THEN]], [[SELECT CASE]], [[UNTIL]] or [[WHILE]] statement.
{{Template:RelationalTable}}
<center>[[#toc|Return to Top]]</center>
==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.
{| align=center border=1
! 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.
* 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]].
<center>'''The [[OCT$]] [[STRING|string]] function return can be converted to a decimal value using [[VAL]]("&O" + OCT$(n)).'''</center>
<center>'''The [[HEX$]] [[STRING|string]] function return can be converted to a decimal value using [[VAL]]("&H" + HEX$(n)).'''</center>
:[[STR$]] converts numerical values to string characters for [[PRINT]] or variable strings. It also removes the right number PRINT space.
<center>[[#toc|Return to Top]]</center>
* 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.
'''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
{{WhiteEnd}}
::The big-endian method compares exponents of 2 <sup>7</sup> down to 2 <sup>0</sup> 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.