Page

D.3.0- Math Functions

Created by Brendan Doss.
Last Updated by Jim Minatel.  

PublicCategorized as Appendix D.

Not yet tagged
<< D.2.8- Unsupported operatorsAppendixDD.4.0- Date and Time Functions and Statements >>

Math Functions

Every now and then, depending on what kind of applications you design, you will need to do some math calculations and VBScript goes a long way towards helping you here. There are a number of intrinsic functions, but it is also possible to derive many other math functions from the intrinsic ones. Math functions are especially helpful when you need to display graphics, charts etc; the listing is in alphabetical order.

 

Abs

Returns the absolute value of a number, i.e. its unsigned magnitude.

Syntax

 

Abs(number)

 

number is any valid numeric expression.

Note

 

Null will be returned if number contains Null.

Example

Abs(-50) ' 50
Abs(50) ' 50

 

See Also

Sgn

 

Atn

Returns the arctangent of a number as Variant subtype Double (5).

 

Syntax

 

Atn(number)

 

number is any valid numeric expression.

Note

 

This function takes the ratio of two sides of a right-angled triangle (number) and returns the corresponding angle in radians. The ratio is the length of the side opposite the angle divided by the length of the side adjacent to the angle. The range of the result is -pi/2 to pi/2 radians.

 

Example

Dim dblPi

' Calculate the
' value of Pi
dblPi = 4 * Atn(1)

 

See Also

Cos, Sin and Tan

 

 

Cos

Returns the cosine of an angle as Variant subtype Double (5).

Syntax

 

Cos(number)

 

number is any valid numeric expression that expresses an angle in radians.

Note

 

This function takes an angle and returns the ratio of two sides of a right-
angled triangle. The ratio is the length of the side adjacent to the angle
divided by the length of the hypotenuse (dblSecant). The result is within
the range -1 to 1, both inclusive.

Example

Dim dblAngle, dblSecant
Dim dblLength

dblLength = 10
' Convert 30° to radians
dblAngle = (30 * 3.14 / 180)
dblSecant = dblLength / Cos(dblAngle)

 

Here the Cos function is used to return the cosine of an angle.
543636_pg939


 

See Also

Atn, Sin and Tan

 

Exp

Returns a Variant subtype Double (5) specifying e (the base of natural logarithms) raised to a power.

Syntax

 

Exp(number)

 

number is any valid numeric expression.

Note

 

A runtime error occurs if number is larger than 709.782712893. e
is approximately 2.718282.

 

Sometimes this function is referred to as the antilogarithm, and complements the action of the Log function.

Example

Dim dblAngle, dblHSin

 

dblAngle = 1.3
dblHSin = (Exp( dblAngle) - Exp( -1 * dblAngle)) / 2

 

Here the Exp function is used to return e raised to a power.

See Also

Log

 

 

Fix

Returns the integer part of a number.

Syntax

Fix(number)

Note

 

Fix is internationally aware, which means that the return value
is based on the locale settings on the machine.

 

Null is returned if number contains Null. The data type returned will be decided from the size of the integer part. Possible return data types in ascending order: Integer, Long, and Double.

 

If number is negative, the first negative integer equal to or greater than number is returned.

Example

Dim vntPosValue

Dim vntNegValue

 

vntPosValue = Fix(5579.56)

vntNegValue = Fix(-5579.56)

 

vntPosValue now holds the value 5579, and vntNegValue the value -5579.

 

Fix is the equivalent of Int when dealing with non-negative numbers. When you handle negative numbers, Fix returns the
first negative integer, greater than, or equal to the number supplied.

See Also

Int, Round and the

Conversion Functions CInt and CLng

 

 

Int

Returns the integer part of a number.

 

Syntax

 

Int(number)

 

number is any valid numeric expression.

 

Note

 

Int is internationally aware, which means that the return value is based on the locale settings on the machine.

 

Null is returned if number contains Null. The data type returned will be decided from the size of the integer part. Possible return data types in ascending order: Integer, Long, and Double.

 

If number is negative, the first negative integer equal to or less than number is returned.

 

Example

Dim vntPosValue

Dim vntNegValue

 

vntPosValue = Int(5579.56)

vntNegValue = Int(-5579.56)

 

vntPosValue now holds the value 5579, and vntNegValue the value -5580.

 

Int is the equivalent of Fix when dealing with non-negative numbers. When you handle negative numbers, Int returns the
first negative integer, less than, or equal to the number supplied.

 

See Also

Fix, Round and the

Conversion Functions CInt and CLng

 

 

Log

Returns the natural logarithm of a number.

 

Syntax

 

Log(number)

 

number is any valid numeric expression greater than zero.

 

Example

 

Dim vntValueBase10

 

vntValueBase10 = Log(5) / Log(10)

 

The above sample code calculates the base-10 logarithm of the number 5, which is 0.698970004336019.

 

See Also

Exp

 

Randomize

Initializes the random number generator, by giving it a new seed-value. A seed-value is an initial value used for generating random numbers.

 

Syntax

 

Randomize [number]

 

number is any valid numeric expression.

 

