[̲] [] [] [ ]

 

5.2.

 

, , AB, . , . , ' , , - . ³ , . , , , , , . ³ . , , - , . , .

, .

 

5.2.1.

 

N - , m Î N. m -

 

N = {n Î N: n <= m}.

m

N

m

. n, k Î N ,

m

 

n + k = n+k <= m n+k

m (5.1)

{ };

 

n * k = n*k <= m n*k

m (5.2)

{ };

 

n - k = n >= k n-k

m (5.3)

{ };

 

div mod:

 

n = k * (n div k) + (n mod k), (5.4)

0 <= (n mod k) < k. (5.5)

 

.

m - . m - , m , Nm, (5.1)-(5.5)

 

{ Nm; +, *, -, div, mod; =, <>, >, >=, <, <= },

.

, AB , , ABN,

 

5.3.

 

M, N, U, V: ;

(M, N);

U <- M; V <- N;

U<>V

U<V V <- V-U

U <- U-V

;

(U)

.

 

5.2.2. ֳ

 

n - , Z - . n -

 

Z = {z Î Z: z <= n}.

n

 

m, k Î Z

n

 

m + k = m+k <= n m+k

n (5.6)

( );

 

m * k = m*k <= n m*k

n (5.7)

( );

 

m - k = m-k <= n m-k

n (5.8)

( );

 

m div k = sign(m) * sign(k) * (m div k); (5.9)

m mod k = m - (m div k) * k. (5.10)

 

n - , n , Zn, (5.6)-(5.10)

 

{ Zn; +, *, -, div, mod; =, <>, >, >=, <, <= }.

, , ABZ. ' ABN ABZ, ABNZ. .

, Z N. . , g: -> , g() = g() = " , Î A.

 

5.4. m< n, Nm Ì Zn,

 

g: N -> Z ,

m n

, - i, j Î Nm

 

g(i + j) = g(i) + g(j);

m n

 

g(i * j) = g(i) * g(j);

m n

 

g(i - j) = g(i) - g(j);

m n

 

g(i div j) = g(i) div g(j);

 

g(i mod j) = g(i) mod g(j).

 

.

 

5.2.3. ij

 

- , Î R t=round(, , b). AB - b q - float () = (f, r), -

, b, q

 

r-q

f * b = t; (5.11)

 

-1

b <= f < 1; (5.12)

 

b * f - . (5.13)

 

f , r - . (5.12) (5.13) .

(5.12) , (5.13) , f . ,

 

float (0.02241383) = (0.22414,4);

5,10,5

 

float (2997934580) = (0.299793,10);

6,10,0

 

 

float (-3.14159265) = (-0.3142,9).

4,10,8

 

(p,b,q,d) - p - b q d x Î D(q,d):

 

R = { (f,r): (f,r)=float (x); x Î D(q,d); r Î N },

p,b,q,d p,b,q d

 

D(q, d) (5.12) (5.13)

r-q r-q d-q

f * b < b <= b .

 

r-q -1 r-q -q-1

f * b >= b * b >= b .

 

,

-q-1 d-q

D(q, d) = {0} È { Î R: = b <= < b = }.

min max

 

U = {: > }

max

,

 

V = {: < } \ {0}

min

- .

 

-

 

+. = + = 0 0

+ <

max

 

+ >= +

min (5.14)

 

{}

{}.

 

³, .

 

ij R, b, q, d, (5.14)

 

{ R, b, q, d;+, -, *, /; =, <>, >, >=, <, <= }.

 

' Zn R, b, q, d.

 

5.4. n <= bp, round(n, , b) = n.

. n= bp, 5.3 5.1 .

r-1 r

n< bp. r , b < n < b.

 

r-p p-r

round(n, , b) = sign(n) * b * [b * n + 1/2] =

 

r-p p-r

= sign(n) * b * b * n = n,

 

r-1 r p-r

b < b , b < b b > 1.

 

min(, d)-q

5.5. n < b, .

 

float () Z R : Z -> R ,

, b, q n , b, q, d n , b, q, d

 

, .

. < d, w - ,

p-q

. b < b , - u, v Î Zn , u w v< n g round.

5.4

 

g(u) w g(v) = round(u, , b) w round(v, , b) = u w v =

 

= round(u w v, , b) = g(u w v).

 

d-q p-q

d < , b < b < b .

R ( ) .

ABRZN, .

 

---------------

ABRZN C + IF + Lw

L--------------

^ ^

----------------------------

------v------ ---------------------v-------

----- ----- ----- -----

B N Z R

L---- L---- L---- L----

L----- -- L--------------- --------

 

ABRZ, : , , , .

 

5.4. ABRZ - e>0 =sin x, :

3 5 2k+1

k

= - -- + -- - ... + (-1) * ------- + ...

3! 5! (2k+1)!

3.16 , e. :

 

(X, Eps);

Y <- 0; <- X; K <- 1; Z <- X*;

abs()>=Eps

Y <- Y+; (5.15)

K <- K+2; (5.16)

<- -A*Z/(K*(K-1)) (5.17)

;

(Y).

 

(5.15)-(5.17). (5.15) (5.16) , , R Z. (5.17) k Z R, float.

