确保c ++十进制比较是正确的 [英] Make sure c++ decimal comparison is correct

查看:155
本文介绍了确保c ++十进制比较是正确的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个双变量。双a = 0.10000,双b = 0.1。如何确保比较(a == b)始终是真的?

解决方案

如果您对 == doubles floatats (你应该是)你可以随时检查它们是否在一个很小的公差范围内。

  bool same = fabs(ab)< 0.000001; 


I have two double variable. double a = 0.10000, double b = 0.1. How can I make sure the comparison (a == b) is always true ?

解决方案

If you are being paranoid about using == on doubles or floats (which you should be) you can always check that they are close within a small tolerance.

bool same = fabs(a-b) < 0.000001;

这篇关于确保c ++十进制比较是正确的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