# 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

| Operator | Comments |
| + | Adds two numbers. Also concatenates strings but avoid this use because Visual Basic will try to convert the string to a number first and the results might not be what you expect. |
| - | Subtract the second number from the first. |
| - unary | negate the operand. |
| * | Multiply two numbers. The result will be promoted to whatever data type is needed to represent the size of the number if one of the numbers is already that type (see note below about odd behavior). |
| / | Normal division. Treats both operands as real numbers and returns a real result. |
| \ | Integer division. Be careful with this because the arguments are converted to integers before the division is performed so use this with integer operands unless you comment your code carefully. Also note that "/" and "*" are evaluated before "\", so (1000 \ 13 * 3) is not equal to ( (1000 \ 13) * 3) |
| Mod | Produces the remainder after integer division. Be careful with this as the interpretation of the modulus operator in mathematics is ambiguous. a Mod b gives the same answer as this expression: a - (a \ b) * b |
| ^ | Raises the first operand to the power of the second. In Visual Basic either or both operands may be negative. If all you want is a square or cube it is faster to explcitly multiply the number by itself the appropriate number of times. |
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:
| Operator: | Meaning: |
| Not | Negation |
| And | Conjuncton |
| Or | Disjunction (logical addition) |
| Xor | Exclusive Or |
| Eqv | Equivalence |
| Imp | Implication |
When you construct logical expressions with these operators you get the following results:
| A | B | A And B | A Or B | A Xor B | A Eqv B | A Imp B |
| T | T | T | T | F | T | T |
| T | F | F | T | T | F | F |
| F | T | F | T | T | F | T |
| F | F | F | F | F | T | T |

## 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:
| Operator | Meaning |
| = | Equality |
| <> | Inequality |
| < | Less than |
| > | Greater than |
| >= | Greater than or equal to. Or put another way: not less than |
| <= | Less than or equal to. Or put another way: not greater than |

## 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 e$^x$. 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:
| Secant | Sec(x) = 1 / Cos(x) |
| Cosecant | Cosec(x) = 1 / Sin(x) |
| Cotangent | Cotan(x) = 1 / Tan(x) |
| Inverse Sine | Arcsin(x) = Atn(x / Sqr(-x * x + 1)) |
| Inverse Cosine | Arccos(x) = Atn(-x / Sqr(-x * x + 1)) + 2 * Atn(1) |
| Inverse Secant | Arcsec(x) = Atn(x / Sqr(x * x - 1)) + Sgn((x) - 1) * (2 * Atn(1)) |
| Inverse Cosecant | Arccosec(x) = Atn(x / Sqr(x * x - 1)) + (Sgn(x) - 1) * (2 * Atn(1)) |
| Inverse Cotangent | Arccotan(x) = -Atn(x) + 2 * Atn(1) |
| Hyperbolic Sine | HSin(x) = (Exp(x) - Exp(-x)) / 2 |
| Hyperbolic Cosine | HCos(x) = (Exp(x) + Exp(-x)) / 2 |
| Hyperbolic Tangent | HTan(x) = (Exp(x) - Exp(-x)) / (Exp(x) + Exp(-x)) |
| Hyperbolic Secant | HSec(x) = 2 / (Exp(x) + Exp(-x)) |
| Hyperbolic Cosecant | HCosec(x) = 2 / (Exp(x) - Exp(-x)) |
| Hyperbolic Cotangent | HCotan(x) = (Exp(x) + Exp(-x)) / (Exp(x) - Exp(-x)) |
| Inverse Hyperbolic Sine | HArcsin(x) = Log(x + Sqr(x * x + 1)) |
| Inverse Hyperbolic Cosine | HArccos(x) = Log(x + Sqr(x * x - 1)) |
| Inverse Hyperbolic Tangent | HArctan(x) = Log((1 + x) / (1 - x)) / 2 |
| Inverse Hyperbolic Secant | HArcsec(x) = Log((Sqr(-x * x + 1) + 1) / x) |
| Inverse Hyperbolic Cosecant | HArccosec(x) = Log((Sgn(x) * Sqr(x * x + 1) + 1) / x) |
| Inverse Hyperbolic Cotangent | HArccotan(x) = Log((x + 1) / (x - 1)) / 2 |
The very useful atan2 function (calculate the angle in all four quadrants of a vector) can be simulated like this:
Other functions:
| Previous: Getting Started | Contents | Next: Branching |