# Mathematical Operations

Jump to navigation Jump to search

## Basic and QB64 Numerical Types

Qbasic Number 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.

QB64 Number Types
• _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.1897E+4932. Cannot be unsigned.
• _OFFSET [%&]: undefined flexable length integer offset values used in DECLARE DYNAMIC LIBRARY declarations.

Signed and Unsigned Integer Values

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.

_UNSIGNED integer, byte and bit variable values can use the tilde ~ suffix before the type suffix to define the type.

Return to Top

## 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

It is an error to divide by zero or to take the remainder modulo zero.

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