Numeric Expressions

Numeric data is represented as a sequence of digits (0 – 9) with an optional decimal point. A leading plus (+) or minus (-) sign might be used, but commas are not allowed. Any data containing any characters other than numbers and a single decimal point will be interpreted as a string. Some examples of numeric values are:

-34

42368.99

+3.1416

Numeric data can contain up to 19 digits, including a maximum of 9 decimal positions. See the PRECISION statement in Overview of mvBASIC Statements and Functions for information on how to set the maximum number of fractional digits.

Arithmetic Operators

Arithmetic operations range from the simplest calculations (such as COST = COST + 5) to complex expressions combining trigonometric and logarithmic functions. In general, when several arithmetic operations are used in one expression, they follow accepted mathematical guidelines to precedence. The arithmetic operators available to mvBASIC, in order of precedence, are listed as follows:

Operator

Operation

Sample Expression

+

Unary plus

+COST

-

Unary minus

–COST

*

Multiplication

COST * EXPENSES

/

Division

COST / EXPENSES

+

Addition

COST + EXPENSES

-

Subtraction

COST – EXPENSES

In cases where operators equivalent in precedence (such as * and /) are used, the order of evaluation follows left to right.

Parentheses in Expressions

The order of evaluation can be changed by using parentheses. Operations on expressions enclosed in parentheses are performed before the others.

Example

(14 * 8) + 12 / 2 + 2

In the example above, expression is evaluated as 112+6+2 or 120. On the other hand, the following arithmetic expression is evaluated as 14*20/4 or 70:

14 * (8 + 12) / (2 + 2)

In arithmetic expressions parentheses must be placed correctly in order to obtain the desired result.

Character Strings in Arithmetic Expressions

If a character string variable that evaluates to a number is used within an arithmetic expression, the character string is treated as a numeric variable. That is, the numeric character string is converted to its equivalent internal number and then evaluated numerically within the arithmetic expression.

55 + "22"

The preceding example is evaluated as 77. If a character string variable that does not evaluate to a number is used within an arithmetic expression, a warning message is displayed and the string is treated as zero.

The following example expression is evaluated as 55:

55 + "TWENTY TWO"

A message displays, warning that the data is nonnumeric, resembling the following:

[B16] LINE 16 NON-NUMERIC DATA WHEN NUMERIC REQUIRED; ZERO USED!

Intrinsic Mathematical Functions

An intrinsic function is a built-in mvBASIC function to be used on numeric operands. The following is a list of the mathematical functions available in mvBASIC:

Function

Description

ABS

Computes the absolute value of a given arithmetic expression.

COS

Returns the cosine value of the angle given in the expression.

EXP

Returns an exponential value that will raise the base number e (2.7183) to the value of the expression.

INT

Truncates the decimal portion of a given arithmetic expression and returns the integer value.

LN

Generates the natural logarithm (log base e) of the given expression.

PWR

Raises the value of an expression to the power denoted by a second expression.

REM

Divides an expression by another, and returns the remainder value only.

RND

Generates a random number within the range of 0 and the value of the expression minus 1.

SIN

Returns the sine value of the angle given in the expression.

SQRT

Computes the square root of any positive numeric expression.

TAN

Returns the tangent value of the angle given in the expression.

See Statement and Function Reference for full information on the syntax and behavior of these functions.

See Also

Building Expressions

Simple Assignment

Using Operators and Functions

String Expressions

Logical Data (Booleans)