C++如何判断运算结果溢出
来源:互联网 发布:erp制作软件 编辑:程序博客网 时间:2024/06/14 00:03
对于有符号整数的溢出,只需要简单判断运算结果符号是否与操作数相等即可;下面我们讨论无符号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>
来自CODE的代码片JudgeOveFlow.cpp
参考:http://www.cplusplus.com/articles/DE18T05o/
0 0
- C++如何判断运算结果溢出
- C++如何判断运算结果溢出
- 如何判断整型算数运算是否溢出
- C语言中无符号数运算判断溢出
- 如何判断除法运算结果是否为整数
- C运算溢出怎么办?
- 补码运算溢出判断方法
- 补码加法运算溢出判断
- C语言溢出判断
- 整数溢出,如何判断整数溢出
- 整数溢出,如何判断整数溢出
- 整数溢出,如何判断整数溢出
- 整数溢出,如何判断整数溢出
- UCOS2如何判断堆栈溢出
- 补码加减运算及溢出判断
- 溢出判断(C语言)
- 如何判断结果中有空
- 如何判断有符号数的溢出
- Python 中 unittest(单元测试)的学习
- Android 基于google Zxing实现二维码、条形码扫描,仿微信二维码扫描效果
- bacula备份工具
- 乐观锁和悲观锁的区别
- Vc中关于DC(设备环境函数(Device Context))的理解
- C++如何判断运算结果溢出
- 修改Eclipse默认workspace目录
- Android 基于google Zxing实现对手机中的二维码进行扫描
- Excel 单元格合并
- C++缓冲区溢出
- iOS常用宏定义
- Git fetch和git pull的区别
- 微信公众平台开发(一) 配置接口
- 连连看消除算法的C++代码实现