comparison of floating point numbers with equality operator. possible loss of precision while rounding values
double值由外部传入
private void Compare(double value)
{
string text;
if (value != ) //小数位后保留2位
{
//小数点后保留2位小数
text = string.Format("{0:0.00}", value);
}
else
{
text = "";
}
}
Nope it's perfectly legal if you are only going to compare against 0 as the right side of comparison will automatically casted to double. On the other hand, it would yield all the round-off errors if you where to compare against == 0.10000001
You are better or reading the discussion about float to 0 comparison here: C#.NET: Is it safe to check floating point values for equality to 0?
Also this discussion is very informative about weird precision problems on floats: Why the result is different for this problem?
i.e. below will yield false:
double d1 = 1.000001; double d2 =0.000001;
Console.WriteLine((d1-d2)==1.0);
It is safe to expect that the comparison will return true if and only if the double variable has a value of exactly 0.0 (which in your original code snippet is, of course, the case). This is consistent with the semantics of the == operator. a == b means "a is equal to b".
It is not safe (because it is not correct) to expect that the result of some calculation will be zero in double (or, more generally, floating point) arithmetics whenever the result of the same calculation in pure Mathematics is zero. This is because when calculations come into the ground, floating point precision error appears - a concept which, needless to say, does not exist in Real number arithmetics in Mathematics.
Well, how close do you need the value to be to 0? If you go through a lot of floating point operations which in "infinite precision" might result in 0, you could end up with a result "very close" to 0.
Typically in this situation you want to provide some sort of epsilon, and check that the result is just within that epsilon:
if (Math.Abs(something) < 0.001)
The epsilon you should use is application-specific - it depends on what you're doing.
Of course, if the result should be exactly zero, then a simple equality check is fine.
comparison of floating point numbers with equality operator. possible loss of precision while rounding values的更多相关文章
- ZOJ 1125 Floating Point Numbers
原题链接 题目大意:给一个16位的数字,表示一个浮点数,按照规则转换成科学计数法表示. 解法:注释比较清楚了,注意浮点运算的四舍五入问题. 参考代码: #include<iostream> ...
- public static float CompareExchange(ref float location1,float value,float comparand)
https://msdn.microsoft.com/en-us/library/k9hz8w9t(v=vs.110).aspx Compares two single-precision float ...
- 关于C#你应该知道的2000件事
原文 关于C#你应该知道的2000件事 下面列出了迄今为止你应该了解的关于C#博客的2000件事的所有帖子. 帖子总数= 1,219 大会 #11 -检查IL使用程序Ildasm.exe d #179 ...
- shell算术运算与进制运算
(())与let是等效的 arithmetic expression type 与[是等效的 source与.是等效的 其实,Shell(这里是Bash)本身不具备处理浮点计算的能力,但是可以使用“b ...
- C# 使用itextsharp 读取pdf中文字坐标
程序调用: using iTextSharp.text.pdf; using System; using System.Collections.Generic; using System.Linq ...
- 【翻译】ExtJS vs AngularJS
原文:ExtJS vs AngularJS ExtJS和AngularJS是两个行业内领先的富界面开发框架.TechFerry有机会使用Ext JS和Angular JS来开发多个富界面的单页面应用程 ...
- .net通过iTextSharp.pdf操作pdf文件实现查找关键字签字盖章
之前这个事情都CA公司去做的,现在给客户做demo,要模拟一下签字盖章了,我们的业务PDF文件是动态生成的所以没法通过坐标定位,只能通过关键字查找定位了. 之前在网上看了许多通多通过查询关键字,然后图 ...
- java Literals
Primitive Data Types The Java programming language is statically-typed, which means that all variabl ...
- Primitive Data Types
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics) http ...
随机推荐
- 经历:如何设置jquery easyui中下拉框不可编辑
今天,在项目中碰到一个这样的问题,当选择按钮时候,查询条件是可以输入的,否则,表单框是不可用的[图1].但是,批量查询中的船名和装港用到了自动配置,即jquery-easyui中的combox的配置. ...
- 12天学好C语言——记录我的C语言学习之路(Day 2)
12天学好C语言--记录我的C语言学习之路 Day 2: 我建议大家每一天学习之前都仅凭记忆去敲前一天敲过的最后一个程序,或者敲前一天你认为最难最长的一个程序,如果一晚上的睡眠之后不看书还能敲的出来, ...
- 学习C++ Primer 的个人理解(十一)
关联容器 就像是个字典, 其元素是 键 - 值 对. 关键字起到索引作用. 有序: map:关联数组:保存 健-值 对 set : 关键字既是值. multimap : 关键字可重复出现的map mu ...
- 构造函数为什么不能是虚函数 ( 转载自C/C++程序员之家)
从存储空间角度,虚函数对应一个指向vtable虚函数表的指针,这大家都知道,可是这个指向vtable的指针其实是存储在对象的内存空间的.问题出来了,如果构造函数是虚的,就需要通过 vtable来调用, ...
- nginx 默认会把header里的参数去掉下划线
做token验证的时候遇到问题:在本地可以获取前端header传的参数,但是部署到服务器获取的就是null(服务器地址用nginx做了代理) 原因: nginx代理默认会把header的参数的 &qu ...
- hdu 5094 Maze 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...
- IE兼容性问题
1.H5标签兼容.解决:js:document.createElement("footer");css:display: block;或者直接使用 html5shiv.js ...
- PHP学习心得(七)——常量
常量的范围是全局的. 可以用 define() 函数来定义常量. <?php define("CONSTANT", "Hello world."); ec ...
- OpenSessionInViewFilter与org.springframework.dao.InvalidDataAccessApiUsageException
报错:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in r ...
- SET QUOTED_IDENTIFIER ON
语法 SET QUOTED_IDENTIFIER { ON | OFF } 注释 当 SET QUOTED_IDENTIFIER 为 ON 时,标识符可以由双引号分隔,而文字必须由单引号分隔 ...