Clilstore Facebook WA Linkedin Email
Login

This is a Clilstore unit. You can link all words to dictionaries.

Operations. Standard functions

The expression means a structure composed of constants, variables, operator symbols, functions, brackets. The expression defines the procedure for calculation of the certain value. Operation applied to one operand is called unary and operation with two operands - binary.

Arithmetic operations

Binary arithmetic operations include:

+   addition

-    subtraction

*    multiplication

/     division

% division modulo; (this operation is similar to operation mod in Pascal - the remainder of the division). Operation % applies only to integer numbers

Unary operations include:

++  increase by one (increment);

- -   decrease by one (decrement);

For example: x=x+1, x++, ++x   the result of the two operations is the same.

Both operation signs can be recorded mark both before operand (prefix form) and after operand (postfix form), for example ++хorх++,––хorх––. When using postfix form, the operations ++ and - - are performed after the value of the variable was used in the expression and the prefix operation - until use.

For example: 1) x=5; y=x++; firstly, value х is assigned to variable у, after value хis increased by 1. Initial value х=5, after performance of operations y=5, x=6.

2) x=5; y=++x; result of operation x=6, y=6.

3) a=3; c=2; x=a++ * c++; result of operation a=4,  c=3,  x=6.

4) a=3; c=2; x=++a * ++c; result of operation a=4,  c=3,  x=12.

 

Relational operations

C uses the same set of relational operations, as in other programming languages:

<           less

<=         less or equal

>           greater

>=         greateror equal

==         equal

!=          not equal

The difference is in the recording of operations “equal” and “not equal”.

Therefore, the result of the relational operation is the integer number: if the relation is true –then 1, if false - then 0.

For example: the result of the relational operation in case 5>1, ‘x’==’x’ will equal 1, and in case 5!=5,  ‘x’==’X’ result=0.

        

Logical operations

Basic logical operations in С++ language will be recorded as follows:

&&  - conjunction (logic “AND”)

||      - disjunction (logic“OR”)

!      - inversion (logic“NOT”)

Table 2 below shows examples of recording expressions in C ++ language using logical operations and relational operations.

Table 2. Recording of expressions

mathematical expression

recording inС++

0<x<10

x>0 && x<10

y≤-1 или y≥1

y<=-1 || y>=1

x≠y

!(x= =y)

a=b

a= =b

‘a’≠’A’

‘a’!=’A’

Priority to perform logical operations and relational operations as follows:

                 

Assignment operation

General view of the assignment operation as follows:

Name of variable = expression;

For example: x=5;  D=b*b-4*a*c;    a=b=0;

Assignment operation in C language is not operand, i.e. assignment as any other sign of operation can several times enter the expression.

For example: а=b=c=x+y;

Also in С++ language, assignment operation can be combined with other operations:   +=,  -=,  /=,  *=,  %=

For example: The following expressions are considered as equivalent.

i+=2   and    i=i+2

p/=5   and    p=p/5

a*=b+c      and    a=a*(b+c)

x-=y    andx=x-y

n%2    andn=n%2

Instead of expression х=х+2 it is preferable to use recordх+=2, since the second expression will be calculated faster. 

Operation“condition”

This is the only operation that has three operands. The format of the operation:

logical operation ? expression -1: expression -2;

or

variable = logical operation? expression -1: expression -2;

This operation implements the algorithmic branch structure. The algorithm of its performance is as follows: the value of the logical expression shall be calculated the first, if it is true, then the expression is calculated - 1 and the obtained result becomes as the result of the operation. Otherwise, the value of the expression-2 is taken as the result. 

For example:

x<0 ? –x:  x;

max=(a<=b) ? b : a;

a>b ? 2*a: 2*b;

 

Functions that are calculated automatically by special modules are called standard functions. They are stored in the header file math.h

Standard functions ofС++ language are shown in Table 3.

 

Table 3. Standard functions ofС++ language

Mathematical function

Record onС++

Argument type

Result type

Note

| x |

abs (x)

int

int

absolute value of integer number

| x |

fabs (x)

double

double

absolute value of real number

sinx

sin(x)

double

double

х- radian

cosx

cos(x)

double

double

х- radian

tgx

tan(x)

double

double

х- radian

e x

exp(x)

double

double

e = 2,718 …

lnx

log(x)

double

double

natural logarithm, x>0

lg(x)

log10(x)

double

double

decimal logarithm

 

sqrt(x)

double

double

square root, x>=0

xy

pow(x,y)

double, double

double

x in power of y

10p

pow10(p)

int

double

10in power of p

arcsinx

asin(x)

double

double

х- radian

arccosx

acos(x)

double

double

х- radian

arctgx

atan(x)

double

double

х- radian

sinhx

sinh(x)

double

double

hyperbolic sine,х- radian

coshx

cosh(x)

double

double

hyperbolic cosine,х- radian

tanhx

tanh(x)

double

double

hyperbolic tangent, х- radian

nearest integer, not less х

ceil(x)

double

double

the result will be displayed in format double

greatest integer, not exceeding х

floor(x)

double

double

the result will be displayed in format double

remainder of the division of integer хonу

fmod(x,y)

double, double

double

the result will be displayed in format double

z2=x2+y2

hypot(x,y)

double, double

double

calculates hypotenuse of the rectangular triangle according tolegs value z2=x2+y2

 

All other functions are expressed through the standard. Examples of recording of mathematical expressions in C ++ language:

  1. x 5® pow(x,5)
  2. Ö x 3 = x 3/2® pow(x, 3/2)
  3. sin 30° ® sin ((3.14*30)/180) (transfer to radians)
  4. log2 x ® log a b = log cb / logca ®       log(x)/log(2)
  5. 4Ö 2x-y ® 4* sqrt(2*x-y)
  6. ctgx+cos60º ® cos(x)/sin(x)+cos((60*3.14)/180)
  7. ® (-b+ sqrt(D))/(2*a)

 

  1. cos2x2® pow(cos(x*x),2)

9. e sinx®     exp (sin(x))  

Clilstore

Short url:   https://clilstore.eu/cs/6245