整数乘法 [英] integer multiplication
问题描述
如果多个两个整数并且结果溢出了C中的
MAX_INT,会发生什么?有没有办法在条件发生时捕获它?
What happens if you multiple two integers and the result overflows the
MAX_INT in C? Is there a way to trap the condition when it happens?
推荐答案
Pesso写道:
Pesso wrote:
如果多个两个整数并且结果溢出了C中的
MAX_INT,会发生什么?
What happens if you multiple two integers and the result overflows the
MAX_INT in C?
有符号整数溢出时的行为取决于实现。
无符号整数不会溢出,但显示环绕行为
根据模2 ^ n算术。
Behaviour when signed integers overflow is implementation dependant.
Unsigned integers do not overflow, but exhibit "wrap-around" behaviour
according to modulo 2^n arithmetic.
有没有办法在条件发生时捕获条件?
Is there a way to trap the condition when it happens?
你必须看到你的实现文档。
标准并没有坚持陷阱。但请注意,在尝试之前,您可以轻松地检查特定操作是否会导致溢出或环绕
。这很乏味,但可以做到。
You''ll have to see your implementation''s documentation for this. The
Standard doesn''t insist on a trap. Note though, that you can easily
check if a particular operation will cause overflow or wrap-around
before attempting it. It''s tedious, but it can be done.
santosh写道:
santosh wrote:
Pesso写道:
Pesso wrote:
如果多个两个整数会导致结果溢出
MAX_INT在C中?
What happens if you multiple two integers and the result overflows the
MAX_INT in C?
有符号整数溢出时的行为取决于实现。
Behaviour when signed integers overflow is implementation dependant.
[...]
对不起,根据标准,它实际上是未定义的行为。 />
< snip>
[ ... ]
Sorry, it''s actually undefined behaviour, according to the Standard.
<snip>
Pesso说:
Pesso said:
如果多个两个整数并且结果溢出了C中的
MAX_INT,会发生什么?
What happens if you multiple two integers and the result overflows the
MAX_INT in C?
假设你的意思是整数和INT_MAX,行为是未定义的。
Assuming you mean ints and INT_MAX, the behaviour is undefined.
有没有办法陷阱它发生时的情况?
Is there a way to trap the condition when it happens?
不可移植,不。正确的技巧不是让它发生在第一名的
。这很容易避免。
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk
电子邮件:rjh在上述域名中, - www。
Not portably, no. The proper technique is not to let it happen in the
first place. It''s easy enough to avoid.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
这篇关于整数乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!