³, >>1, , . max , , . sin. , sin x [0, p/2].

:

 

SinX

Pi= 3.14159265;

X, Eps, , Z, Y: ;

K, M: ;

(X, Eps);

X<0 { sin(- ) = -sin() }

M <- -1; X <- -X

M <- 1

;

<- 2*Pi;

X>= { sin(-2p) = sin() }

X <- X-A

; { 0<= < 2p }

X>=Pi { sin() = -sin(-p) }

M <- -M; X <- X-Pi

; { 0<= < p }

X>=Pi/2 { sin() = sin(p-) }

X <- Pi-X

; { 0<= < p/2 }

Y <- 0; <- X; K <- 1; Z <- X*;

abs()>=Eps

Y <- Y+;

K <- K+2;

<- -A*Z/(K*(K-1))

;

Y <- Y*M;

(Y)

.

 

5.2.4.

 

' 8- - . - - : 0 1 ( 9 : ). 28 = 256 , 0 255, 2. , - N255, . ' , - .

, , . , , .

.

. , , 15 , 215 = 32768 . Z32767.

- . . , ѳ . 16. 3 . b = 16, = 6, 2 16. 166 = 16777216, . , , (0 - , 1 - ) . , 7 , , d = 27 - 1 = 127. q = 64.

 

-65 -79 63 75

= 16 = 5.4 * 10, = 16 = 7.2 * 10 .

min max

 

+

- r f

<-><------> <-------------------------------->

----------- ---------- ---------- ----------

L-+-------- L--------- L--------- L---------

1 2 3 4

 

,

6 7 15

ABRZ { 6, 16, 2, 2 - 1; 2 - 1 }.

 

IBM PC . . .

 

+

- r f

<-><-----------><------------------------------>

----------- ----------- ---------- ----------

L-+-------- L-+-------- L--------- L---------

4 3 2 1

 

. , b = 2, = 23. 23 . , . , . ,

 

f = 1.a1 2 3... 23,

 

k - k- ( 2-k ).

224 = 16 777 216, , . d = 28 - 1 q = 127. ,

 

-127 -39 128 38

= 2 = 5.9 * 10, = 2 = 3.4 * 10.

min max

 

7 8 15

ABRZ { 23, 2, 2 -1, 2 - 1; 2 - 1 }.

 

³, ABRZ, , Zn n = 231 - 1 = 2 147 483 647 R, b, q, d = 52, d = 211-1. 16 , .

 

. ' , - , .

, . , .

. . .

 

5.3.

 

5.3.1.

 

. - . , 2 : byte word. byte m Nm 255 (28 - 1), word 65535 (216 1).

:

 

var N, M, L: byte;

var K, I: word;

 

byte ' , word .

, . .

5.3. , byte word, . - Overflow checking .

 

ѳ unsigned, . ѳ .

 

5.3.2. ֳ

 

: . integer longint. integer -maxint maxint, maxint - ( ). maxint 32767 (215-1). longint -231 231-1, -2 147 483 648..2 147 483 647. :

 

var I, J, K: integer;

var A, B, C: longint;

, :

 

a) : +; x-y; *; div ; mod .

5.4. , , . 2P. , .

 

b) :

abs() - ;

sqr() - 2;

 

true, - ,

odd() = <

false, - ;

 

-1, >= -maxint,

pred() = <

maxint, = -maxint-1,

 

+1, < maxint,

succ() = <

-(maxint+1), = maxint,

 

random() - [0, ].

 

1P. n>= 1 1,..., n. , 1,..., n - . k 1,..., n k.

 

program Zmax;

var J, K, N: word;

, M: integer;

begin

write('n=? '); readln(N);

write(' 1-? '); readln(M);

K:= 1;

for J:=2 to N do begin

write(J:2,'-? '); readln();

if >M then begin M:= ; K:= J end

end;

writeln(' = ', M:2,' = ', K:1)

end.

 

2P. .

 

program Euclid;

var X, Y, M, N: integer;

begin

write(', =? '); readln(X, Y);

M:= abs(X); N:= abs(Y);

while M<>N do

if M>N then M:= M-N

else N:= N-M;

writeln('( ', X:2,',', Y:2,' )= ', M:2);

N:= abs(X) div M;

if N>maxint div abs(Y) then writeln(' ')

else writeln('( ', X:2,',', Y:2,' )= ', N*abs(Y):2)

end.

 

ѳ int. ֳ : short (), long () unsigned ( ), signed ( ). :

 

signed int a, b, c;

signed short int d;

unsigned long int x, y;

 

, int . signed unsigned, signed. :

 

int a, b, c;

short d;

unsigned long x,y;

 

ѳ short int int 2 , long int 4 .

int long int, unsigned unsigned long. int long int integer longint . unsigned word . unsigned long 0 232-1, 4 294 967 296.

, :

 

a) : +; x-y; *; / ; % .

5.4 ѳ.

 

b) :

abs() - (int x);

labs() - (long x);

 

: math.h:

#include <math.h>

 

1C.

 

#include <stdio.h>

 

/* Zmax */

main()