Note

 

You can repeat a sequence of random numbers, by calling the
Rnd function with a negative number, before using the Randomize statement with a numeric argument.

Example

Const LNG_UPPER_BOUND = 20

Const LNG_LOWER_BOUND = 1

 

Dim intValue

Dim lngCounterIn

Dim lngCounterOut

 

For lngCounterOut = 1 To 3

Rnd -1

Randomize 3

For lngCounterIn = 1 To 3

intValue = Int((LNG_UPPER_BOUND - LNG_LOWER_BOUND + 1) * _
Rnd + LNG_LOWER_BOUND)

MsgBox intValue

Next

Next

 

The above sample has an inner loop that generates three random numbers and an outer loop that calls the Rnd function with a negative number, immediately before calling Randomize with an argument. This makes sure that the random numbers generated in the inner loop will be the same for every loop the outer loop performs.

 

See Also

Rnd

 

Rnd

Returns a random number, less than 1 but greater than or equal to 0.

 

Syntax

 

Rnd[(number)]

 

number (Optional) is any valid numeric expression that determines how the random number is generated; if number is:

 

< 0 – uses same number every time,

> 0 or missing – uses next random number in sequence,

= 0 – uses most recently generated number.

 

Note

 

Use the Randomize statement, with no argument, to initialize the random-number generator with a seed based on the system timer, before calling Rnd.

 

The same number sequence is generated for any given initial seed, because each successive call to Rnd uses the previous number as the seed for the next number in the sequence.

 

Call Rnd with a negative argument immediately before using Randomize with a numeric argument, in order to repeat sequences of random numbers.

Example

Const LNG_UPPER_BOUND = 20

Const LNG_LOWER_BOUND = 1

 

Dim intValue

Dim lngCounter

 

For lngCounter = 1 To 10

intValue = Int( _
(LNG_UPPER_BOUND - _
LNG_LOWER_BOUND + 1) * _
Rnd + LNG_LOWER_BOUND)

MsgBox intValue

Next

 

This produces 10 random integers in the range 1-20.

See Also

Randomize

 

Round

Returns a number rounded to a specified number of decimal places as
a Variant subtype Double (5).

 

Syntax

 

Round(number, [numdecimalplaces])

 

number is any valid numeric expression.

 

numdecimalplaces, (Optional) indicates how many places to the right of the decimal separator should be included in the rounding.

 

Note

 

An integer is returned if numdecimalplaces is missing.

Example

Round(10.4) ' Returns 10
Round(10.456) ' Returns 10
Round(-10.456) ' Returns –10
Round(10.4, 1) ' Returns 10.4
Round(10.456, 2) ' Returns 10.46
Round(-10.456, 2) ' Returns –10.46

See Also

Int and Fix

 

Sgn

Returns an integer indicating the sign of a number.

 

Syntax

 

Sgn(number)

 

number is any valid numeric expression.

 

Note

 

Sgn returns the following when number is:

 

< 0 – -1

= 0 – 0

> 0 – 1

 

Example

Sgn(10.4) ' Returns 1
Sgn(0) ' Returns 0
Sgn(-2) ' Returns -1

 

See Also

Abs

 

Sin

Returns a Variant subtype Double (5) specifying the sine of an angle.

 

Syntax

 

Sin(number)

 

number is any valid numeric expression that expresses an angle in radians.

 

Note

 

This function takes an angle and returns the ratio of two sides of a right-angled triangle. The ratio is the length of the side opposite the angle (dblCosecant) divided by the length of the hypotenuse (dblSecant). The result is within the range -1 to 1, both inclusive.

Example

Dim dblAngle, dblCosecant
Dim dblSecant

dblSecant = 11.545
' Convert 30° to radians
dblAngle = (30 * 3.14 / 180)
dblCosecant = dblSecant * Sin(dblAngle)

 

Here the Sin function is used to return the sine of an angle.

 

543636_pg944.jpg

 

See Also

Atn, Cos and Tan

 

Sqr

Returns the square root of a number.

 

Syntax

 

Sqr(number)

 

number is any valid numeric expression greater than or equal to zero.

 

Example

 

Sqr(16) ' Returns 4

 

Tan

Returns a Variant subtype Double (5) specifying the tangent of an
angle.

 

Syntax

 

Tan(number)

 

number is any valid numeric expression that expresses an angle in radians.

Note

 

This function takes an angle and returns the ratio of two sides of a right-angled triangle. The ratio is the length of the side opposite the angle (dblCosecant) divided by the length of the side adjacent to the angle (dblLength), - see diagram of Sin function.

 

The result is within the range -1 to 1, both inclusive.

Example

Tan(10.4) ' Returns 1.47566791425166
Tan(0) ' Returns 0
Tan(-2) ' Returns 2.18503986326152

 

See Also

Atn, Cos and Sin

<< D.2.8- Unsupported operatorsAppendixDD.4.0- Date and Time Functions and Statements >>

Copyright © 2003 by Wiley Publishing, Inc.

Powered by Near-TimeTerms of Services | Privacy Policy | Security Policy |