Mathematical Operations

From QB64 Wiki
Revision as of 20:37, 30 September 2009 by imported>Clippy
Jump to navigation Jump to search
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:
+ .... Addition: c = a + b
- .... Subtraction: c = a - b
* .... 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 or remainder division returns a value only if an integer division cannot divide a number evenly.
\ .... Integer division: c = a \ b
MOD .... Remainder division: c = a MOD b
There are also a couple of operators for exponential calculations. The exponential operator is used to raise a number's value to a designated exponent of itself. The SQR function returns 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. Must use parenthesis!
^ .... Exponent: c = a ^ 2 + b ^ 2
SQR(n) .... Square root: h = SQR(c)


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) Multiplication and Division calculations
3) Addition and Subtraction 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.


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 it's opposite. It returns 0 if a value is not 0 and -1 if it is 0.


  • Truth table of the 6 BASIC Logical Operators:
Values
A .. B .... NOT A ... 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 ......... F ............ F .............. T ............. T .............. F ............... F
F .. T ......... T ............ F .............. T ............. T .............. F ............... T
F .. F ......... T ............ F .............. F ............. F ............. T ............... T
  • Note that Basic returns -1 for True and 0 for False.
BASIC can accept any + or - value that is not 0 to be True when used in an evaluation.