"Algorithmic design and data structure" why do programmers like to set the INF to 0x3f3f3f3f?

Source: Internet
Author: User

In the algorithmic race, we often need to use a "infinity" value, for me, most of the time I will be based on a specific question to take a 99999999 of the number (looks very unprofessional ah. )

When looking at other people's Code on the Internet, I often see that they set the INF as 0x7fffffff, wondering why a hexadecimal number is so strange that a check is known 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 when calculating the minimum), then 0x7fffffff is a perfect choice.

But in more cases, 0x7fffffff is not a good choice, for example, in the shortest path algorithm, we use the relaxation operation:

if (D[u]+w[u][v]<d[v]) d[v]=d[u]+w[u][v];

If there is no edge between the 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.

To be exact, 0x7fffffff cannot satisfy the "infinity plus a poor number is still infinite" condition, it will become a very small negative.

Further, if there is a number that satisfies "infinity plus infinity is still infinite", then it's better.

Before the array inadvertently saw a different value, inf=0x3f3f3f3f, then I was depressed, and this value represents what. So I went to find the answer and found that the setting of this value was really subtle.

0x3f3f3f3f decimal is 1061109567, is the 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 also bring us an unexpected additional benefit:
If we want to clear an array, we usually use memset (A,0,sizeof (a)), which is convenient and efficient, but when we want to assign an array to infinity, we can't use the Memset function to write the loop ourselves, because Memset is a byte-by-bit operation, It is able to clear 0 of the array because 0 of each byte is 0 (usually we only use it when we assign a value of-1 and 0). Well now, if we set infinity to 0x3f3f3f3f, then miracles happen, and every byte of 0x3f3f3f3f 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. Reprinted from: http://blog.csdn.net/jiange_zh/article/details/50198097

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.