In the CR question "Terminating a C loop when maximum hardware limit reached" the use of a positive sum wrapping to a negative value is used to terminate the loop. The queston was posted to Code Review even though it could reasonably been asked on Stack Overflow instead. The responses have provoked another question:
When
fib
is less than the max integer value supportedprev
is also well less than the max integer supported. So, the sum of the two will never exceed the integer size so as to overflow beyond a negative range. The use of the testfib>0
will work in the special case of the Fibonacci numbers but does not generalize. What code would be a better solution for the more general case?
Which is the better fit for the question, Code Review or Stack Overflow?