arithmetic in pascal (2)
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 PresentationTRANSCRIPT
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
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
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
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
Xy := 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