Math, Financial, and the Boolean "Is" Functions

 

Math Functions

 

Function

Returns:

Abs(x)

the absolute value of x

Atn(x)

the trigonometric arctangent of x (in radians)

Cos(x)

the trigonometric cosine of x (in radians)

Exp(x)

exponential function ex

Fix(x)

the integer portion of x

Int(x)

the integer portion of x, except that if x is negative, it will return the next smallest number.

For example, Int(-4.3) would return –5, not –4 as you might expect. To get the integer portion of a number, the Fix function will always produce the expected result.

Log(x)

natural logarithm of x (base e)

Round(x, y)

x rounded to y decimal places

Rnd

a random number less than 1 but greater than or equal to zero

Sgn(x)

-1 if x is negative, 0 if x is 0, 1 if x is positive

Sin(x)

the trigonometric sine of x (in radians)

Sqr(x)

the square root of x

Tan(x)

the trigonometric tangent of x (in radians)

 

Tip: How to isolate the decimal portion of a mixed number with Fix:

 

Dim sngMixedNumber As Single

Dim sngDecimalPortion As Single

 

sngDecimalPortion = sngMixedNumber – Fix(sngMixedNumber)

 

 

Generating Random Numbers with Rnd:

 

Use the following formula to generate a random number between a range of numbers (where a range of numbers has a lowerbound and an upperbound – for example, in the range of 1 to 10, 1 is the lowerbound and 10 is the upperbound):

(upperboundlowerbound + 1) * Rnd + lowerbound

 

The Rnd function returns a Single number between 0 and 1 (including 0, but not including 1); therefore, the other factors in the expression are used for scaling the range for the desired lowerbound and upperbound values.

 

For example, to generate a random number between 5 and 25.999999, you can use the expression:

            (25 – 5 + 1) * Rnd + 5

 

which can be simplified to:

            21 * Rnd + 5

 

To store the result in the variable sngRandomNumber, you could write the statement:

            sngRandomNumber = 21 * Rnd + 5

 

Often, you only want to generate a random integer within a certain range.  To accomplish this, use the VB Int function "on top of " the expression involving Rnd.  The Int function truncates the decimal portion of the expression without rounding – unlike CInt, which does round.  The following statements cause a random integer between 1 and 10 to be stored in the variable intRandNum:

 

            intRandomNumber = Int((10 – 1 + 1) * Rnd + 1)

            - or simply -

            intRandomNumber = Int(10 * Rnd + 1)

 

IMPORTANT: Always execute the Randomize statement at least once in the program prior to using Rnd function, otherwise, the same set of random numbers will be generated on every execution of the program.

 

 

Financial Functions

 

To do this:

Use one of these functions:

Calculate depreciation.

DDB, SLN, SYD

Calculate future value.

FV

Calculate interest rate.

Rate

Calculate internal rate of return.

IRR, MIRR

Calculate number of periods.

NPer

Calculate payments.

IPmt, Pmt, PPmt

Calculate present value.

NPV, PV

 

 

Boolean "Is" functions

 

Function

Description

IsDate

Returns a Boolean value indicating whether an expression can be converted to a date. Useful for validating input.

IsNumeric

Returns a Boolean value indicating whether an expression can be evaluated as a number. Can be useful for validating input; but use with caution: if the expression contains the letter "E" or "D", the input argument could be interpreted as a number in scientific notation (thus returning True when you would expect the function to return False).

IsNull

Returns a Boolean value that indicates whether an expression contains no valid data (Null). Usually used with database fields, although a Variant variable can also contain Null (no other intrinsic VB datatype can store Null values).

IsMissing

Returns a Boolean value indicating whether an optional Variant argument has been passed to a procedure.

IsEmpty

Returns a Boolean value indicating whether a Variant variable has been initialized (i.e., if it has  ever been assigned a value).

IsObject

Returns a Boolean value indicating whether a Variant variable represents an object.

IsError

Returns a Boolean value indicating whether a Variant variable contains the special value Error.

IsArray

Returns a Boolean value indicating whether a variable is an array.