For an input , start at and repeatedly increment and double while . In exact arithmetic, the loop invariant and the minimality of the terminating index give . For a general positive real input, the formula is instead : if , the loop makes no updates and cannot return a negative index.
Finite-width integer arithmetic also requires an overflow check. In wrapping signed 32-bit arithmetic, doubling produces the negative value and a further doubling produces zero. For a positive input exceeding , the loop can then remain at zero indefinitely. This is a two's complement wraparound example, not a universal language rule for signed overflow. A signed 64-bit doubling variable can represent the first power of two above every positive signed 32-bit input, repairing this particular range. The proof of the mathematical loop and the representation limits are separate parts of its correctness argument.
Articles by others on the same topic
There are currently no matching articles.