Programming:Visual Basic Classic/Simple Arithmetic - eMyTextBooks

Search:  

Free online books - just read it!

See live article   •   Programming:Visual Basic Classic/Simple Arithmetic
 

Programming:Visual Basic Classic/Simple Arithmetic

Table of contents

Introduction

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 dataype is needed to represent the size of the number.
/ 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.
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:

 Text2 = Text1 * 5

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

Boolean Arithmetic

Boolean operators use Boolean variables or integer variables where each individual bit is treated as a Boolean. The 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 of equal to another.

For example:

 Dim i
 i = 50
 If i < 0 Then
   MsgBox "i is less than 0" 
 ElseIf i <= 100 And i >= 0 Then
   MsgBox "i is less than or equal to one hundred and greater than or equal to 0"
 ElseIf i > 100 And i < 200 Then
   MsgBox "i is greater than one hundred less than 200"
 Else
   MsgBox "i is greater than or equal to 200"
 End if

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:

 ' This returns False :
 Debug.Print (Sqr(1.234) * Sqr(1.234)) = 1.234
 ' This returns True :
 E = 0.000001#
 Debug.Print Abs( (Sqr(1.234) * Sqr(1.234)) - 1.234 ) < E
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.
Exp(x)
returns the value ex. e is Euler's constant, the base of natural logarithms.
Log(x)
the natural 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.
Sqr(x)
square root of x. x must be non-negative.

Derived Functions

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

 Log(x,base) = Log(x) / Log(base)

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

 RootN(x, n) = x^(1.0/n)

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

 radians = degrees * π / 180
 ArcSin(x) = Atn(x / Sqr(-x * x + 1))
 ArcCos(x) = Atn(-x / Sqr(-x * x + 1)) + 2 * Atn(1)

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

Here are some more:

SecantSec(x) = 1 / Cos(x)
CosecantCosec(x) = 1 / Sin(x)
CotangentCotan(x) = 1 / Tan(x)
Inverse SineArcsin(x) = Atn(x / Sqr(-x * x + 1))
Inverse CosineArccos(x) = Atn(-x / Sqr(-x * x + 1)) + 2 * Atn(1)
Inverse SecantArcsec(x) = Atn(x / Sqr(x * x - 1)) + Sgn((x) - 1) * (2 * Atn(1))
Inverse CosecantArccosec(x) = Atn(x / Sqr(x * x - 1)) + (Sgn(x) - 1) * (2 * Atn(1))
Inverse CotangentArccotan(x) = Atn(x) + 2 * Atn(1)
Hyperbolic SineHSin(x) = (Exp(x) - Exp(-x)) / 2
Hyperbolic CosineHCos(x) = (Exp(x) + Exp(-x)) / 2
Hyperbolic TangentHTan(x) = (Exp(x) - Exp(-x)) / (Exp(x) + Exp(-x))
Hyperbolic SecantHSec(x) = 2 / (Exp(x) + Exp(-x))
Hyperbolic CosecantHCosec(x) = 2 / (Exp(x) - Exp(-x))
Hyperbolic CotangentHCotan(x) = (Exp(x) + Exp(-x)) / (Exp(x) - Exp(-x))
Inverse Hyperbolic SineHArcsin(x) = Log(x + Sqr(x * x + 1))
Inverse Hyperbolic CosineHArccos(x) = Log(x + Sqr(x * x - 1))
Inverse Hyperbolic TangentHArctan(x) = Log((1 + x) / (1 - x)) / 2
Inverse Hyperbolic SecantHArcsec(x) = Log((Sqr(-x * x + 1) + 1) / x)
Inverse Hyperbolic CosecantHArccosec(x) = Log((Sgn(x) * Sqr(x * x + 1) + 1) / x)
Inverse Hyperbolic CotangentHArccotan(x) = Log((x + 1) / (x - 1)) / 2

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

 Public Function Atan2(ByVal X As Double, ByVal Y As Double)
     If Abs(X) < 0.0000001 Then  ' Direct comparison with zero doesn't always work
       Atan2 = Sgn(Y) * 1.5707963267949
     Else
       Atan2 = Atn(Y/X) + Sgn(Y) * 3.14159265358979 
     End If
 End Function
[[../Simple Output|Previous: Simple Output]] Contents [[../Branching|Next: Branching]]


Also helps finding: basicclassicsimple, ProgrammingVisual, VisualBasic, BasicClassic, ClassicSimple, SimpleArithmetic, basi, clasic, simples, bacic, classi, simpel, asic, classis, simle

   
 
  
Add to bookmarks
Top Articles
 
Ada Programming
Ada Programming/Keywords/array
Ada Programming/Keywords/begin
Ada Programming/Keywords/in
Ada Programming/Keywords/is
Ada Programming/Keywords/new
Ada Programming/Keywords/out
Ada Programming/Keywords/pragma
Cookbook:Butter
Cookbook:Cup
Cookbook:Egg
Cookbook:Onion
Cookbook:Salt
Cookbook:Tablespoon
Cookbook:Vegetable
Dichotomous Key
FHSST Physics
Japanese
SA NCS:Life Sciences
SA NCS:Second Additional Language
The Golden Bough
Search LiveJournal blogs for Programming:Visual Basic Classic/Simple Arithmetic
 

Wordpress Themes  •  Free Advertising  •  Wordpress Theme  •  Wordpress Themes •  E-commerce articles

Copyright @ 2005 eMyTextBooks.com
This article is from Wikibooks. All text is available under the terms of the GNU Free Documentation License.