Java如何在内存中存储+-Infinite?
如果需要全部32位来存储从-2^31到2^31,它如何存储+和-无穷大?它使用更多内存吗?存储这些值是否良好且安全?
更新:感谢答案,我知道只有浮点数据类型可以存储 Inf 值,整数不能。
If it takes all 32 bits to store from -2^31 to 2^31, how can it store + and - Infinite? Does it use more memory? Is it good and safe to store those values?
UPDATE: Thanks to the answers, I know that only floating point data types can store Inf value, Integers can't.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Java 与大多数其他编程语言一样,遵循 IEEE 754 规范浮点数。可能值的范围略有减小,以允许无穷大以及
NaN< /code>(不是数字)
。这些可以安全储存;它们甚至可以作为常量使用,例如
Double.POSITIVE_INFINITY
。我认为您的意思是 32 位,以存储此范围内的所有可能的整数值。 Java 的 Integer 类型按照您的描述工作,并且无法存储无穷大或其他特殊值。
Java, along with most other programming languages, follows the IEEE 754 specification for floating point numbers. The range of the possible values is slightly reduced to allow for the infinities as well as
NaN
(Not a Number). These are safe to store; they're even available as constants, for exampleDouble.POSITIVE_INFINITY
.I think you mean 32 bits, to store all of the possible integer values in this range. Java's Integer type works as you describe, and has no way to store infinities or other special values.
您正在谈论
整数
s。您正在谈论
Float
和双
,这是不同的数据类型。Float
是 32 位 IEEE 754 数字,Double
是 64 位 IEEE 754 数字。 IEEE 754 浮点数为正无穷和负无穷预留了特殊值。Java 的
Integer
类中没有特殊的“无穷大”值,您可以存储全范围的值。There you're talking about
Integer
s.There you're talking about
Float
andDouble
, which are different data types.Float
is a 32-bit IEEE 754 number, andDouble
is a 64-bit IEEE 754 number. IEEE 754 floating-point numbers have special values set aside for positive and negative infinity.There is no special "infinity" value in Java's
Integer
class, you can store the full range of values.