Mathematic for VB

Embed Size (px)

Citation preview

  • 8/13/2019 Mathematic for VB

    1/9

    Abs Function

    Returns a value of the same type that is passed to it specifying the absolute value of anumber.

    Syntax

    Abs(number)

    The required numberargumentcan be any valid numeric expression. If numbercontains

    Null, Nullis returned; if it is an uninitialized variable, zero is returned.

    Remarks

    The absolute value of a number is its unsigned magnitude. For example, ABS( - 1) and

    ABS( 1) both return 1.

    Abs Function Example

    This example uses the Absfunction to compute the absolute value of a number.

    Di m MyNumberMyNumber = Abs(50. 3) ' Ret ur ns 50. 3.MyNumber = Abs(- 50. 3) ' Ret ur ns 50. 3.

    Atn Function

    Returns a Doublespecifying the arctangent of a number.

    Syntax

    Atn(number)

    The required numberargumentis a Doubleor any valid numeric expression.

    Remarks

    The Atnfunction takes the ratio of two sides of a right 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.

  • 8/13/2019 Mathematic for VB

    2/9

    The range of the result is -pi/2 to pi/2 radians.

    To convert degrees to radians, multiply degrees by pi/180. To convert radians to degrees,

    multiply radians by 180/pi.

    Note Atnis the inverse trigonometric function of Tan, which takes an angle as itsargument and returns the ratio of two sides of a right triangle. Do not confuse Atnwith

    the cotangent, which is the simple inverse of a tangent (1/tangent).

    Atn Function Example

    This example uses the Atnfunction to calculate the value of pi.

    Di m pipi = 4 * Atn(1) ' Cal cul at e t he val ue of pi .

    Cos Function

    Returns a Doublespecifying the cosine of an angle.

    Syntax

    Cos(number)

    The required numberargumentis a Doubleor any valid numeric expressionthat

    expresses an angle in radians.

    Remarks

    The Cosfunction takes an angle and returns the ratio of two sides of a right triangle. Theratio is the length of the side adjacent to the angle divided by the length of the

    hypotenuse.

    The result lies in the range -1 to 1.

    To convert degrees to radians, multiply degrees bypi/180. To convert radians to degrees,

    multiply radians by 180/pi.

    Cos Function Example

    This example uses the Cosfunction to return the cosine of an angle.

  • 8/13/2019 Mathematic for VB

    3/9

    Di m MyAngl e, MySecantMyAngl e = 1. 3 ' Def i ne angl e i n r adi ans.MySecant = 1 / Cos(MyAngl e) ' Cal cul at e secant .

    Exp Function

    Returns a Doublespecifying e(the base of natural logarithms) raised to a power.

    Syntax

    Exp(number)

    The required numberargumentis a Doubleor any valid numeric expression.

    Remarks

    If the value of numberexceeds 709.782712893, an error occurs. The constanteis

    approximately 2.718282.

    Note The Expfunction complements the action of the Logfunction and is sometimes

    referred to as the antilogarithm.

    Exp Function Example

    This example uses the Expfunction to return eraised to a power.

    Di m MyAngl e, MyHSi n' Def i ne angl e i n r adi ans.MyAngl e = 1. 3' Cal cul at e hyper bol i c si ne.MyHSi n = ( Exp(MyAngl e) - Exp(- 1 * MyAngl e)) / 2

    Int, Fix Functions

    Returns the integer portion of a number.

    Syntax

    Int(number)

  • 8/13/2019 Mathematic for VB

    4/9

    Fix(number)

    The required numberargumentis a Doubleor any valid numeric expression. If number

    containsNull, Nullis returned.

    Remarks

    Both Intand Fixremove the fractional part of numberand return the resulting integer

    value.

    The difference between Intand Fixis that if numberis negative, Intreturns the firstnegative integer less than or equal to number,whereas Fixreturns the first negative

    integer greater than or equal to number.For example, Intconverts -8.4 to -9, and Fix

    converts -8.4 to -8.

    Fix(number)is equivalent to:

    Sgn( number) * I nt ( Abs(number) )

    Int Function, Fix Function Example

    This example illustrates how the Intand Fixfunctions return integer portions of

    numbers. In the case of a negative number argument, the Intfunction returns the firstnegative integer less than or equal to the number; the Fixfunction returns the first

    negative integer greater than or equal to the number.

    Di m MyNumber

    MyNumber = Int(99. 8) ' Ret ur ns 99.MyNumber = Fix(99. 2) ' Ret ur ns 99.

    MyNumber = Int(- 99. 8) ' Ret ur ns - 100.MyNumber = Fix(- 99. 8) ' Ret ur ns - 99.

    MyNumber = Int(- 99. 2) ' Ret ur ns - 100.MyNumber = Fix(- 99. 2) ' Ret ur ns - 99.

    Log Function

    Returns a Doublespecifying the natural logarithm of a number.

    Syntax

    Log(number)

  • 8/13/2019 Mathematic for VB

    5/9

    The required numberargumentis a Doubleor any valid numeric expressiongreater than

    zero.

    Remarks

    The natural logarithm is the logarithm to the base e. The constanteis approximately2.718282.

    You can calculate base-nlogarithms for any number xby dividing the natural logarithm

    of xby the natural logarithm of nas follows:

    Logn(x)= Log(x) / Log(n)

    The following example illustrates a custom Functionthat calculates base-10 logarithms:

    St at i c Funct i on Log10( X)Log10 = Log( X) / Log( 10#)

    End Funct i on

    Log Function Example

    This example uses the Logfunction to return the natural logarithm of a number.

    Di m MyAngl e, MyLog' Def i ne angl e i n r adi ans.MyAngl e = 1. 3' Cal cul at e i nver se hyper bol i c si ne.MyLog = Log(MyAngl e + Sqr ( MyAngl e * MyAngl e + 1) )

    Rnd Function

    Returns a Single containing a random number.

    Syntax

    Rnd[(number)]

    The optional numberargumentis a Singleor any valid numeric expression.

    Return Values

    If numberis Rnd generates

  • 8/13/2019 Mathematic for VB

    6/9

    Less than zero The same number every time, using numberas the seed.

    Greater than zero The next random number in the sequence.

    Equal to zero The most recently generated number.

    Not supplied The next random number in the sequence.

    Remarks

    The Rndfunction returns a value less than 1 but greater than or equal to zero.

    The value of numberdetermines how Rndgenerates a random number:

    For any given initial seed, the same number sequence is generated because eachsuccessive call to the Rndfunction uses the previous number as a seed for the next

    number in the sequence.

    Before calling Rnd,use the Randomizestatement without an argument to initialize the

    random-number generator with a seed based on the system timer.

    To produce random integers in a given range, use this formula:

    I nt ( ( upper bound - l owerbound + 1) * Rnd + l owerbound)

    Here, upperboundis the highest number in the range, and lowerboundis the lowest

    number in the range.

    Note To repeat sequences of random numbers, call Rndwith a negative argument

    immediately before using Randomizewith a numeric argument. Using Randomizewith

    the same value for numberdoes not repeat the previous sequenc

    Rnd Funct ion Example

    This example uses the Rndfunction to generate a random integer value from 1 to 6.

    Di m MyVal ueMyVal ue = I nt ( ( 6 * Rnd) + 1) ' Generate r andom val ue bet ween 1 and

    6.

    Sgn Function

  • 8/13/2019 Mathematic for VB

    7/9

    Returns a Variant(Integer) indicating the sign of a number.

    Syntax

    Sgn(number)

    The required numberargumentcan be any valid numeric expression.

    Return Values

    If numberis Sgn returns

    Greater than zero 1

    Equal to zero 0

    Less than zero -1

    Remarks

    The sign of the numberargument determines the return value of the Sgnfunction.

    Sgn Funct ion Example

    This example uses the Sgnfunction to determine the sign of a number.

    Di m MyVar1, MyVar2, MyVar3, MySi gnMyVar1 = 12: MyVar 2 = - 2. 4: MyVar 3 = 0MySi gn = Sgn(MyVar 1) ' Ret ur ns 1.MySi gn = Sgn(MyVar 2) ' Ret ur ns - 1.MySi gn = Sgn(MyVar 3) ' Ret ur ns 0.

    Sin Function

    Returns a Doublespecifying the sine of an angle.

    Syntax

    Sin(number)

    The required numberargumentis a Doubleor any valid numeric expressionthat

    expresses an angle in radians.

  • 8/13/2019 Mathematic for VB

    8/9

    Remarks

    The Sinfunction takes an angle and returns the ratio of two sides of a right triangle. The

    ratio is the length of the side opposite the angle divided by the length of the hypotenuse.

    The result lies in the range -1 to 1.

    To convert degrees to radians, multiply degrees bypi/180. To convert radians to degrees,

    multiply radians by 180/pi.

    Sin Funct ion Example

    This example uses the Sinfunction to return the sine of an angle.

    Di m MyAngl e, MyCosecantMyAngl e = 1. 3 ' Def i ne angl e i n r adi ans.MyCosecant = 1 / Sin(MyAngl e) ' Cal cul at e cosecant .

    Sqr Function

    Returns a Doublespecifying the square root of a number.

    Syntax

    Sqr(number)

    The required numberargumentis a Doubleor any valid numeric expressiongreater than

    or equal to zero.

    Sqr Function Example

    This example uses the Sqrfunction to calculate the square root of a number.

    Di m MySqrMySqr = Sqr(4) ' Ret ur ns 2.

    MySqr = Sqr(23) ' Ret urns 4. 79583152331272.MySqr = Sqr(0) ' Ret ur ns 0.MySqr = Sqr(- 4) ' Gener at es a r un- t i me er r or .

    Tan Function

  • 8/13/2019 Mathematic for VB

    9/9

    Returns a Doublespecifying the tangent of an angle.

    Syntax

    Tan(number)

    The required numberargumentis a Doubleor any valid numeric expressionthat

    expresses an angle in radians.

    Remarks

    Tantakes an angle and returns the ratio of two sides of a right triangle. The ratio is the

    length of the side opposite the angle divided by the length of the side adjacent to the

    angle.

    To convert degrees to radians, multiply degrees bypi/180. To convert radians to degrees,

    multiply radians by 180/pi.

    Tan Funct ion Example

    This example uses the Tanfunction to return the tangent of an angle.

    Di m MyAngl e, MyCot angentMyAngl e = 1. 3 ' Def i ne angl e i n r adi ans.MyCot angent = 1 / Tan(MyAngl e) ' Cal cul at e cot angent .