Blog.aikilis.com2471.html if the range of each data in the problem is clear, the infinite limit setting is not a problem. If it is not clear, many programmers use 0x7fffffff as the infinity, because this is the largest 32-bitint. If this infinity is only used for general comparison (for example, to obtain the minimum min variable), then 0x7fffff
Http://blog.aikilis.com/2471.html if the scope of the data in the problem is clear, then the infinite set is not a problem, in the case of unclear, many programmers take 0x7fffffff as Infinity, because this is the maximum 32-bit int. If this infinity is only used for general comparison (for example, to obtain the minimum min variable), then 0x7fffff
Http://blog.aikilis.com/2471.html
If the range of data in the problem is clear, the infinite setting is not a problem. If it is not clear, many programmers take 0x7fffffff as the infinite, because this is the maximum value of 32-bit int. If this infinity is only used for general comparison (for example, the initial value of the min variable when the minimum value is obtained), 0x7fffffff is indeed a perfect choice, but in more cases, 0x7fffffff is not a good choice.
- In many cases, we do not just compare infinite values, but compare them after computation. For example, we use the relaxation operation in most Shortest Path Algorithms:
If (d [u] + w [u] [v] We know that if there is no edge between u and v, w [u] [v] = INF. If our INF is 0x7fffffff, then d [u] + w [u] [v] will overflow into a negative number, and our relaxation operation will go wrong. More generally, 0x7fffffff cannot meet the requirement of "infinite plus a finite number is still infinite", and it becomes a small negative number.
- In addition to satisfying the fact that a constant is still infinite, our constant should also satisfy the condition that "infinite plus infinity is still infinite". At least two Infinity additions should not cause catastrophic errors, at this point, 0x7fffffff still cannot satisfy us.
So we need a better guy to replace 0x7fffffff. the most rigorous approach is to handle infinity in particular, rather than finding a large constant to replace it (or simulate it ), but this will make our programming process very troublesome. In the code I have read, the most exquisite infinite constant value is 0x3f3f3f3f. I don't know who is the first to use this exquisite constant for infinity, but I did learn from a blog about ACMer (ID: Staginner) That I don't know. He/she uses this constant in many of his/her code, so I tried it myself and found that it was very useful. When I made a more in-depth analysis on this constant, I found that it was actually very delicate.
- 0x3f3f3f3f is 1061109567 in decimal format, that is, 10 ^ 9 (an order of magnitude of 0x7fffffff). Generally, the data is smaller than 10 ^ 9, therefore, it can be used as an infinite number without the case where data is larger than an infinite number.
- On the other hand, since the average data is not greater than 10 ^ 9, when we add a data to infinity, it does not overflow (this satisfies "Infinite plus a finite number is still infinite"). In fact, 0x3f3f3f3f + 0x3f3f3f3f = 2122219134, this is very large, but it does not exceed the 32-bit int expression range, so 0x3f3f3f3f also meets our "infinite plus infinity or infinity" requirements.
- Finally, 0x3f3f3f3f can bring us an unexpected extra benefit: if we want to clear an array, we usually use memset (a, 0, sizeof ()) this code is convenient and efficient, but when we want to assign all the values of an array to infinity (for example, when we solve the graph theory problem, the initialization of the adjacent matrix ), instead of using the memset function, we have to write loops by ourselves (it is really painful to write these unimportant Code). We know this is because memset operates in bytes, it can be used to clear the array because every byte of 0 is 0. Now, if we set the infinity to 0x3f3f3f3f, then the miracle will happen. Each byte of 0x3f3f3f is 0x3f! Therefore, to set all memory segments to infinity, we only need memset (a, 0x3f, sizeof ()).
In general, 0x3f3f3f3f is a great choice.
? 2012, Aikilishttp: // blog.aikilis.com /.