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

时间:2021-10-27 02:14:24
对于有符号整数的溢出,只需要简单判断运算结果符号是否与操作数相等即可;下面我们讨论无符号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/