About 0x3f3f3f3f (0x four x 3f)

Source: Internet
Author: User
Tags constant

In the previous minimum spanning tree problem encountered, I followed the previous habit of the INF defined as 1 billion, and then initialize the array to the maximum value, and then enter the connected path +value, the last value is still inf is not the case. But there is a problem here, the INF is defined as 1 billion, the last displayed array value is negative, finally changed to 0x3f3f3f3f finally solved.

Today just see a related blog, so carefully analysis.

Referenced from: http://blog.csdn.net/bossup/article/details/9090277

>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>

If the scope of the data in the problem is clear, then the Infinity setting is not a problem, and in the ambiguous case, many programmers take 0x7fffffff as infinity, because this is the maximum value of 32-bit int. If this infinity is used only for general comparisons (such as the initial value of the min variable at the minimum), then 0x7fffffff is a perfect choice, but in more cases, 0x7fffffff is not a good choice. Most of the time we do not simply compare the infinity, but we will compare it after the operation, for example, the relaxation operation that will be used in most shortest path algorithm:
if (D[u]+w[u][v]<d[v]) d[v]=d[u]+w[u][v];
We know that if there is no edge between u,v, then W[u][v]=inf, if our INF takes 0x7fffffff, then d[u]+w[u][v] will overflow and become negative, our relaxation operation is wrong, more generally speaking, 0x7fffffff can not meet the " Infinity plus a poor number is still infinite ", it becomes a very small negative. In addition to being satisfied plus a constant is still infinity, our constants should also meet "infinity plus infinity is still infinite", at least two infinite sum should not be catastrophic error, this point 0x7fffffff still can not satisfy us.

So we need a better guy to replace 0x7fffffff, the most rigorous way of course is to deal with Infinity in particular rather than find a big big constant to replace it (or simulate it), but this will make our programming process cumbersome. In the code I've read, the most ingenious infinity constant value is 0x3f3f3f3f, I don't know who first started using this subtle constant to do infinity, but I did learn from a blog that I didn't know about Acmer (Id:staginner). This constant was used in many of her code, so I tried it myself and found it very useful, and when I did a more in-depth analysis of the constant, I found it was really ingenious. 0x3f3f3f3f decimal is 1061109567, that is, 10^9 level (and 0x7fffffff an order of magnitude), and the general situation of the data is less than 10^9, so it can be used as infinity without the case of data greater than infinity. On the other hand, since the general data is not greater than 10^9, when we add infinity to a data, it does not overflow (this satisfies the "infinity plus a poor number is still infinite"), in fact 0x3f3f3f3f+0x3f3f3f3f= 2122219134, this is very large but not more than the 32-bit int range, so 0x3f3f3f3f also meet our "Infinity plus infinity or infinity" demand. Finally, 0X3F3F3F3F can bring us an unexpected additional benefit: if we want to clear an array, we usually use code like memset (A,0,sizeof (a)) to do it (convenient and efficient), But when we want to assign an array to infinity (for example, to initialize the adjacency matrix when solving graph theory problems), you can't use the memset function to write loops (it's really painful to write these unimportant code), and we know that because Memset is in bytes, It can clear the array 0 because 0 of each byte is 0, now well, if we set infinity as 0x3f3f3f3f, then the miracle happened, 0x3f3f3f3f each byte is 0x3f. So to set a memory to infinity, we only need memset (A,0x3f,sizeof (a)).

So in the usual situation, 0x3f3f3f3f really is a great choice.

Negative infinity is better with 0XCFCFCFCF.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.