positive and negative infinity for integral types in c++

不打扰是莪最后的温柔 提交于 2019-12-24 00:09:25

问题


I am reading about positive and negative infinity in c++.

I read that integral types dont have a infinite value ie. std::numeric_limits<int>::infinity(); wont work, but std::numeric_limits<int>::max(); will work and will represent the maximum possible value that can be represented by the integral type.

so the std::numeric_limits<int>::max(); of the integral type could be taken as its positive infinite limit ?

Or the integral type has only the max value and the infinity value is not true ?


回答1:


Floating point numbers have a special bit pattern to indicate "the value is infinity", which is used when the result of some operation is defined as infinite.

Integer values have a set number of bits, and all bits are used to represent a numeric value. There is no "special bit pattern", it's just whatever the sum of the bit positions mean.

Edit: On page 315 of my hardcopy of AMD64 Architecture Programmer's Manual, it says

Infinity. Infinity is a positve or negative number +∞ and -∞, in which the integer bit is 1, the biased exponent is maximum and fraction is 0. The infintes are the maximum numbers that can be represented in floating point format, negative infinity is less than any finite number and positive infinity is greater than any finite number (ie. the affine sense).

And infinite result is produce when a non-zero, non-infinite number is divided by 0 or multiplied by infinity, or when infinity is added to infinity or to 0. Arithmetic infinites is exact. For example, adding any floating point number to +∞ gives a result of +∞ Arithmetic comparisons work correctly on infinites. Exceptions occur only when the use of an infinity as a source operand constitutes an invalid operation.

(Any typing mistakes are mine)




回答2:


Integers are always finite.

The closest you can get to what you're looking for is by setting an integer to it's maximum value, which for a signed integer only around 2 billion something.

std::numeric_limits has a has_infinity member which you can use to check if the type you want has an infinite representation, which are usually only on floating point numbers such as float and double.



来源:https://stackoverflow.com/questions/16356073/positive-and-negative-infinity-for-integral-types-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!