首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从减去两个双数中得到0?

如何从减去两个双数中得到0?
EN

Stack Overflow用户
提问于 2016-01-18 22:21:05
回答 2查看 191关注 0票数 2

由于某种原因,我得到了2.7234235252E-17,而不是我的程序减去.1 - .1时的0,为什么呢?我怎么才能正确地减去呢?作为第三个问题,如果我减去.1 - .1,bool认为它是0吗?例如:0 == .1 - .1 = true还是false?

代码语言:javascript
运行
复制
 List<string> hd1List = new List<string>();



        double hd1CD = 0;
        double hd2CD = 0;
        double hd3CD = 0;
        double hd4CD = 0;


       // for (int i = 0; i < 10; i++)
       // {


            if (hd1CD <= 0)
            {

                hd1List.Add("q");

                hd1CD = 4;
                hd1CD = hd1CD - .7;

            }


            Console.WriteLine(hd1CD);
            Console.WriteLine(hd2CD);
            Console.WriteLine(hd3CD);
            Console.WriteLine(hd4CD);


        if (hd2CD <= 0 && hd1CD >= .7)
            {

                hd1List.Add("e");
                hd2CD = 1;
                hd1CD = hd1CD - .7;
                hd2CD = hd2CD - .7;
            }

        Console.WriteLine(hd1CD);
        Console.WriteLine(hd2CD);
        Console.WriteLine(hd3CD);
        Console.WriteLine(hd4CD);

        if (hd3CD <= 0 && hd1CD >= .2 && hd2CD >= .2)
            {
                hd1List.Add("r");
                hd3CD = 5;
                hd1CD = hd1CD - .2;
                hd2CD = hd2CD - .2;
                hd3CD = hd3CD - .2;

            }

        Console.WriteLine(hd1CD);
        Console.WriteLine(hd2CD);
        Console.WriteLine(hd3CD);
        Console.WriteLine(hd4CD);

        if (hd4CD <= 0 && hd1CD >= .1 && hd2CD >= .1 && hd3CD >= .1)
            {
                hd1List.Add("w");
                hd4CD = 3;
                hd1CD = hd1CD - .1;
                hd2CD = hd2CD - .1;
                hd3CD = hd3CD - .1;
                hd4CD = hd4CD - .1;

            }


        Console.WriteLine(hd1CD);
        Console.WriteLine(hd2CD);
        Console.WriteLine(hd3CD);
        Console.WriteLine(hd4CD);

 if (hd4CD < 1.2 || hd1CD < 1.2 || hd2CD < 1.2 || hd3CD < 1.2 && hd4CD != 0 && hd1CD != 0 && hd2CD != 0 && hd3CD !=0)
The above is checking whether the variables hold 0, since im getting epsilon values my program cant run correctly...
            {
                //get minimum
                hd1List.Add("extra" + superList.Min());

                hd1CD = hd1CD - superList.Min();
                hd2CD = hd2CD - superList.Min();
                hd3CD = hd3CD - superList.Min();
                hd4CD = hd4CD - superList.Min();

            }

摘要:我如何重新排列我的代码,以便如果我减去.1 - .1,它=0而不是一个可笑的数字,我不需要建议,我需要一个例子。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-19 00:35:17

虽然你有一个很好的答案,我想我可以尝试重构你的代码。试一试:

代码语言:javascript
运行
复制
List<string> hd1List = new List<string>();
double[] hdCDs = new double[4];
double EPSILON = 0.00000001;

Action<int, double, double, string> update = (i, v, d, t) =>
{
    if (hdCDs[i] <= 0.0 && hdCDs.Take(i).All(x => x >= d))
    {
        hd1List.Add(t);
        hdCDs[i] = v;
        for (int x = 0; x <= i + 1; x++)
        {
            hdCDs[x] =- d;
        }
    }
    Console.WriteLine(String.Join(Environment.NewLine, hdCDs));
};

update(0, 4.0, 0.7, "q");
update(1, 1.0, 0.7, "e");
update(2, 5.0, 0.2, "r");
update(3, 4.0, 0.1, "w");

if (hdCDs.Any(x => x < 1.2) && hdCDs.All(x => x <= EPSILON))
{
    var min = superList.Min();
    hd1List.Add("extra" + min);
    for (int x = 0; x < hdCDs.Length; x++)
    {
        hdCDs[0] -= min;
    }
}
票数 0
EN

Stack Overflow用户

发布于 2016-01-18 22:24:09

不应将双值与==进行比较。例如,有类似的代码

if (value==0) { /* do something here*/ }

如果value是双倍的话,那是个坏主意。

相反,您可以这样做:

if (Math.Abs(value) < EPSILON) { /* do something here*/ }

其中EPSILON是一些小的正数/常数,例如1e-5

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34865139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档