c#:两个双数字问题的总和 [英] c#: sum of two double numbers problem

查看:100
本文介绍了c#:两个双数字问题的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

为什么C#中的浮点算术不精确?


嗨。我有以下问题:

  43.65 + 61.11 = 104.75999999999999 
 (十进制)43.65+(小数位数)

小数点)61.11 = 104.76

为什么双重错误?

解决方案

这个问题及其答案是丰富的信息 - .NET中的decimal,float和double之间的差异



To引用:



  • 对于自然准确
    小数的值,使用十进制。
    这通常适用于人类发明的任何
    概念:财务
    值是最明显的例子,
    ,但也有其他。考虑给潜水员或滑冰运动员的
    分数,例如


  • 对于
    的更多文物的价值无论如何,无法真正衡量的性质
    ,float / double更适合
    。例如,科学的
    数据通常将以
    表示。在这里,原始值
    将不会十分准确地启动
    ,所以对于
    预期结果来说,维持
    十进制精度并不重要。浮动二进制
    点类型的工作速度要快于
    而不是小数位数。




Possible Duplicate:
Why is floating point arithmetic in C# imprecise?

Hi. I've got following problem:

43.65+61.11=104.75999999999999

for decimal is correct:

(decimal)43.65+(decimal)61.11=104.76

Why result for double is wrong?

解决方案

This question and its answers are a wealth of info on this - Difference between decimal, float and double in .NET?

To quote:

  • For values which are "naturally exact decimals" it's good to use decimal. This is usually suitable for any concepts invented by humans: financial values are the most obvious example, but there are others too. Consider the score given to divers or ice skaters, for example.

  • For values which are more artefacts of nature which can't really be measured exactly anyway, float/double are more appropriate. For example, scientific data would usually be represented in this form. Here, the original values won't be "decimally accurate" to start with, so it's not important for the expected results to maintain the "decimal accuracy". Floating binary point types are much faster to work with than decimals.

这篇关于c#:两个双数字问题的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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