Mathematical Functions


The mathematical functions in OOo Basic take a numeric argument. All of the standard types are converted to a Double before they are used. Strings may include hexadecimal and octal numbers . The functions are the same as those available in Visual Basic (see Table 3 ).

Table 3: Mathematical functions supported by OOo Basic.

OOo Basic

VB

VB .NET

Return Value

ABS

ABS

Math.Abs

The absolute value of a specified number.

Exp

Exp

Math.Exp

The base of natural logarithms raised to a power.

Log

Log

Math.Log

The logarithm of a number. In VB .NET you can overload this method to return either the natural (base e) logarithm or that of a specified base.

Sgn

Sgn

Math.Sign

Integer value indicating the sign of a number.

Sqr

Sqr

Math.Sqrt

The square root of a number.

Use the ABS function to determine the absolute value of a number, which you can think of as simply throwing away the leading + or - sign from the front of the number. The geometrical definition of ABS(x) is the distance from x to 0 along a straight line.

 ABS(23.33) = 23.33 ABS(-3)    = 3 ABS("-1")  = 1 'Notice that the string value "-1" is converted to a Double 

Use the Sgn function to determine the sign of a number. An integer with the value -1, 0, or 1 is returned if the number is negative, zero, or positive.

 Sgn(-37.4) = -1 Sgn(0)     = 0 Sgn("4")   = 1 

The square root of 9 is 3, because 3 multiplied by 3 is 9. Use the Sqr function to get the square root of a number. The Sqr function can't calculate the square root of a negative number-attempting to do so causes a run-time error.

 Sqr(100) = 10 Sqr(16)  = 4 Sqr(2)   = 1.414213562371 

Logarithms were devised by John Napier, who lived from 1550 through 1617. Napier devised logarithms to simplify arithmetic calculations, by substituting addition and subtraction for multiplication and division. Logarithms have the following properties:

 Log(x*y) = Log(x) + Log(y) Log(x/y) = Log(x) - Log(y) Log(x^y) = y * Log(x) 

The Exp function is the inverse of the Log function. For example, Exp(Log(4)) = 4 and Log(Exp(2)) = 2. By design, logarithms turn multiplication problems into addition problems. This allows the use of logarithms as they were originally designed.

 Print Exp(Log(12) + Log(3)) '36 = 12 * 3 Print Exp(Log(12) - Log(3)) ' 4 = 12 / 3 

Logarithms are defined by the equation y=b^x. It is then said that the logarithm, base b, of y is x. For example, the logarithm base 10, 10^2 = 100 so the logarithm, base 10, of 100 is 2. The natural logarithm, with a base approximated by e=2.71828182845904523536, is frequently used because it has some nice mathematical properties. This is called the "natural logarithm" and is used in OOo Basic. Visual Basic .NET allows you to calculate logarithms of other bases. This is easily done using the formula that the logarithm base b is given by Log(x)/Log(b), regardless of the base of the logarithm that is used.

Logarithms are not as useful as a general shortcut for calculations today, when lots of computing power is available. However, the logarithmic relationship describes the behavior of many natural phenomena. For example, the growth of populations is often described using logarithms, because geometric growth expressed on a logarithmic graph displays as a straight line. Exponentials and logarithms are also used extensively in engineering computations that describe the dynamic behavior of electrical, mechanical, and chemical systems.

The macro in Listing 9 calculates the logarithm of the number x (first argument) to the specified base b (second argument). For example, use LogBase(8, 2) to calculate the log, base 2, of 8 (the answer is 3).

Listing 9: LogBase is found in the Numerical module in this chapter's source code files as SC03.sxw.
start example
 Function LogBase(x, b) As Double   LogBase = Log(x) / Log(b) End Function 
end example
 



OpenOffice.org Macros Explained
OpenOffice.org Macros Explained
ISBN: 1930919514
EAN: 2147483647
Year: 2004
Pages: 203

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net