To learn more about author Sanjaya Hettihewa, please visit the author's homepage.
Mathematical functions are used to perform complex calculations that cannot be easily performed using simple mathematical operators. This chapter covers mathematical functions of the VBScript language by discussing the syntax of VBScript functions and how they can be used to perform complex calculations. By the end of this chapter, you will know how to develop a VBScript application that uses all the VBScript mathematical functions discussed next.
Tip: Experiment with the application at the end of this chapter to become more familiar with VBScript mathematical functions. The application at the end of this chapter can be found in the CD-ROM kit that accompanies this resource library (\Chap-19\Math.html).
Most VBScript mathematical functions are used to perform trigonometric calculations. The syntax and graphs of VBScript mathematical functions are discussed next.
Note: Degrees can be converted to radians by multiplying the degrees by Pi/180. Multiply radians by 180/Pi to convert radians to degrees.
Atn() returns the arc tangent of a number. See Figure 19.1 for a graph of the
Atn() function.
Figure 19.1. Graph of the Atn() function.
Cos() returns the cosine of an angle passed to the function. See Figure 19.2 for
a graph of the Cos() function.
Figure 19.2. Graph of the Cos()
function.
Sin() returns the sine of an angle. For example, Sin (Pi) returns 0. See Figure
19.3 for a graph of the Sin() function.
Figure 19.3. Graph of the Sin()
function.
The Tan() function can be used to calculate the tangent of an angle. For example,
Tan (0) returns 0. See Figure 19.4 for a graph of the Tan() function.
Figure 19.4. Graph of the Tan()
function.
Exp() returns the value of e raised to a power. For example Exp(1) returns 2.71828182845905.
See Figure 19.5 for a graph of the Exp() function.
Figure 19.5. Graph of the
Exp() function.
Log() returns the natural logarithm of a non-negative, numeric expression. See
Figure 19.6 for a graph of the Log() function.
Figure 19.6. Graph of the Log()
function.
Sqr() returns the square root of a non-negative, numeric expression. See Figure
19.7 for a graph of the Sqr() function.
Figure 19.7. Graph of the Sqr()
function.
Randomize() can be used to initialize the random-number generator, and can be used either with or without a numeric argument. If it is used with a numeric argument, the numeric argument is used to seed the random-number generator. If it is used without an argument, a number from the system clock is used to seed the random-number generator. You must seed the random-number generator before attempting to generate any random numbers. Random numbers can be used in Web pages. For example, a random number can be used to greet the user with a random greeting or tip of the day.
Rnd() returns a random number between 1 and 0. Be sure to seed the random-number generator by calling Randomize() before using the Rnd() function.
Mathematical functions discussed in the previous sections can be used in VBScript applications as demonstrated in Listing 19.1. You might want to experiment with the application shown in Figure 19.8 to become familiar with VBScript mathematical functions. Enter a value in a text box and click the Evaluate button to see the result shown in Figure 19.9.
1: <HTML> 2: <HEAD> 3: <TITLE>Using VBScript Mathematical Functions</TITLE> 4: </HEAD> 5: <BODY BGCOLOR="FFFFFF"> 6: <form name="SimpleCalculator"> 7: <H3> 8: Atn (<input type="text" size="5" name="AtnInput" value="1">) 9: <input type="button" name="AtnEvaluate" value="Evaluate!"><P> 10: Cos (<input type="text" size="5" name="CosInput" value="1">) 11: <input type="button" name="CosEvaluate" value="Evaluate!"><P> 12: Sin (<input type="text" size="5" name="SinInput" value="1">) 13: <input type="button" name="SinEvaluate" value="Evaluate!"><P> 14: Tan (<input type="text" size="5" name="TanInput" value="1">) 15: <input type="button" name="TanEvaluate" value="Evaluate!"><P> 16: Exp (<input type="text" size="5" name="ExpInput" value="1">) 17: <input type="button" name="ExpEvaluate" value="Evaluate!"><P> 18: Log (<input type="text" size="5" name="LogInput" value="1">) 19: <input type="button" name="LogEvaluate" value="Evaluate!"><P> 20: Sqr (<input type="text" size="5" name="SqrInput" value="1">) 21: <input type="button" name="SqrEvaluate" value="Evaluate!"><P> 22: </H3> 23: </form> 24: <SCRIPT LANGUAGE="VBScript"> 25: <!-- 26: Sub AtnEvaluate_OnClick 27: MsgBoxString = "Atn (" & SimpleCalculator.AtnInput.Value & _ 28: ") = " & Atn (SimpleCalculator.AtnInput.Value) 29: MsgBox MsgBoxString , 64 , "Result of Calculation" 30: End Sub 31: Sub CosEvaluate_OnClick 32: MsgBoxString = "Cos (" & SimpleCalculator.CosInput.Value & _ 33: ") = " & Cos (SimpleCalculator.CosInput.Value) 34: MsgBox MsgBoxString , 64 , "Result of Calculation" 35: End Sub 36: Sub SinEvaluate_OnClick 37: MsgBoxString = "Sin (" & SimpleCalculator.SinInput.Value & _ 38: ") = " & Sin (SimpleCalculator.SinInput.Value) 39: MsgBox MsgBoxString , 64 , "Result of Calculation" 40: End Sub 41: Sub TanEvaluate_OnClick 42: MsgBoxString = "Tan (" & SimpleCalculator.TanInput.Value & _ 43: ") = " & Tan (SimpleCalculator.TanInput.Value) 44: MsgBox MsgBoxString , 64 , "Result of Calculation" 45: End Sub 46: Sub ExpEvaluate_OnClick 47: MsgBoxString = "Exp (" & SimpleCalculator.ExpInput.Value & _ 48: ") = " & Exp (SimpleCalculator.ExpInput.Value) 49: MsgBox MsgBoxString , 64 , "Result of Calculation" 50: End Sub 51: Sub LogEvaluate_OnClick 52: MsgBoxString = "Log (" & SimpleCalculator.LogInput.Value & _ 53: ") = " & Log (SimpleCalculator.LogInput.Value) 54: MsgBox MsgBoxString , 64 , "Result of Calculation" 55: End Sub 56: Sub SqrEvaluate_OnClick 57: MsgBoxString = "Sqr (" & SimpleCalculator.SqrInput.Value & _ 58: ") = " & Sqr (SimpleCalculator.SqrInput.Value) 59: MsgBox MsgBoxString , 64 , "Result of Calculation" 60: End Sub 61: --> 62: </SCRIPT> 63: </BODY> 64: </HTML>
Figure 19.8. The VBScript application
in Listing 19.1.
Figure 19.9. Calculating the value of
Sin(0).
Mathematical functions of the VBScript language are used to perform complex calculations--especially calculations that involve trigonometric functions. You were shown the syntax of VBScript mathematical functions, their graphs, and how they are used in VBScript applications to perform calculations.
© Copyright, Macmillan Computer Publishing. All rights reserved.