Is the following code meaningful? Please answer in detail.

REAL Y=2.0, X=1.0, E=1.0; DO (E=E/2.0; Y=X+E; ) WHILE (Y<>1.0) 

Closed due to the fact that it is necessary to reformulate the question so that the participants can give an objectively correct answer Vladimir Martyanov , insolor , aleksandr barakin , Athari , Grundy 17 Apr '16 at 7:17 .

The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Launched the program. Why did the program stop counting after 30 iterations, although 1 <> 1 (e to the extent)? - V.Ch
  • one
    Programming language indicate. - Qwertiy
  • one
    Who can guess the language in three lines? Fortran? Meaningful is a relative concept. It may be necessary to check if this cycle is executed at least twice. It is enough to count manually only once. If y turns out = 1 the first time, then there is certainly no point in the cycle. Another option is when y is always <> 1, we get an infinite loop. E should tend to zero, it means y to strive to 1. Will it reach 1? Prompt the specialist in calculations with real numbers. If he says that he will not achieve it, then the question is, is there any sense for someone in infinite cycles? - Sergey
  • @Qwertiy, suppose it is an unknown programming language, it doesn't matter here, the algorithm itself is important. Theoretically, yes, it is infinite, but in the realities of life in working with iron it turns out differently. - V.Ch
  • @ V.Ch, I kind of wrote the answer? - Qwertiy

1 answer 1

The binary representation of y contains only two single bits: the first (the integer part is equal to 1) and one more, distant from the first by some power of two. First it is 1.1 , then 1.01 , 1.001 and so on. Theoretically - to infinity. But computer real numbers have limited accuracy. As soon as that bit is outside this precision, a number of 1 obtained.

I note that in this code, all operations except addition are performed absolutely exactly.


The similar code on javascript performs 53 iterations (this is the double digits of the mantissa):

 var y=2, x=1, e=1; do { e /= 2; y = x + e; } while (y !== 1); 
  • And it is possible more in detail about I will notice that in this code all operations except addition are carried out absolutely precisely. ? How here with the accuracy of division, for example? - splash58
  • @ splash58, dividing by 2 decreases the value of the exponent by 1. All bits of the mantissa are 0. Because the exit from the cycle will occur when the number of iterations exceeds the din of the mantissa, and the division becomes inaccurate when all the negative exponent values ​​are finished, and then when the bit is in a denormalized form crawls all the number. Even regardless of the size of the exponent, the denormalized number will allow to save without loss the exact value with which we increase the unit, but it will not reach here. - Qwertiy
  • @Qwertiy, thanks for the answer, the normalization of the floating-point number is to blame, as I understand. - V.Ch
  • @ V.Ch, no, wrong. Normalization has nothing to do with it. - Qwertiy
  • @Qwertiy, the number of iterations depends on the bitness of the data type, right? - V.Ch