2

I'm migrating some code from a compiler with 32-bit integers to 64-bit. I found some old code that assumes the highest possible integer is 2147483647 and the lowest possible is -947483647.

I understand the highest (maximum signed 32-bit integer), but does anyone know what makes the lowest special? There is nothing in the business logic that suggests that this integer (used for an ID) can't be below that number.

Searching google turns up very little except some other code where someone used 947483646 in a variable called INF (infinity/highest possible number? In signed, two's-complement representation that would make "negative infinity" -947483647).

It may be just a meaningless number, but there are also a few other hits using that exact number for other integers like monster HP in a video game (while searching for other close-by numbers turn up no results) that makes me think there's something behind it.

4

1 回答 1

2

Int32 数据类型没有无穷大的值,因此可以随机选择幻数来表示 INF。
为 +INF 选择最大整数值 (2^31-1) 是很自然的。
但是为什么 -INF 没有选择 (-2^31) 呢?
可能是因为 +INF 和 -INF 在打印时必须具有相同的长度(每个 10 个字符)。

于 2013-05-13T21:54:59.673 回答