Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C+中,Float是inf,但integer不是inf+;-xCode。为什么?_C++_Xcode_Int - Fatal编程技术网

C++ 在C+中,Float是inf,但integer不是inf+;-xCode。为什么?

C++ 在C+中,Float是inf,但integer不是inf+;-xCode。为什么?,c++,xcode,int,C++,Xcode,Int,可能是一些基本问题。但我想不出答案 据我所知,int的最高数字是:-2147483648 当我超过那个极限时,编译器总是向我显示那个最高的值 对于float来说,它是:3.4e+38(或者减号,但这不是重点) 当我通过浮点限制时,编译器总是向我显示inf。为什么不3.4e+38或-3.4e-38 我的代码是这样的: int bytesT=8*5; int a=0; for (int i=0; i<bytesT; i++) { a += pow(2, i); } std::cout

可能是一些基本问题。但我想不出答案

据我所知,
int
的最高数字是:
-2147483648

当我超过那个极限时,编译器总是向我显示那个最高的值

对于
float
来说,它是:
3.4e+38
(或者减号,但这不是重点)

当我通过浮点限制时,编译器总是向我显示
inf
。为什么不
3.4e+38
-3.4e-38

我的代码是这样的:

int bytesT=8*5;
int a=0;

for (int i=0; i<bytesT; i++)
{
    a += pow(2, i);
}
std::cout << "a = " << a << std::endl;


float b= 3.4 * pow(10, 39);
std::cout << "b = " << b << std::endl;

有人能解释这种行为吗?提前感谢。

inf
表示一个大于相应浮点类型所能容纳的值。因此,如果
float
的最高值为
3.4e+38
,则
3.4*pow(10,39)
较大(注意
39
),因此
float
将该值表示为特殊值
inf
。但是,如果您编写
3.4*pow(10,38)
,您将获得(几乎)最高的值

实际最高(和最低)数字在
中提供:

