如何使两个不同长度的双精度变量相等
How to make two double variables with different length become equal
我是Visual C++的新手,我有两个双变量,比如说AA = 10.650406
b = 10.65040606439
,我怎样才能使它们相等?
这是一个例子
AA = 10.650406;
if (a == tempfunc(zz)) 执行真函数还 exceute falsefunction
变量 AA 是双精度值,而函数 tempfunc 返回双精度值,如果值 AA 为 10.65040606439,而 tempfunc 的返回值为 10.65040606439。 问题是如何使这些值相等,以便我可以执行函数 TRUEFUNCTION
典型的解决方案是使用"epsilon 值"比较差异。类似的东西
const double eps = 0.000001; // Adjust this to match the "perecision" you need.
if (abs(a-b) < eps)
{
// Values are almost equal
}
我想你的问题是:如何对这两个数字进行检查,但不是对所有数字都适用..:
10.123 == 10.1234 (TRUE)
10.123 == 11.123 (FALSE)
如果小数点分隔符后有固定位数(示例中为 6),则可以执行以下操作:
int a2 = a * 10e6;
int b2 = b * 10e6; // (conversion to integers; so the digits smaller than the 6th digit will be removed)
现在您可以检查:
if (a2 == b2)
{
//do your thing
}
总之:
if ( ((int) a*10e6) == ((int) b*10e6))
{
//do your thing
}
相关文章:
- 在 C++ 中将双精度变量写入二进制文件
- 将双精度变量设置为另一个变量的值
- 将双精度值转换为 char 变量时字符串流如何工作
- 从二进制流中读取时,将双精度变量的地址转换为 char* 意味着什么?
- 警告:左移计数 >= 在 C++ 中将字节流读取为双精度变量时的类型宽度
- 为什么双精度引用值在分配给C++中的浮点变量时不会更改
- 使用词法强制转换在'string to double'中设置双精度变量的精度
- 整数和双精度变量的初始化
- 将 Int 指针分配给双精度变量
- 如何在 for 循环内部或外部打印双精度变量
- 通过更改代码格式来计算结构向量中包含的双精度变量的平均值的不同结果?
- 如何让用户输入正双精度变量
- 小数点后的数字,用于双精度变量/C++计算
- 对应该返回的双精度变量进行舍入,而不是打印
- 删除双精度变量中的尾随零
- 我可以控制调试器窗口中显示的浮点数和双精度变量的位数吗?
- 使用 cv::fileStorage 保存双精度变量
- 将长双精度对象存储在多个双精度变量中
- vc.net:无法设置双精度变量的值
- 如何使两个不同长度的双精度变量相等