0x3f3f3f3f an interesting number for INF

If you are using C++ to write program, sometimes you need to set an large number. We can use INT_MAX of course, however, sometimes we may get overflow if we add the large number by 1.

Some people they like use this number as INF which is 0x3f3f3f3f.

Several points:

  • 0x3f3f3f3f is large enough for most cases
  • 0x3f3f3f3f + 0x3f3f3f3f will not overflow
  • we can easily use memset : memset(a, 0x3f, sizeof (a)).
From math is fun

What about -INF?

For -INF, we can use 0xcfcfcfcf

From math is fun

Reference

https://www.cnblogs.com/zarth/p/6534526.html

--

--