#包括
int main(){
浮子b=3.4*pow(10,38);
浮点c=std::数值限制::最大值();

cout整数通常没有“无限”for或任何其他标记状态。它们只有值。这里使用2的幂将位逐个设置为1,使int为32位。这意味着您将得到一个值,作为2的补码,它是有符号int可以具有的最小值。如果该值是无符号的,它将是最大的。如果将32位值相加,则为v值的最低32位全部为零,没有任何变化。因此,您将以该值结束

(IEEE)浮点值的工作方式不同。它们有表示无穷大的标志,而不是数字等。因此,当值超出范围时使用这些标志是有意义的。这就是为什么当添加太多数字时,值超出范围时,最终使用inf

如果你一直把1添加到int变量中,你会看到它没有停留在任何一个值上。它会溢出并继续运行。我不知道C++规范有多少定义应该如何处理这些,所以它甚至可能是特定于实现的,但通常值溢出并继续到处流传。

代码>我要试试。要使此操作尽可能简单,请执行以下操作:
I'm gonna try to make this as simple as possible:  

int = 32 bits:
0000 0000 0000 0000 0000 0000 0000 0000  
left-most bit represents sign, so largest int32 number that you can have is:  
0111 1111 1111 1111 1111 1111 1111 1111 (which is 2147483647)

for negative numbers if left-most bit is set to 1 then to get "human readable"  
number you will have to do complement 2 of that number which is invert every bit  
and add 1.

ex.
1111 1111 1111 1111 1111 1111 1111 1111  <- my number (i know it is negative, -?)  
0000 0000 0000 0000 0000 0000 0000 0000  <- all bits converted  
0000 0000 0000 0000 0000 0000 0000 0001  <- added 1    
=> so that begging number was -1  

Now that you know this, question is what happens if you add 1 to the   
largest positive int32 number (0111 1111 1111 1111 1111 1111 1111 1111) ?  

Well you will get 1000 0000 0000 0000 0000 0000 0000 0000
which is (-2147483648). What happened here was overflow caused by addition.  
This was the case in your program for your 'a' variable.

- As for your confusion with 'inf': 

Floating point numbers work differently then integers.  
I'm not gonna go into too much detail on how bits and bytes are arranged.
Just know that there is :  
 - 1 bit reserved for sign (top-left)   
 - next 8 (for float32) bits for exponent   
 - and the rest for fraction  
Now what is interesting is that there are certain 'special' values defined in
IEEE 754 which are the following:

 - +0 = 0 00000000 00000000000000000000000  
 - -0 = 1 00000000 00000000000000000000000  
 - +inf = 0 11111111 00000000000000000000000
 - -inf = 1 11111111 00000000000000000000000
 - NaN = 0 11111111 00000000000000000000001

As you see these special values are not defined in int type which is the reason  
why you get 'inf' in float and something weird/unexpected in int.

I hope i answered your question. 
int=32位: 0000 0000 0000 0000 0000 0000 0000 0000 最左边的位表示符号,所以您可以拥有的最大int32数为: 0111111111111111111111111111111111111111111(即2147483647) 对于负数,如果最左边的位设置为1,则为“人类可读” 你们必须对那个数字进行补码2,这是每一位的倒数 并添加1。 前任。
1111111111111111111111111111111111111111
-2147483648
不是
int
可以接受的最高值。这是它可以接受的最小值,前提是你的
int
是32位。这是
inf
的一个很好的解释。但我觉得答案还应该讨论
int
溢出以及为什么
a+=pow(2,39)
是一个坏主意,
a
是一个32位有符号的
int
。是的,我同意弗朗索瓦·安德烈的观点。非常感谢你的回答和解释什么是
inf
。但我的问题的关键是“为什么对于整数我没有得到inf,而是得到了可能的最高数?”这是标准行为还是仅适用于xCode,或者可能仅适用于OS X系统?我现在没有Windows来测试它,但我记得在Windows中,编译器会显示一些超出限制的整数的随机数。但不确定。我经历了您在这里讲过的所有事情。所以这是正常行为?不仅是unix系统,还是xCode编译器?“如果你把最大的正整数32加1,会发生什么?”,也许你应该提到C++标准把这个当作整数溢出,这导致了未定义的行为;即使编译器可能忽略溢出,按照你所描述的那样进行,也没有保证。
#include <limits>

int main() {

    float b= 3.4 * pow(10, 38);
    float c= std::numeric_limits<float>::max();
    cout << b << " " << c << endl;

    int imax = std::numeric_limits<int>::max();
    int imin = std::numeric_limits<int>::min();
    cout << imin << " " << imax << endl;
}
I'm gonna try to make this as simple as possible:  

int = 32 bits:
0000 0000 0000 0000 0000 0000 0000 0000  
left-most bit represents sign, so largest int32 number that you can have is:  
0111 1111 1111 1111 1111 1111 1111 1111 (which is 2147483647)

for negative numbers if left-most bit is set to 1 then to get "human readable"  
number you will have to do complement 2 of that number which is invert every bit  
and add 1.

ex.
1111 1111 1111 1111 1111 1111 1111 1111  <- my number (i know it is negative, -?)  
0000 0000 0000 0000 0000 0000 0000 0000  <- all bits converted  
0000 0000 0000 0000 0000 0000 0000 0001  <- added 1    
=> so that begging number was -1  

Now that you know this, question is what happens if you add 1 to the   
largest positive int32 number (0111 1111 1111 1111 1111 1111 1111 1111) ?  

Well you will get 1000 0000 0000 0000 0000 0000 0000 0000
which is (-2147483648). What happened here was overflow caused by addition.  
This was the case in your program for your 'a' variable.

- As for your confusion with 'inf': 

Floating point numbers work differently then integers.  
I'm not gonna go into too much detail on how bits and bytes are arranged.
Just know that there is :  
 - 1 bit reserved for sign (top-left)   
 - next 8 (for float32) bits for exponent   
 - and the rest for fraction  
Now what is interesting is that there are certain 'special' values defined in
IEEE 754 which are the following:

 - +0 = 0 00000000 00000000000000000000000  
 - -0 = 1 00000000 00000000000000000000000  
 - +inf = 0 11111111 00000000000000000000000
 - -inf = 1 11111111 00000000000000000000000
 - NaN = 0 11111111 00000000000000000000001

As you see these special values are not defined in int type which is the reason  
why you get 'inf' in float and something weird/unexpected in int.

I hope i answered your question.