惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy & Cybersecurity Law Blog
V
V2EX
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
P
Privacy International News Feed
The Last Watchdog
The Last Watchdog
Security Archives - TechRepublic
Security Archives - TechRepublic
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
K
Kaspersky official blog
S
Secure Thoughts
T
Tenable Blog
Security Latest
Security Latest
The Cloudflare Blog
S
Security @ Cisco Blogs
H
Heimdal Security Blog
aimingoo的专栏
aimingoo的专栏
TaoSecurity Blog
TaoSecurity Blog
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
IT之家
IT之家
Latest news
Latest news
The Hacker News
The Hacker News
C
Check Point Blog
T
The Exploit Database - CXSecurity.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
腾讯CDC
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
N
News | PayPal Newsroom
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
S
Security Affairs
S
Securelist
Google Online Security Blog
Google Online Security Blog
WordPress大学
WordPress大学
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
A
About on SuperTechFans

博客园 - 火星的森林

快到清明了 AquaFold.Data.Studio.v6.5 期待已久的星际争霸II 初次接触unix 呵呵 咋说呐 全国DNS和ADSLVPI VCI 整理版 计数器 终于有点时间了 好久没写blog了 找点资料 初学ASP.NET,搞定一个frames间控制的问题 又开始工作了~! 辞职ed 使用.net下的系统事件增强应用程序(zt) [转载]魔鬼的颤音 准备写个nhibernate的学习笔记 程序员常见保健方法(zt) 一点流水帐
开发一个统计组件时候遇到的一个问题
火星的森林 · 2005-04-27 · via 博客园 - 火星的森林

开发一个统计组件时候遇到的一个问题
即浮点运算精度的问题,刚刚在cnblogs首页看见有人提出的一个解决方案(请见《第一次遇到浮点运算精度不够的问题 》),想起自己也做过类似工作,于是写下这篇文字,给需要的朋友一个参考。
下面是我写的一个求和函数(之一):
-------------------------------
/// <summary>
  /// 求数组和,指定精度
  /// </summary>
  /// <param name="dDataArray">要求和的double数组</param>
  /// <param name="iPrecision">要保留的精度长度</param>
  /// <returns></returns>
  public double Sum(double[] dDataArray, int iPrecision)
  {
   if ( dDataArray == null || dDataArray.Length < 1)
    return double.NaN ;

   int pow = (int)Math.Pow( 10, iPrecision);
   double dSum = 0;
   //精确相加
   foreach( double d in dDataArray)
   {
    dSum += d;
    //精确度处理
    dSum = Math.Floor( dSum * pow ) / pow;
   }

   return dSum;
  }
-------------------------------
关键就在于Math.Floor()这个函数了,这个函数的作用是“返回小于或等于指定数字的最大整数”。
如《第》文中例子“100.85-100.5=0.35 这么一目了然的操作,计算的结果却是0.349999999999994”,作者的“解决办法是把 double 类型的值放大 10000 倍,然后转成 long 长整型,中间计算过程全部用 long 型,最后把计算结果再转回 double 型并除以 10000 得到最终运算结果”,其实我这段代码也是一个意思,只是利用.net自带的一个函数,中间呢,没有如《第》文中写的那些数据类型转换(也许这个floor函数自己内部有转换吧,呵呵,那就不是俺的事了)。

不好意思,不会写文章,大家将就着看吧。