QBasic Information Center Tutorials - Arithmetic Functions
These tutorials were written to help you get a quick, but thorough, understanding of QBasic -
the scope of the language as well as it's specific capabilities.
Arithmetic Functions Summary
As languages go, QBasic provides relatively few arithmetic functions.
Example usage of each function is provided below, but as you can
see the functions are very simple to use. The arithmetic functions
can be categorized roughly into four areas:
|
| let, swap, clear
|
|
| data, read, restore
|
|
| exp, log, sqr
|
|
| hex$, oct$
|
|
| abs, sgn, fix, mod, int
|
|
| rnd, randomize
|
Arithmetic Function Reference
Here's a quick reference of the available arithmetic functions, in alphabetical
order. The functions are very simple to use.
Unless otherwise noted, these functions operate on $_ by default.
- abs - absolute value
result = abs (-5) # result is 5
- clear - closes all files, clears all common variables,
reset values of all strings/numbers.
clear # no arguments required
- data - specifies data for subsequent read statements
data value1, value2, value3 .. # comma separated value
data 1, 4, 5, 7 # numbers
data "ab", "rs" # strings
data statements may be entered only at the module level.
- exp - raises e to a value
$result = exp (expression) # raises e to the expression value
- fix - truncates a floating point value
result = fix (2.63) # returns 2
- hex$ - returns hexadecimal string representation of a number
result$ = hex (79) # returns "4F"
Note: expression values are rounded to integers before converting
- int - returns largest integer less than or equal to expression
result = int (79.8) # returns 79
- let - assign value to a variable
let result = 5
- log - natural (base e) logarithm
result = log 1 # returns base e log of 1 - 2.718182
- mod - returns remainder of a division
result = (expr1)MOD(expr2) # returns remainder of expr1/expr2
result = 5.8 MOD 2.8 # returns 0 0 6/3=2 with no remainder
Note: expression values are rounded to integers before dividing
- oct$ - returns octal string representation of a number
result$ = oct$ (49) # returns the string "61"
result$ = oct$ (47.8) # returns the string "61"
Note: expression values are rounded to integers before converting
- randomize - initializes random number generator
randomize (seed$) # provides seed value
randomize TIMER # seed is seconds since midnight
- read - reads values from data statements
data "one", "two", "three" # data statement stores values
read a$, b$, c$ # reads values from data statements
- restore - allows rereading of values
data "one", "two", "three" # data statement stores values
read a$, b$, c$ # reads values from data statements
restore line # next read start with data on line
read d$, e$, f$ # rereads values from data statement
- rnd - return random number between 0 and 1
result = rnd # new random number (no value)
result = rnd (-5) # same number (any value < 0)
result = rnd (2) # new random number (any value > 0)
result = rnd (0) # last random number (value = 0)
- sgn - returns sign of expression, or 0 if expression = 0
result = sgn (-5) # returns -1
result = sgn (75) # returns +1
result = sgn (0) # returns 0
- sqr - returns square root of a numeric-expression
result = sqr (25) # returns 5
result = sqr (42) # returns 6.480741
- swap - exchange values of two variables
a$ = "a" : b$ = "b" # set test values
swap a$, b$ # a$ = "b" and b$ = "a" (values swapped)
|