简体   繁体   English

为什么我不能将大值传递给Int32?

[英]Why can't I pass a large value as an Int32?

I have a number: 94,800,620,800 我有一个号码:94,800,620,800

Float is 4-byte data-type. Float是4字节数据类型。 Int32 is also 4-byte data-type. Int32也是4字节数据类型。

float f = 94800620800; // ok
Int32 t = 94800620800; // error

Please explain this problem. 请解释这个问题。 Why I get a error when using Int32. 为什么我在使用Int32时出错。 Why I can use this number for float data-type because both of them are 4-byte data-type. 为什么我可以将此数字用于float数据类型,因为它们都是4字节数据类型。 Thanks. 谢谢。

Because the number you are trying to assign is larger than the largest possible value for a number of type Int32 , which happens to be 2,147,483,647. 因为您尝试分配的数字大于Int32类型的最大可能值 ,恰好是2,147,483,647。 To note, the maximum value for a Single is 3.402823 × 10 38 . 需要注意的是, Single的最大值为3.402823×10 38

The max value for Int32 is 2,147,483,647 - which is less than 94,800,620,800. Int32的最大值为2,147,483,647 - 小于94,800,620,800。

A float can take a value in the following range: ±1.5 × 10−45 to ±3.4 × 1038 浮点值可以取以下范围内的值:±1.5×10-45到±3.4×1038

Also, check out this SO question - what the difference between the float and integer data type when the size is same in java? 另外,看看这个SO问题 - 当java中的大小相同时,浮点数和整数数据类型之间有什么区别? . It's a Java question, but the concept is the same and there's a detailed explanation of the difference, even though they're the same size. 这是一个Java问题,但概念是相同的,并且有差异的详细解释,即使它们的大小相同。

Because that number is too big for a 4 byte int. 因为这个数字对于一个4字节的int来说太大了。 Scalar values like Int32 have a minimum and maximum limit (which are -2 31 and 2 31 - 1 in this case, respectively), and you simply can't store a value outside this range. Int32这样的标量值有一个最小和最大限制(在这种情况下分别为-2 31和2 31 - 1),并且您根本无法存储超出此范围的值。

Floating point numbers are stored totally differently, so you won't get compiler errors with huge values, only possible precision problems later, during runtime. 浮点数的存储方式完全不同,因此您不会在运行时期间获得具有巨大值的编译器错误,只会出现可能的精度问题。

Because of those types internal representation. 因为那些类型的内部表示。

float uses something like i,d ^ n where i is the integral part, d is the decimal part and n is the exponent (of course, this happens in base 2). float使用类似i,d ^ n的东西,其中i是整数部分,d是小数部分,n是指数(当然,这发生在基数2中)。

In this way, you can store bigger numbers in a float , but it won't be accurate as a, say, Int32 in storing integral numbers. 通过这种方式,您可以在float存储更大的数字,但在存储整数时,它不会像Int32那样准确。 If you try to convert 如果你试图转换

float f = 94800620800; 

to an integral type large enough to store its value, it may not be the same as the initial 94800620800. 对于足以存储其值的整数类型,它可能与初始94800620800不同。

Maybe you should read the error message? 也许你应该阅读错误信息? ;) ;)

Error   Integral constant is too large

the maximum valiue of a 32 bit int is 2,147,483,647 32位int的最大值为2,147,483,647

the float on the other hand works because it stores a mantissa and exponent, rather than just a single number, so it can cope with a much bigger range at the expense of possibly losing precision 另一方面,浮点数是有效的,因为它存储了一个尾数和指数,而不仅仅是一个数字,所以它可以应对更大的范围,但代价是可能会失去精度

try printing out your float and you will get 94800620000 instead of 94800620800 as the lower bits are lost 尝试打印你的浮动,你会得到94800620000而不是94800620800因为低位丢失

Integers types are exact representations, while floating point numbers are a combination of significant digits and exponent. 整数类型是精确表示,而浮点数是有效数字和指数的组合。

The floating point wiki page is explaining how this works. 浮点维基页面解释了它的工作原理。

Int32 has a max value of 2,147,483,647. Int32的最大值为2,147,483,647。 Your value is much higher. 你的价值要高得多。

Take a look at system.int32.maxvalue 看看system.int32.maxvalue

提供的常量表达式值不在int数据类型的范围内。

The range of Int32 goes from − 2,147,483,648 to 2,147,483,647. Int32的范围从 - 2,147,483,648到2,147,483,647。 Your variable is way out of range. 你的变量超出了范围。

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

相关问题 对于文本框中的Int32,值太大或太小 - Value was either too large or too small for an Int32 in Textbox 为什么我不能为 int32 分配 32 位的二进制文字? - Why can I not assign a binary literal of 32 bits for a int32? 我可以将UInt32,Int32和浮点值存储在double中而不会丢失信息吗? - Can i store a UInt32, Int32 and a float value inside a double without losing information? 为什么var Int32不是List <Int32> 在这个例子中 - Why is var Int32 not List<Int32> in this example 为什么我们不能使用GetConstructor找到Int32的默认构造函数? - Why can't we find Int32's Default Constructor using GetConstructor? 为什么我得到NaN不是Int32错误的有效值? - Why am I getting a NaN is not a valid value of Int32 error? 如何创建一个泛型类型,其中T也可以是int32,但不仅仅是一个类 - How can I create a generic type where T also can be int32,but not only a class 为什么Int32的最大值为0x7FFFFFFF? - Why is Int32's maximum value 0x7FFFFFFF? 由于Int32是一个值类型,为什么它继承.ToString()? - Since Int32 is a value type why does it inherit .ToString()? 为什么是列表的.Count <T> (Int32)初始大小为0? - Why is the .Count of a List<T>(Int32) with initial size 0?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM