| << D.2.8- Unsupported operators | AppendixD | D.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
|
|
|
Example |
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
|
|
|
Example |
Dim dblPi
|
|
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- |
|
Example |
Dim dblAngle, dblSecant
Here the Cos function is used to return the cosine of an
angle. |
|
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
Sometimes this function is referred to as the antilogarithm, and complements the action of the Log function. |
|
Example |
Dim dblAngle, dblHSin
dblAngle = 1.3
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
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)
|
|
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)
|
|
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 |
|
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) * _ 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
|
|
|
Example |
Const LNG_UPPER_BOUND = 20 Const LNG_LOWER_BOUND = 1
Dim intValue Dim lngCounter
For lngCounter = 1 To 10 intValue = Int( _ 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
|
|
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
|
|
|
Example |
Round(10.4) ' Returns
10 |
|
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
|
|
|
Example |
Sgn(10.4) ' 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 dblSecant = 11.545
Here the Sin function is used to return the sine of an angle.
|
|
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
|
|
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
|
|
See Also |
Atn, Cos and Sin |
| << D.2.8- Unsupported operators | AppendixD | D.4.0- Date and Time Functions and Statements >> |

RSS
