为什么溢出发生在计算取决于数据类型,当值被分配的类型可以保存它 [英] why overflow happens on calculation depending on the data type when the type where the value is being assigned can hold it

查看:190
本文介绍了为什么溢出发生在计算取决于数据类型,当值被分配的类型可以保存它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早些时候我想出了一些东西,我解决了,但它让我后来
让我们看看一个类似的例子:

Earlier I came up with something, which I solved, but it got me later let's take a look at a similar example of what I was on:

int b = 35000000; //35million
int a = 30000000;
unsigned long n = ( 100 * a ) / b;

输出:4294967260

Output: 4294967260

unsigned long ,正确的85%输出将会出现,因为 code>是一个有符号的32位整数。但这让我后来。在(100 * a)中没有对 a 赋值,只是简单的计算和正确的值是3亿会出现而不是溢出。要了解是否没有真正的赋值 a 我从代码中删除了 a ,并手动写入值

I simply changed a to unsigned long and the correct 85% output would come up, because a is a signed 32bit integer. But this got me later. There is no value assignment to a during ( 100 * a ) there is just simply a calculation and the correct value which is 3billion should come up instead of an overflow. To understand if there wasn't really an assignment to a I removed a from the code and manually write the value instead instead:

int b = 35000000;
unsigned long n = ( 100 * 30000000 ) / b;

最大的惊喜是输出也是:4294967260

3亿的值可以分配给 unsigned long
我的第一个想法是(100 * 30000000)导致溢出,但后来我问溢出什么,没有什么溢出。
然后我将 b 更改为unsigned long,甚至最令人惊讶的是输出是正确的85%。

The big surprise was that the output is also: 4294967260
And of course value of 3billion can be assigned to an unsigned long. My first thought was that ( 100 * 30000000 ) was causing an overflow, but then I asked "an overflow on what? there is nothing to be overflowed". Then I changed b to unsigned long, which even most suprisingly the output was correct 85%.

在第一个示例中将 a 更改为 unsigned long

In the first example changing a to unsigned long

int b = 35000000;
unsigned long a = 30000000;
unsigned long n = ( 100 * a ) / b;

并将 b $ c> int ,因为它是工作,但在第二个例子,它不,发生什么?

and leaving bas an int as it is works, but on the second example it doesn't, what is occuring?

这可能是一个

工作(输出= 85):

Works (Output = 85):

int b = 35000000;
unsigned long a = 30000000;
unsigned long n = ( 100 * a ) / b;

工程(输出= 85):

Works (Output = 85):

unsigned long b= 35000000;
unsigned long n = ( 100 * 30000000 ) / b;

无效(溢出):

int b = 35000000;
int a = 30000000;
unsigned long n = ( 100 * a ) / b;

无效(溢出):

int b = 35000000;
unsigned long n = ( 100 * 30000000 ) / b;


推荐答案

让我解释一下这里发生了什么。
On:

Let me explain what is occuring here.
On:

int b= 35000000;
unsigned long n = ( 100 * 30000000 ) / b;

值不正确,因为溢出发生在(100 * 30000000) code>
但在:

The value is incorrect because overflow happens at ( 100 * 30000000 ) But on:

unsigned long b= 35000000;
unsigned long n = ( 100 * 30000000 ) / b;

值正确,发生了什么?

在第一个例子中,如$ Tony所说, b 是一个 int 其中(100 * 30000000)的临时值将能够保存32位有符号整数,这是因为100是一个int,30000000也是一个int AND ,因为 b 也是一个int,在这种情况下的注册表是聪明的,当所有 c $ c> int 它假定值也必须是 int ,但是当mighty unsigned long 来参加聚会,它知道将 int 除以 unsigned long / b 错误,因此将(100 * 30000000)的值存储到 unsigned long

In the first example b is a int, as said by Tony, an overflow happens because the register where the temporary value of ( 100 * 30000000 )will be assigned is able to hold 32bit signed integers, that happens because 100 is an int and 30000000 is also an int AND because b is also an int, the register in this case are smart, when ALL values on the right side are int it assumes the values also have to be an int but when a mighty unsigned long comes to the party, it knows that dividing an int by an unsigned long, / b is wrong, so it stores the value of ( 100 * 30000000 ) to an unsigned long.

这篇关于为什么溢出发生在计算取决于数据类型,当值被分配的类型可以保存它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