# Arithmetic in Pascal (2) Arithmetic Functions Perform arithmetic calculations Gives an argument to the function and it returns the result.

## Presentation on theme: "Arithmetic in Pascal (2) Arithmetic Functions Perform arithmetic calculations Gives an argument to the function and it returns the result."— Presentation transcript:

Arithmetic in Pascal (2) Arithmetic Functions

Perform arithmetic calculations Gives an argument to the function and it returns the result

Some Arithmetic Functions Pascal function Argument type Function type sqr(x)integer or realsame as argument sqrt(x)integer or realreal sin(x)integer or realreal cos(x)integer or realreal ln(x)integer or realreal exp(x)integer or realreal

Some Arithmetic Functions Pascal function Argument type Function type random(x)integer abs(x)integer or realsame as argument round(x)realinteger trunc(x)realinteger

sqr(x) Return the square of the argument The type of the result is the same as the argument D := sqr(2) D = 4

sqrt(x) Return the square root of the argument The type of the result is always real The function type is real even when the result is a rounded number D := sqrt(9) D = 3.0

sin(x), cos(x) Return the sine and cosine of the argument The type of the result is always real The argument should be in radians, not degree Use Degree * Pi / 180 to calculate the radians

ln(x), exp(x) Return the value of ln and exp like the same function in your calculator The type of the result is always real X y := exp( y * ln(x) )

random(x) Return a random number between 0 and the argument – 1 The type of the result is integer Exception When you use random with no argument, it returns a number of type real ranged from 0 to 1 (but not include 1)

random(x) (cont.) Try to write a program to generate three random number Run the program for a few times Something strange !! Try to add a line randomize; before using random(x) This procedure randomize the random number generator

abs(x) Return the absolute value (positive value) of the argument The type of the result is the same as the type of input argument

round(x) Return the value of the argument rounded to the nearest integer The type of the result is always integer round(10.5) = 11 round(10.4) = 10 round(-10.5) = -11 round(-10.4) = -10

trunc(x) Return the value of the argument rounded to the nearest integer towards zero Or we could say everything after the decimal point is truncated trunc(10.9) = 10 trunc(-10.9) = 10

Download ppt "Arithmetic in Pascal (2) Arithmetic Functions Perform arithmetic calculations Gives an argument to the function and it returns the result."

Similar presentations