Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is infinity = 0x3f3f3f3f?

Tags:

c

infinity

In some situations, one generally uses a large enough integer value to represent infinity. I usually use the largest representable positive/negative integer. That usually yields more code, since you need to check if one of the operands is infinity before virtually all arithmetic operations in order to avoid overflows. Sometimes it would be desirable to have saturated integer arithmetic. For that reason, some people use smaller values for infinity, that can be added or multiplied several times without overflow. What intrigues me is the fact that it's extremely common to see (specially in programming competitions):

const int INF = 0x3f3f3f3f; 

Why is that number special? It's binary representation is:

00111111001111110011111100111111 

I don't see any specially interesting property here. I see it's easy to type, but if that was the reason, almost anything would do (0x3e3e3e3e, 0x2f2f2f2f, etc). It can be added once without overflow, which allows for:

a = min(INF, b + c); 

But all the other constants would do, then. Googling only shows me a lot of code snippets that use that constant, but no explanations or comments.

Can anyone spot it?

like image 845
Gabriel Avatar asked Aug 25 '13 12:08

Gabriel


People also ask

How do you write negative infinity in C++?

Use Negative Product of numeric_limits::infinity() in C++ Use the limits library in C++.

How do you return infinity in C++?

The C++ infinity is written as “INF” and it accrues in the outcome of dividing a positive numeric value by a null value or calculating a numeric value that is greater than the larger number of our system that can be represented in 64 bits.


1 Answers

I found some evidence about this here (original content in Chinese); the basic idea is that 0x7fffffff is problematic since it's already "the top" of the range of 4-byte signed ints; so, adding anything to it results in negative numbers; 0x3f3f3f3f, instead:

  • is still quite big (same order of magnitude of 0x7fffffff);
  • has a lot of headroom; if you say that the valid range of integers is limited to numbers below it, you can add any "valid positive number" to it and still get an infinite (i.e. something >=INF). Even INF+INF doesn't overflow. This allows to keep it always "under control":

    a+=b; if(a>INF)     a=INF; 
  • is a repetition of equal bytes, which means you can easily memset stuff to INF;

  • also, as @Jörg W Mittag noticed above, it has a nice ASCII representation, that allows both to spot it on the fly looking at memory dumps, and to write it directly in memory.
like image 92
Matteo Italia Avatar answered Sep 30 '22 19:09

Matteo Italia