C++如何判断运算结果溢出

831 篇文章 16 订阅
300 篇文章 0 订阅
对于有符号整数的溢出,只需要简单判断运算结果符号是否与操作数相等即可;下面我们讨论无符号integer的溢出检测问题:
假设我们有两个变量a和b,size 为n,最大值为R。'+'代表实际的数学运算符--加号,‘$’代表计算机中的运算。
加法溢出
显然如果a+b<=R-1;a$b=a+b;
如果a+b>=R;a$b=a+b-R; 分析:由于R比a和b都大,所以a-R与b-R都是负值,所以我们有a+b-R<a;a+b-R<b;
乘法溢出
1.如果a*b>max,则有a>max/b;(如果无符号,max=R-1;否则max=R/2-1)
<a target=_blank id="L1" href="http://blog.csdn.net/mxw976235955/article/details/24003943#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a>
<a target=_blank id="L2" href="http://blog.csdn.net/mxw976235955/article/details/24003943#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a>
<a target=_blank id="L3" href="http://blog.csdn.net/mxw976235955/article/details/24003943#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a>
<a target=_blank id="L4" href="http://blog.csdn.net/mxw976235955/article/details/24003943#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a>
<a target=_blank id="L5" href="http://blog.csdn.net/mxw976235955/article/details/24003943#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a>
<a target=_blank id="L6" href="http://blog.csdn.net/mxw976235955/article/details/24003943#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a>
<a target=_blank id="L7" href="http://blog.csdn.net/mxw976235955/article/details/24003943#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a>
<a target=_blank id="L8" href="http://blog.csdn.net/mxw976235955/article/details/24003943#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a>
<a target=_blank id="L9" href="http://blog.csdn.net/mxw976235955/article/details/24003943#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a>
<a target=_blank id="L10" href="http://blog.csdn.net/mxw976235955/article/details/24003943#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a>
<a target=_blank id="L11" href="http://blog.csdn.net/mxw976235955/article/details/24003943#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
<a target=_blank id="L12" href="http://blog.csdn.net/mxw976235955/article/details/24003943#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a>
<a target=_blank id="L13" href="http://blog.csdn.net/mxw976235955/article/details/24003943#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a>
<a target=_blank id="L14" href="http://blog.csdn.net/mxw976235955/article/details/24003943#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
           
           
int is_mul_overflow ( int a , int b ) {
if ( a >= 0 && b >= 0 ) {
return INT_MAX / a < b ;
}
else if ( a < 0 && b < 0 ) {
return INT_MAX / a > b ;
}
else if ( a * b == INT_MIN ) {
return 0 ;
}
else {
return a < 0 ? is_mul_overflow ( - a , b ) : is_mul_overflow ( a , - b );
}
}
 来自CODE的代码片
JudgeOveFlow.cpp

参考:http://www.cplusplus.com/articles/DE18T05o/
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值