简体   繁体   English

C ++中整数类型的正无穷大

[英]positive and negative infinity for integral types in c++

I am reading about positive and negative infinity in c++. 我正在阅读有关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(); 将无法工作,但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(); 所以std::numeric_limits<int>::max(); of the integral type could be taken as its positive infinite limit ? 整数类型的positive infinite limit整数可以看作是它的positive infinite limit

Or the integral type has only the max value and the infinity value is not true ? 还是整数类型只有max valueinfinity value不是真的?

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. 通过将整数设置为其最大值,可以最接近所需的值,对于带符号整数,最大值约为20亿。

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. std :: numeric_limits具有has_infinity成员,您可以使用该成员检查所需的类型是否具有无限表示形式,该表示形式通常仅针对浮点数,例如float和double。

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 编辑:在我的《 AMD64体系结构程序员手册》的精装本第315页上,它说

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). 无穷大是一个正数或负数+∞和-∞,其中整数位为1,有偏指数为最大值,分数为0。无穷大是可以浮点格式表示的最大值,负无穷小大于任何有限数,并且正无穷大大于任何有限数(即仿射感)。

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. 当将非零,非无限数除以0或乘以无穷大,或者将无穷大加到无穷大或加到0时,就会产生无限大结果。 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) (任何打字错误都是我的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM