PowerBASIC provides a reasonably complete set of arithmetic functions, which can be broken into the following categories. As you can see from the examples below, use of the functions is very simple and mostly self-explanatory.

**Assignment****Powers****Logarithms****Base Conversions****Random****Modify****Properties****Integer Result****Decimal Place Control****Multiple Variables****Sign Operation****Fraction/Remainder****Value Conversions****Pointer**

let, reset | |

exp, exp2, exp10, sqr | |

log, log2, log10 | |

bin$, hex$, oct$ | |

rnd, randomize | |

incr, decr | |

len, sizeof | |

int, ceil, fix | |

round | |

min, max, swap | |

abs, sgn | |

frac, mod | |

val, variant# | |

varptr |

**E Notation**

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.

**INT**- convert to integer (rounds down)**FIX**- truncate number to an integer (drops fractional part)**CEIL**- first integer greater than or equal to a value (rounds up)**ROUND**- round numeric value (>=0.5 rounds up, <0.5 rounds down)**CVI**- convert string to integer**MK$**- convert numeric value to string

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

**ABS**- absolute value of numeric expression**BIN$**- return binary (base 2) representation of a numeric value**CEIL**- first integer greater than or equal to a value**DECR**- subtract one from a variable (decrement)**EXP**- raise e to a power**EXP2**- raise 2 to a power**EXP10**- raise 10 to a power**FIX**- truncate fractional part of a number**FRAC**- fractional part of a floating-point number**HEX$**- hexadecimal (base 16) string representation of a number**INCR**- add one to a variable (increment)**INT**- convert to integer**LEN**- logical length**LET**- assign a value (or object reference with variants)**LOG**- natural logarithm (base e)**LOG2**- logarithm (base 2)**LOG10**- logarithm (base 10)**MAX**- return largest number from list**MIN**- return smallest number from list**MOD**- remainder of the division between two numbers**OCT$**- return octal (base 8) representation of a numeric value**RANDOMIZE**- seed random number generator**RESET**- set variable to zero (also applies to arrays, UDT, and variants)**RND**- random number 0<= x < 1**ROUND**- round numeric value to specified number of decimal places**SGN**- sign of a numeric expression (-1, 0, +1)**SQR**- square root**SWAP**- exchange values two variables, pointers, or pointer targets**VAL**- Return the numeric equivalent of a string argument**VARIANT#**- Return the numeric value contained in a Variant variable**VARPTR**- Returns address of a variable

Here are examples for each of the arithmetic functions. The functions are listed in alphabetical order.

**abs**- absolute valueresult = abs (-5) ' result = 5

**bin$**- binary representation of a numeric valueresult$ = bin$(6) ' result$ = "110"

**ceil**- first integer greater than or equal to a valueresult = ceil(5.2) ' result = 6

**decr**- subtract one from a variable (decrement)result = 5 DECR result ' result = 4

**exp**- raise e to a powerresult = exp(5) ' result = e^5 = 148.4132

**exp2**- raise 2 to a powerresult = exp2(5) ' result = 2^5 = 32

**exp10**- raise 10 to a powerresult = exp10(5) ' result = 10^5 = 10,000

**fix**- truncate fractional part of a numberresult = fix (2.63) ' result = 2

**frac**- fractional part of a floating-point numberresult = frac(2.3) ' result = 0.3 result = frac(2.9) ' result = 0.9

**hex$**- returns hexadecimal string representation of a numberresult$ = hex$ (79) # result$ = "4F"

Note: expression values are rounded to integers before converting**incr**- add one from a variable (decrement)result = 5 INCR result ' result = 6

**int**- returns largest integer less than or equal to expressionresult = int (79.8) ' returns 79 result = int (-1.5) ' returns -2

Note that INT rounds downward, hence -1.7 rounds down to -2.

**len**- for numeric values is number of bytes to store data typea = 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**- assign value to a variablelet result = 5 ' result = 5 result = 5 ' "let" is optional

**log**- log to base e of a numberresult = log 5 ' result = 1.60944

**log2**- log to base 2 of a numberresult = log2 5 ' result = 2.321928

**log10**- log to base 10 of a numberresult = log10 5 ' result = 0.69897

**max**- return largest number from listresult = max(1,2,3,2) ' result = 3

**min**- return smallest number from listresult = min(1,2,3,2) ' result = 1

**mod**- returns remainder of a divisionresult = 5 MOD 2 ' result = 1 result = 5.8 MOD 2.8 ' result = 0.2000003

For 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.**oct$**- returns octal string representation of a numberresult$ = oct$ (49) ' result$ = "61" result$ = oct$ (47.8) ' result$ = "61"

Expression values are ROUNDed to integers before converting.**randomize**- initializes random number generatorrandomize 5 ' seed is always the same value randomize TIMER ' seed is current time randomize ' default seed is TIMER

**reset**- set variable to zero (also applies to arrays, UDT, and variants)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

**rnd**- return random number 0<= x < 1result = 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

**round**- rounds numeric value to n decimal places (>=0.5 rounds up, <0.5 rounds down)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

**sgn**- returns sign of expression, or 0 if expression = 0result = sgn (-5) ' result = -1 result = sgn (75) ' result = +1 result = sgn (0) ' result = 0

**sqr**- returns square root of a numeric-expressionresult = sqr (25) ' result = 5 result = sqr (2) ' result = 1.414

**swap**- exchange values of two variables (must be of same type)a = 5 : b = 2 ' set test values swap a, b ' a = 2 and b = 5

**val**- return the numeric equivalent of a string argumentresult = "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.

**variant#**- returns numeric value from a variantDim varVariant As Variant varVariant = 10 result = variant#(varVariant) ' result = 10

**varptr**- returns address of a variableDim 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.