# Mathematical Operations

Basic's Mathematical Operations

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 Name Usage Example
+ Addition c = a + b
- Subtraction c = a - b
- Negation c = - a
* Multiplication c = a * b
/ Division c = a / b

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 exactly divisible.

Symbol Name Usage Example
\ Integer division c = a \ b
MOD Remainder division c = a MOD b

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 roots the exponential operator can be used with fractions such as (1 / 3) designating the cube root of a number. Note that the fraction should be parenthesized in order for it to be treated as a fraction rather than a division of the result of the exponentiation by the denominator.

Symbol Name Usage Example
^ Exponent c = SQR(a ^ 2 + b ^ 2)

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

1. Exponential and exponential Root calculations
2. Negation (Note that this means that - 3 ^ 2 is treated as -(3^2) and not as (-3)^2.)
3. Multiplication and Division calculations

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 those calculations first and the others from left to right in the operation order.

Basic's Mathematical Functions
• ABS(n) ........... returns the absolute (positive) value of n (ABS(-5) = 5)
• ATN(angle) ...... returns the arctangent of a radian angle (π = 4 * ATN(1))
• COS(angle) ..... returns the cosine of a radian angle (horizontal ratio)
• EXP(n) ............ returns the exponent of n (n <= 88.02969)
• LOG(n) ........... returns the 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 a radian angle (vertical ratio)
• SQR(n) ........... returns the square root of a number (SQR(25) = 5)
• TAN(angle) ...... returns the tangent of a radian angle
Note: To convert from degrees to radians use: radians = degrees * (3.14159 / 180)

## Signed and Unsigned Numerical 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, 0 or 1
ABS(n) changes negative values to positive ones
QB64: _UNSIGNED in a DIM, AS or _DEFINE statement for only positive values.

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

### Truth table of the 6 BASIC Logical Operators

The results of the bitwise logical operations, where A and B are operands, and T and F indicate that a bit is set or not set:
Operands Operations
A B NOT B A AND B A OR B A XOR B A EQV B A IMP B
T T F T T F T T
T F T F T T F F
F T F F T T F T
F F T F F F T T
Relational Operations return negative one (-1, all bits set) and zero (0, no bits set) for true and false, respectively.
This allows relational tests to be inverted and combined using the bitwise logical operations.

BASIC can accept any + or - value that is not 0 to be True when used in an evaluation.

## 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.
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 value in QB64.
Note: Each of the above functions define the value's type in addition to rounding the values.