检查C ++中的下溢/溢出? [英] Checking for underflow/overflow in C++?
本文介绍了检查C ++中的下溢/溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个通用的方法来检查给定数据类型(uint32,int等)的溢出或下溢吗?
我正在这样做:
uint32 a,b ,C;
... //初始化a,b,c
if(b a - =(c-b)
}
当我打印一些迭代后,它显示一个大数字,如:4294963846。
code> uint32 a,b;
//赋值
uint32 result = a + b;
if(result< a){
//溢出
}
您的具体检查将是:
if(a>(cb)){
/ / underflow
}
Is there a general way to check for an overflow or an underflow of a given data type (uint32, int etc.)?
I am doing something like this:
uint32 a,b,c;
... //initialize a,b,c
if(b < c) {
a -= (c - b)
}
When I print a after some iterations, it displays a large number like: 4294963846.
解决方案
To check for over/underflow in arithmetic check the result compared to the original values.
uint32 a,b;
//assign values
uint32 result = a + b;
if (result < a) {
//Overflow
}
For your specific the check would be:
if (a > (c-b)) {
//Underflow
}
这篇关于检查C ++中的下溢/溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文