Mathematical operators

From Liberty BASIC Family
Jump to navigation Jump to search
Supported in Just BASIC Supported in Liberty BASIC Not supported in Liberty BASIC 5 Supported in Run BASIC

Description

JB mathematical operators list:
Operator Meaning
^ Power (Exponentiation)
* Multiplication
/ Division
MOD Modulus (Remainder of a Division)
+ Addition
- Subtraction

Syntax

  1. numerical_expression operator numerical_expression
Result is a numerical expression.

Hints

If there are several operators in an expression, they are evaluated by rules of precedence
  • first: ^
  • second: * / mod
  • third: + -
within the same precedence - from left to right.
Just Basic supports operations on arbitrary-length integers, see Numbers. When working with integers, all operations except division return an integer (possibly long integer). There is no special integer division, if a and b are integers, and a/b happens to return an integer, you get an (may be long) integer, otherwise you get a Double (a floating point value with 16 significant digits and a maximum range of about +/- 1e307).
But if you take INT(a/b) where a is a long integer and b is a (possibly long) integer, then you can get a long integer. It is probably due to JB not storing intermediate results as floating point until it has to.
Exponentiation operator (^, a^b reads as "a to the power of b") is defined in mathematics so that, if a>0, then b could be any number; but if a<0, then b must be an integer.
The MOD operator could be described as
a MOD b = a - INT(a/b)*b
"all we get when we subtract b from a till the remaining number is less than b"
Note that in JB for negative a the result of a MOD b is negative.
The MOD operator is very handy for wrapping indexes around, just remember that a MOD b for integers a and b produces numbers from 0 to b-1.
Wrapping things around is nicely illustrated by the day of week concept (see the DATE$() function).


Example

' Place a simple, working example of the keyword here

Useful Procedures

' Place a useful function or sub using this keyword here