Infinitely large settings in programming
Many people may be set to 0x7fffffff, this number is indeed the maximum value of 32-bit int, the sign bit is 0, the others are 1
However, in many cases, 0X7FFFFFFF will be error, such as overflow , so that the two Infinity number added will become negative, as in the Dijkstra to find the shortest way, as a relaxation operation, judging if (D[u]+w[u][v]<d[v]) D[v] =D[U]+W[U][V], if you do not have a path to V, W[U][V]=0X7FFFFFFF, so d[u]+w[u][v] will become negative , which produces an error.
In order to avoid the above error, we can change the Infinity setting, we can set the 0x3f3f3f3f to infinity, the0x3f3f3f3f 10 is represented as 1061109567, the number has reached 10^9, enough to indicate infinity, and 0x3f3f3f3f+0x3f3f3f3f=2122219134, satisfying infinity + infinity is still infinitely large
When the infinity is set to 0x3f3f3f3f, it is also very convenient to initialize, for example, when initializing array A, you can use the
Memset (A,0x3f,sizeof (a)), because each byte of 0x3f3f3f3f is 0x3f, and if you use 0x7fffffff, you need to loop to assign a value, which takes more time
#include <stdio.h>
#include <string.h>
#define MAX1 0x7fffffff
#define MAX2 0x3f3f3f3f
int a[3];
int main ()
{
memset (a,0x3f,sizeof (a));
printf ("%d\n", MAX1);
printf ("%d\n", max1*2);
printf ("%d\n", MAX2);
printf ("%d\n", max2*2);
for (int i=0;i<3;i++)
printf ("%d", A[i]);
printf ("\ n");
return 0;
}
Reference: http://blog.csdn.net/mylovestart/article/details/8238088