|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
2-3 Standard Arithmetic FunctionsPascal includes a large number of predefined functions that may be used in expressions among constants and variables. Table 2-3 shows the standard arithmetic functions divided into three groups:
Any function operates on a parameter that comes inside its parentheses. The parameter is an expression of a specific type (notice that the expression may be a single variable or constant). Before using any of these functions, you must know the type of parameter the function uses and the type of the returned value (which is also the type of the function). The conversion functions, for instance, take real parameters and return integer results. Other functions use either integer or real parameters, and produce different types. The type of the returned value is important when you assign the function to a variable.
Look at these examples: SQR(3)=9 SQR(2.5)=6.25 SQRT(9)=3.00 ABS(-28.55)=28.55 LN(EXP(1))=1.00 ARCTAN(1)=45 degrees (see the note below) Note that the type of result returned by the function SQR is the same as the type of the parameter, but the function SQRT returns a real number regardless of the parameter type. Notice also that the parameter of any function may contain another function, such as LN(EXP(1)). The output returned from the last function (ARCTAN) is here converted to degrees but will come in radians if not converted. The program which produced these results is shown in figure 2-1. Pay attention to the format descriptors, which are used to produce the output in these formats. { ----------------------------- figure 2-1 ------------------------------- } { Arithmetic Standard Functions } PROGRAM FunctionDemo(OUTPUT); CONST Pi = 3.14159; { No need for this part in Turbo Pascal } BEGIN WRITELN('SQR(3)=',SQR(3)); WRITELN('SQR(2.5)=',SQR(2.5):0:2); { Notice the format } WRITELN('SQRT(9)=',SQRT(9):0:2); WRITELN('ABS(-28.55)=',ABS(-28.55):0:2); WRITELN('LN(EXP(1))=',LN(EXP(1)):0:2); WRITELN('ARCTAN(1)=',ARCTAN(1)* 180/Pi:0:0,' degrees') { Notice the conversion and the format } END. Example: The Power FunctionThe power operator does not exist in Pascal as it does in some other languages (such as FORTRAN and BASIC), but you can make one using arithmetic functions. You can, of course, use the function SQR to produce small powers, thus:
You can also make use of the following mathematical relationship to express any power: x = EXP(LN(x) * y) In the following program this expression is used to raise a number to any power. The program asks you to enter both the base x and the exponent y, then displays the formatted result. { ------------------------------ figure 2-2 ------------------------------} { Arithmetic Standard Functions } PROGRAM PowerOperator(INPUT,OUTPUT); VAR a, b:REAL; BEGIN WRITE('Enter the base and the exponent separated by a space:'); READLN(a,b); WRITELN('The value of ',a:0:2,' raised to the power ',b:0:2,' is ', EXP(LN(a)*b):0:2) END. A sample run of the program gives the following: Enter the base and the exponent separated by a space:2 10 The value of 2.00 raised to the power 10.00 is 1024.00
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |