List confusion

How to find a range of listings? I have two versions that define this differently when the lowest value is negative. Stroustrup says for

 enum e3 { min = -10 , max = 1000000 }; // range -1048576:1048575

but C ++ Primer Plus 5th edition says

for example, if the smallest counter is -6, the next power of two [minus sign times] is -8, and therefore the lower limit is -7

I am confused, what is right?

+3
source share
3 answers

I believe both of them are correct (see Primer definition below), depending on how compatible you are. Formal definition

, e min - , e max - , b min b max, b min b max, , , e min e max.

, . :

b max - , max (abs (e min) - 1, abs (e max)) 2 -1; b , e - (b + 1) .

, -1048575:1048575. . Primer definiton , , -7. , -1048575:1048575.

+4

++ (§ [dcl.enum]/7):

, e min , e max , b min b min, :

  • K - 1 ....
  • b max - , max (| e min | - K, | e max |) 2 M - 1, M - .
  • b min , e min - (b max + K) .

,

  • e min= -10, e max= 1000000
  • , b max ≥ max (10 & minus; 1, 1000000) = 1000000
  • b max 2 M & minus; 1, M = 20 → b max= 1048575
  • e min , b min= & minus; (1048575 + 1) = & plus; 1048576.

  • e min= -6, e max=
  • , b max ≥ max (6 & minus; 1, ) = 5
  • 5, b max 7, b min - -8.
  • > 7, .

, ++ Primer , .

+2

, . 1000000, 20 , -1048576 +1048575. -6 3 , , , -8 +7.

0

Source: https://habr.com/ru/post/1774584/


All Articles