arithmetic in pascal (2)

13
Arithmetic in Pascal (2) Arithmetic Functions

Upload: stacy-potts

Post on 30-Dec-2015

35 views

Category:

Documents


0 download

DESCRIPTION

Arithmetic in Pascal (2). Arithmetic Functions. Arithmetic Functions. Perform arithmetic calculations Gives an argument to the function and it returns the result. Some Arithmetic Functions. Some Arithmetic Functions. sqr(x). Return the square of the argument - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Arithmetic in Pascal (2)

Arithmetic in Pascal (2)

Arithmetic Functions

Page 2: Arithmetic in Pascal (2)

Arithmetic Functions Perform arithmetic calculations Gives an argument to the function

and it returns the result

Page 3: Arithmetic in Pascal (2)

Some Arithmetic Functions

Pascal function

Argument type

Function type

sqr(x) integer or real

same as argument

sqrt(x) integer or real

real

sin(x) integer or real

real

cos(x) integer or real

real

ln(x) integer or real

real

exp(x) integer or real

real

Page 4: Arithmetic in Pascal (2)

Some Arithmetic Functions

Pascal function

Argument type

Function type

random(x)

integer integer

abs(x) integer or real

same as argument

round(x) real integer

trunc(x) real integer

Page 5: Arithmetic in Pascal (2)

sqr(x) Return the square of the argument The type of the result is the same

as the argument D := sqr(2)

D = 4

Page 6: Arithmetic in Pascal (2)

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

Page 7: Arithmetic in Pascal (2)

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

Page 8: Arithmetic in Pascal (2)

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

Xy := exp( y * ln(x) )

Page 9: Arithmetic in Pascal (2)

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)

Page 10: Arithmetic in Pascal (2)

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

Page 11: Arithmetic in Pascal (2)

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

Page 12: Arithmetic in Pascal (2)

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

Page 13: Arithmetic in Pascal (2)

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