{

unsigned j, k, n;

int a, m;

 

printf(n=? ); scanf(%u, &n);

printf( 1-? ); scanf(%d, &m);

k = 1;

for (j=2; j<=n; j++)

{

printf(%u-? , j); scanf(%d, &a);

if (a>m)

{

m = a; k = j;

}

}

printf( = %d = %d\n, m, k);

}

 

2C.

 

#include <stdio.h>

#include <math.h>

 

/* Euclid */

main()

{

int x, y, m, n;

 

printf(, =? ); scanf(%d %d, &x, &y);

m = abs(x); n = abs(y);

while (m!=n)

if (m>n) m -= n;

else n -= m;

printf((%d,%d)= %d\n, x, y, m);

n = abs(x) / m;

if (n>32767 / abs(y)) printf(\n);

else printf((%d,%d)= %d\n, x, y, n*abs(y));

}

 

5.3.3. ij

 

. . .

 

real 2.9e -39 .. 1.7e 38 11-12 6

single 1.5e -45 .. 3.4e 38 7- 8 4

double 5.0e -324 .. 1.7e 308 15-16 8

extended 3.4e-4932 .. 1.1e4932 19-20 10

 

:

var X, Y, Z: real;

var a, b, c: single;

var s, p, q: double;

var t, u, v: extended;

 

, , 0, 0.0 +00, 3.14159, 6.022 -13.

- Pi=3.1415926536+00.

, :

 

a) : +; x-y; *; /.

 

5.7. , . , .

 

b) :

abs() - ;

sqr() - 2;

 

sqrt() - ;

 

sin() - ;

 

cos() - ;

 

arctan() - ;

 

ln() - ;

 

exp() - x;

 

frac() - , , frac(2.7) = 0.7;

 

int() - , , int(2.9) = 2.0, int(-2.9) = -2.0;

 

random - [0,0.99].

 

trunc round

 

trunc() = sign()*[ ];

round() = sign()*[+1/2 ].

 

3P.

k

S sin kn

10 n=1

S = S -----------.

k=1 k!

 

program Sum;

var K, N: word;

S, , U: real;

begin

S:= 0; U:= 1.0;

for K:=1 to 10 do begin

U:= U*K; := 0;

for N:=1 to K do

:= +sin(K*N);

S:= S+/U

end;

write(' = ', S)

end.

 

4P. 1/n, ,

 

1/n +n-1

= lim , = -------,

k->¥ k 1 2

_ _

1

= - (n-1)* + ------ , k=1,2,...

k+1 n k n-1

_ _

k

" > 0 n Î N.

k+1 k< e, e>0.

 

program Sq1;

var N, K, M: integer;

, X, Y, Z, Eps: real;

begin

write('n, a, eps=? '); readln(N, , Eps);

M:= N-1; Y:= 0.0; X:= (+M)/2.0; K:= 1;

while abs(X-Y)>=Eps do begin

Y:= X; Z:= exp(M*ln(X));

X:= (M*+/Z)/N; K:= K+1

end;

write(' ', K:1,' ', ,' ^ 1/', N:1,' = ', X)

end.

 

e=0.01 9 , ' 3.71293 1.3000003980 +00.

kn < e,

 

program Sq2;

var N, K, M: integer;

, X, Z, Eps: real;

begin

write('n, a, eps=? '); readln(N, , Eps);

M:= N-1; X:= (+M)/2.0; Z:= exp(M*ln(X)); K:= 1;

while abs(A-Z*)>=Eps do begin

X:= (M*+/Z)/N; K:= K+1;

Z:= exp(M*ln(X))

end;

write(' ', K:1,' ', ,' ^ 1/', N:1,' = ', X)

end.

 

8 1.3005088115+00.

5.1. , , 7 = 1.3184430036+00.

 

ѳ : float, double long double. , single, double extended .

 

, :

 

a) : +; x-y; *; /.

 

b) :

fabs() - ;

 

sqrt() - ;

 

sin() - ;

 

cos() - ;

 

asin() - ;

 

acos() - ;

 

atan() - ;

 

log() - ;

 

log10() - ;

 

exp() - x;

 

pow(,y) - xy;

 

ceil() , x;

 

floor() , x;.

 

: math.h. double.

 

3C.

 

#include <stdio.h>

#include <math.h>

 

/* Sum */

main()

{

unsigned k, n;

double s, t, u;

 

s = 0; u = 1.0;

for (k=1; k<=10; k++)

{

u *= k; t = 0;

for (n=1; n<=k; n++)

t += sin(k*n);

s += t/u;

}

printf( = %lf, s);

}

 

4C.

 

#include <stdio.h>

#include <math.h>

 

/* Sq1 */

main()

{

int k, n, m;

double a, x, y, z, eps;

 

printf(n, a, eps=?);

scanf(%d %lf %lf, &n, &a, &eps);

m = n-1; y = 0.0; x = (a+m)/2.0; k = 1;

while (fabs(x-y)>=eps)

{

y = x; z = exp(m*log(x));

x = (m*x+a/z)/n; k++;

}

printf( %d %lf ^ 1/%d = %lf, k, a, n, x);

}

 

[̲] [] [] [ ]