The setting technique of infinite constants in programming "0X3F3F3F3F"

Source: Internet
Author: User



the technique of setting infinite constants in programming

If the scope of the data in the problem is clear, then the infinite setting is not a problem, 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.

    1. . Span style= "Color:rgb (102,102,102)", for example, the slack operation used in most shortest path algorithms:
      if (D[u]+w[u][v]<d[v]) d[v]=d[u]+w[u][v];
      We know if there's 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 "infinity plus a poor number is still infinite", it becomes a very small negative.
    2. In addition to satisfying 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 from a acmer that I didn't know (ID: Staginner's blog learned that the constant was used in many of his/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.

  1. 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.
  2. 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.
  3. 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 the graph theory problem), you can't use the memset function to write loops (it's really painful to write these unimportant code), and we know that this is because Memset is in bytes, and it can clear the array 0 is 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.

the technique of setting infinite constants in programming

If the scope of the data in the problem is clear, then the infinite setting is not a problem, 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.

    1. . Span style= "Color:rgb (102,102,102)", for example, the slack operation used in most shortest path algorithms:
      if (D[u]+w[u][v]<d[v]) d[v]=d[u]+w[u][v];
      We know if there's 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 "infinity plus a poor number is still infinite", it becomes a very small negative.
    2. In addition to satisfying 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 from a acmer that I didn't know (ID: Staginner's blog learned that the constant was used in many of his/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.

  1. 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.
  2. 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.
  3. 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 the graph theory problem), you can't use the memset function to write loops (it's really painful to write these unimportant code), and we know that this is because Memset is in bytes, and it can clear the array 0 is 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.

The setting technique of infinite constants in programming "0X3F3F3F3F"

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.