double值由外部传入

        private void Compare(double value)
{
string text;
if (value != ) //小数位后保留2位
{
//小数点后保留2位小数
text = string.Format("{0:0.00}", value);
}
else
{
text = "";
}
}

http://stackoverflow.com/questions/5658799/is-it-wrong-to-compare-a-double-to-0-like-this-doublevariable-0

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);

http://stackoverflow.com/questions/485175/is-it-safe-to-check-floating-point-values-for-equality-to-0-in-c-net

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.

http://stackoverflow.com/questions/6598179/the-right-way-to-compare-a-system-double-to-0-a-number-int

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的更多相关文章

  1. ZOJ 1125 Floating Point Numbers

    原题链接 题目大意:给一个16位的数字,表示一个浮点数,按照规则转换成科学计数法表示. 解法:注释比较清楚了,注意浮点运算的四舍五入问题. 参考代码: #include<iostream> ...

  2. 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 ...

  3. 关于C#你应该知道的2000件事

    原文 关于C#你应该知道的2000件事 下面列出了迄今为止你应该了解的关于C#博客的2000件事的所有帖子. 帖子总数= 1,219 大会 #11 -检查IL使用程序Ildasm.exe d #179 ...

  4. shell算术运算与进制运算

    (())与let是等效的 arithmetic expression type 与[是等效的 source与.是等效的 其实,Shell(这里是Bash)本身不具备处理浮点计算的能力,但是可以使用“b ...

  5. C# 使用itextsharp 读取pdf中文字坐标

      程序调用: using iTextSharp.text.pdf; using System; using System.Collections.Generic; using System.Linq ...

  6. 【翻译】ExtJS vs AngularJS

    原文:ExtJS vs AngularJS ExtJS和AngularJS是两个行业内领先的富界面开发框架.TechFerry有机会使用Ext JS和Angular JS来开发多个富界面的单页面应用程 ...

  7. .net通过iTextSharp.pdf操作pdf文件实现查找关键字签字盖章

    之前这个事情都CA公司去做的,现在给客户做demo,要模拟一下签字盖章了,我们的业务PDF文件是动态生成的所以没法通过坐标定位,只能通过关键字查找定位了. 之前在网上看了许多通多通过查询关键字,然后图 ...

  8. java Literals

    Primitive Data Types The Java programming language is statically-typed, which means that all variabl ...

  9. Primitive Data Types

    Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics) http ...

随机推荐

  1. [Guava官方文档翻译] 7. Guava的Immutable Collection(不可变集合)工具 (Immutable Collections Explained)

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3538666.html ,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体 ...

  2. HDOJ 1176 免费馅饼 -- 动态规划

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1176 Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小 ...

  3. Linux中的安装神器--yum源安装

    linux配置yum源 一.修改yum的配置文件     /etc/yum.repos.d/xxx.repo          1.进入yum配置文件目录        # cd /etc/yum.r ...

  4. 网站开发常用jQuery插件总结(一)提示插件alertify

    1.alertify插件功能 主要实现提示功能,用于代替js中的alert,confirm,prompt,显示友好的提示框 2.alertify官方地址 http://fabien-d.github. ...

  5. css3太极图效果+自动旋转

    主要使用border-radius属性实现圆,半圆,定位坐标覆盖部分模块. 半圆: width: 50%; height: 100%; border-radius:100% 0 0 100% /50% ...

  6. PHP中如何设置error_reporting错误报告级别

    错误报告级别:指定了在什么情况下,脚本代码中的错误(这里的错误是广义的错误,包括E_NOTICE注意.E_WARNING警告.E_ERROR致命错误等)会以错误报告的形式输出.   设置错误报告级别的 ...

  7. Yii 配置默认controller和action

    设置默认controller 在/protected/config/main.php添加配置 <?php return array( 'name'=>'Auto', 'defaultCon ...

  8. python log 层次结构

    文件结构 - run.py - b -- __init__.py run.py import logging import b log = logging.getLogger("" ...

  9. UILocalNotification本地通知

    // 执行通知一定要退出应用或挂起应用(进入后台)才能收到通知. 1.在iOS8及其以后版本中使用本地消息需要先获得用户的许可,否则无法成功注册本地消息.因此,我们将询问用户许可的代码片段添加到了ap ...

  10. Git权威指南 读笔(1)

    第四章 Git初始化: 设置Git当前用户和邮件地址: $ git config --global user.name $ git config --global user.email 设置Git命令 ...