为什么我使用浮动时,我得到错误的结果呢? [英] Why am I getting the wrong result when using float?
本文介绍了为什么我使用浮动时,我得到错误的结果呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能显示的文件:结果
的为什么(双)0.6f> (双)(6 / 10F)?结果
的在C#中不精确为什么浮点运算
我在C#下面的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StackOverflow
{
class Program
{
static void Main(string[] args)
{
float num1 = 17.03F;
float num2 = 17F;
float result = num1 - num2;
Console.WriteLine(result);
}
}
}
中的代码工作正常,但我我没有得到预期的结果。 ?有人可以解释为什么发生这种情况。
The code works fine but I am not getting the expected result. Can someone explain why this is happening?
推荐答案
我想你指的偏差导致通过的浮点算术。您可以在提供的链接有关阅读。
I guess you are refering to deviations cause by Floating point arithmetics. You can read about in in the provided link.
如果你真的需要进行计算100%的准确,你可以使用小数
,而不是浮动
。
If you really need to make the calculation 100% accurate, you can use decimal
instead of float
.
这篇关于为什么我使用浮动时,我得到错误的结果呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文