Visual Basic/Simple Arithmetic

Visual Basic has all of the common arithmetical functions. It does not have complex numbers nor does it allow you to overload operators so manipulating complex numbers or matrices must be done by explicit function and subroutine calls.

Arithmetic Operators
The operators are infix and take two arguments: arg1 operator arg2 except for unary plus and minus

Numeric Operators
For example:

Will display the value in Text1, multiplied by 5, in Text2. E.g. if Text1 = 4 then Text2 will be equal to 20.

Order of operations

 * Exponentiation (^)
 * Multiplication and normal division (* and /)
 * Integer division (\)
 * Mod (Mod)
 * Addition and subtraction (+,-)

General hint : If an expression uses more than (+,-,*) use all possible brackets to force the expression to be evaluated the way you are thinking it.

VB odd behavior note
VB considers "explicitly stated" integral numbers to be of type Integer (which must be between -32768 and 32767) if they are within (-32768, +32767) and gives an error if the result of arithmetic with them is more than 32768. This can be seen by trying

or

which both cause an error. This can be solved (in a direct but ugly way) by enclosing numbers in CLng (Convert to Long) so that

or by using the type-declaration character & which specifies a Long constant:

neither of which cause an error. To avoid having to think about this, avoid using explicit numbers in code and instead use "Long" variables and constants such as :

Boolean Arithmetic
Boolean operators use Boolean variables or integer variables where each individual bit is treated as a Boolean. There are six operators:

When you construct logical expressions with these operators you get the following results:

Comparison Operators
These operators, composed of <, > and =, are use to decide whether one value is smaller than, larger than, or equal to another.

For example:

Caution! Due to the internal structure of floating-point numbers (Single and Double), do not use = or <> to compare them. Instead, use a small value (usually called Epsilon) as a "maximum difference". For example:

Built in Arithmetic Functions
There are not many native mathematical functions in Visual basic but this doesn't mean that you can't do significant calculations with it.


 * Abs(x):returns the absolute value of x, that is, it removes a minus sign if there is one. Examples: Abs(3)=3 ; Abs(-3)=3
 * Exp(x):returns the value ex. e is Euler's constant, the base of natural logarithms.
 * Log(x): the Neperian ('Natural', e base) logarithm of x.
 * Randomize(x): not really a mathematical function because it is actually a subroutine. This initializes the random number generator.
 * Rnd(x):produces the next random number in the series. Please read that sentence again! the random numbers aren't really random, they are instead pseudo-random.  If you initialize the random number generator with the same number each time you start a program then you will get the same series of values from Rnd
 * Round(x,n):returns a real number rounded to n decimal places (uses Banker's rounding).
 * Sgn(x):returns plus one if x is positive, minus one if it is negative, zero if x is identically zero. Sgn(-5)=-1 ; Sgn(5)=1 ; Sgn(0)=0
 * Sqr(x):square root of x. Example: Sqr(25)=5. x must be non-negative. Thus Sqr(-25) will generate an error

Derived Functions
If you want logarithms to some other base you can use this expression:

And to calculate the nth root of a number (cube root, ...)

Trigonometrical Functions
Visual Basic has the usual simple trigonometric functions, sin, cos, tan, but if you want some of the more unusual ones or inverses you will need to write some simple functions.

Remember that the angles must be supplied as radians

Notice that the range of applicability of these expressions is limited to the range -1<=x<=1.

Here are some more:

The very useful atan2 function (calculate the angle in all four quadrants of a vector) can be simulated like this:

Other functions: