exp, exp2, exp10, sqr
log, log2, log10
bin$, hex$, oct$
int, ceil, fix
min, max, swap
PowerBASIC allows number to be written using E notation for powers of 10. "E", "e", "D", or "d" may be used at the separator between the coefficient number and the exponent of ten. The E/e refers to raising the coefficient to the power of 10, not the mathematical constant e.
The use of E was popularized by the need for an easy way to express large and small numbers on calculators and computers, where 2.4E2 is easier to write than 2.4 x 10^2.
Numeric Conversion Between Types
In assignments and when passing parameter, PowerBASIC automatically converts a numeric values to the numeric type required, as in the following example.
i% = a! 'single precision a! is converted to integer c& = f@ 'currency type is converted to long integer
However, PowerBASIC also provides a series of functions which can explicitly make these conversions. These are rarely used since PowerBASIC makes the conversions automatically. The conversion functions are:
CBYT CDBL CINT CSNG CCUR CDWB CLNG CWRD CCUX CEXT CQUD i% = CINT(a!) 'same result as i%=a!
These functions should be self-explanatory as to the data type each returns. However, note that with the CINT/CLNG functions, when the fractional part of the number is exactly 0.5, the number is rounded to the nearest even number. This means a number ending in .5 may round up or down - not always up!
CINT(0.5) 'returns 0, not 1 (nearest EVEN number) CINT(1.5) 'returns 2 (nearest EVEN number) CINT(2.5) 'returns 2, not 3 CLNG(-4.7) 'returns -5 CLNG(7.7) 'returns 7
Note that while PowerBASIC will automatically convert between numeric data types, it will not automatically convert numeric values to strings. This has to be explicitly done by the program code.
Clarification of Integer Functions
The PowerBASIC functions dealing with integer - CINT, INT, FIX, CEIL, ROUND, CVI, and MKI$ - may seem obvious in their usage, but there are some nuances. Here are comparisons on how these functions round various numbers, including negative numbers. See specific examples further down this page.
BIN$ / HEX$ / OCT$ Functions
PowerBASIC can work with numbers in other than base 10. In particular, it supports base 2, 8, and 16. Numbers in bases other than 10 are written with one of the following prefixes. Numbers in base 10 are referred to a decimal values. Here's the notation to use for describing numbers of other bases. Note that PowerBASIC supports 3 ways to represent octal notation.
&B - binary &O - octal (that's letter O, not number zero 0) &Q - octal &Q7 & - octal &7 &H - hexadecimal A = &H0F 'hex value of "0F" (decimal 15) B = &Q7 'octal value of "7" (decimal 7) C = &B11 'binary value of "11" (decimal 3)
To convert numeric values to strings, PowerBASIC supplies the BIN$, OCT$, and HEX$ functions. These three functions take a numeric value and return the binary (base 2), octagonal (base 8), or hexadecimal (base 16) value as a string.
To convert base 2/8/16 strings to numeric (base 10) numbers, use the VAL function. It recognizes the following base number string prefixes.
Arithmetic Functions Listing
Here are one-line descriptions of all the arithmetic functions.
result = abs (-5) ' result = 5
result$ = bin$(6) ' result$ = "110"
result = ceil(5.2) ' result = 6
result = 5 DECR result ' result = 4
result = exp(5) ' result = e^5 = 148.4132
result = exp2(5) ' result = 2^5 = 32
result = exp10(5) ' result = 10^5 = 10,000
result = fix (2.63) ' result = 2
result = frac(2.3) ' result = 0.3 result = frac(2.9) ' result = 0.9
result$ = hex$ (79) # result$ = "4F"Note: expression values are rounded to integers before converting
result = 5 INCR result ' result = 6
result = int (79.8) ' returns 79 result = int (-1.5) ' returns -2
Note that INT rounds downward, hence -1.7 rounds down to -2.
a = 5 result = len(a) ' result = 4 (4 bytes) result = len("dog") ' result = 3 (3 characters)
The data type determines what Len will return.
numeric literal - not allowed string literal - length of string numeric variable - bytes required to hold that data type string variable - length of string fix-length string - buffer length ASCIIZ string - length of data stored (SizeOf gets max size) UDT - bytes required to hold that data type
let result = 5 ' result = 5 result = 5 ' "let" is optional
result = log 5 ' result = 1.60944
result = log2 5 ' result = 2.321928
result = log10 5 ' result = 0.69897
result = max(1,2,3,2) ' result = 3
result = min(1,2,3,2) ' result = 1
result = 5 MOD 2 ' result = 1 result = 5.8 MOD 2.8 ' result = 0.2000003For integers MOD works intuitively, but for floating point values the exact calculation is not so obvious. MOD truncates the division result and multiplies that by the divisor (number on bottom) before subtracting that product from the dividend (number on top). The second example above shows such a result.
result$ = oct$ (49) ' result$ = "61" result$ = oct$ (47.8) ' result$ = "61"Expression values are ROUNDed to integers before converting.
randomize 5 ' seed is always the same value randomize TIMER ' seed is current time randomize ' default seed is TIMER
reset a! ' a! is set to zero reset a!(5) ' element 5 of array is set to zero reset a() ' all elements are set to zero
result = rnd ' 0<= result < 1 result = rnd (0) ' argument 0 - repeats last result result = rnd (-5) ' argument < 0 - reseeds result = rnd (2) ' argument > 0 - next random number result = rnd (5,7) ' random long integer from 5 to 7
result = round(5.23,0) ' result = 5 result = round(5.23,1) ' result = 5.2 result = round(5.40,0) ' result = 5 result = round(5.50,0) ' result = 6 result = round(5.73,0) ' result = 6
result = sgn (-5) ' result = -1 result = sgn (75) ' result = +1 result = sgn (0) ' result = 0
result = sqr (25) ' result = 5 result = sqr (2) ' result = 1.414
a = 5 : b = 2 ' set test values swap a, b ' a = 2 and b = 5
result = "52AR" ' result = 52 result = "1.0G4T" ' result = 1.04 result = "A22" ' result = 0 result = "2e2" ' result = 200 result$ = "&H2" ' result = 2 result$ = "&HB" ' result = 11
Notes: Leading white spaces are ignored. +/- symbols are allowed. "e" or "d" may be used as powers of 10. Binary (&B) and octal (&O, &Q, &) strings are also recognized. Trailing type-specifiers are ignored.
Dim varVariant As Variant varVariant = 10 result = variant#(varVariant) ' result = 10
Dim result as DWORD Dim b as single b = 5 result = varptr(b) ' result = memory address of variable b
If you have any suggestions or corrections, please let me know.