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

发布于 2024-12-03 20:51:37 字数 240 浏览 1 评论 0原文

我有一个数字:94,800,620,800

Float 是 4 字节数据类型。 Int32 也是 4 字节数据类型。

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

请解释一下这个问题。为什么我在使用 Int32 时出现错误。为什么我可以使用这个数字作为浮点数据类型,因为它们都是 4 字节数据类型。谢谢。

I have a number: 94,800,620,800

Float is 4-byte data-type.
Int32 is also 4-byte data-type.

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

Please explain this problem. Why I get a error when using Int32. Why I can use this number for float data-type because both of them are 4-byte data-type. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

撩人痒 2024-12-10 20:51:37

因为您尝试分配的数字大于最大可能值 对于 Int32 类型的数字,恰好是 2,147,483,647。请注意,Single 的最大值为 3.402823 × 1038

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. To note, the maximum value for a Single is 3.402823 × 1038.

ぃ弥猫深巷。 2024-12-10 20:51:37

Int32 的最大值为 2,147,483,647 - 小于 94,800,620,800。

浮点数可以取以下范围内的值:±1.5 × 10−45 到 ±3.4 × 1038

另外,请查看这个SO问题 - 当大小相同时,浮点和整数数据类型之间有什么区别java?。这是一个 Java 问题,但概念是相同的,并且有对差异的详细解释,即使它们大小相同。

The max value for Int32 is 2,147,483,647 - which is less than 94,800,620,800.

A float can take a value in the following range: ±1.5 × 10−45 to ±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?. 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.

小苏打饼 2024-12-10 20:51:37

因为这个数字对于 4 字节 int 来说太大了。像 Int32 这样的标量值具有最小和最大限制(在本例中分别为 -231 和 231 - 1),并且您根本无法存储超出此范围的值。

浮点数的存储方式完全不同,因此您不会在运行时遇到具有巨大值的编译器错误,而只会在稍后出现精度问题。

Because that number is too big for a 4 byte int. Scalar values like Int32 have a minimum and maximum limit (which are -231 and 231 - 1 in this case, respectively), and you simply can't store a value outside this range.

Floating point numbers are stored totally differently, so you won't get compiler errors with huge values, only possible precision problems later, during runtime.

深海不蓝 2024-12-10 20:51:37

由于这些类型的内部表示。

float 使用类似 i,d ^ n 的东西,其中 i 是整数部分,d 是小数部分,n 是指数(当然,这发生在基数 2 中)。

通过这种方式,您可以在 float 中存储更大的数字,但它在存储整数方面不会像 Int32 那样准确。如果您尝试转换

float f = 94800620800; 

为足够大的整数类型来存储其值,则它可能与初始的 94800620800 不同。

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).

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. 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.

擦肩而过的背影 2024-12-10 20:51:37

整数类型是精确表示,而浮点数是有效数字和指数的组合。

浮点维基页面正在解释其工作原理。

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.

萌吟 2024-12-10 20:51:37

也许您应该阅读错误消息? ;)

Error   Integral constant is too large

32 位 int 的最大值是 2,147,483,647,

另一方面,float 可以工作,因为它存储尾数和指数,而不仅仅是单个数字,因此它可以处理更大的范围,但可能会损失精度

尝试打印出你的浮动,你会得到 94800620000 而不是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

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

笨死的猪 2024-12-10 20:51:37

Int32 的最大值为 2,147,483,647。你的价值要高得多。

看一下 system.int32.maxvalue

Int32 has a max value of 2,147,483,647. Your value is much higher.

Take a look at system.int32.maxvalue

浊酒尽余欢 2024-12-10 20:51:37

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

Provided value of the constant expression is not within the range of int datatype.

尹雨沫 2024-12-10 20:51:37

Int32 的范围从 − 2,147,483,648 到 2,147,483,647。您的变量超出范围。

The range of Int32 goes from − 2,147,483,648 to 2,147,483,647. Your variable is way out of range.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文