简体   繁体   English

我如何检查一个双精度数组是否包含 c# 中的某个双精度

[英]how do i check to see if a double array contains a certain double in c#

I'm using this code right now我现在正在使用此代码

        double[] LocationsDown = { 40, 85, 130, 175, 220, 265, 310, 355 };
        double[] LocationsUp = { 50, 95, 140, 185, 230, 275, 320, 5 };
        double curretangle = Math.Round(targetAngle);

        if (LocationsDown == curretangle) // <- Compile Time Error here
        {
            //thing
        }

but it says that但它说

" Operator '==' cannot be applied to operands of type 'double[]' and 'double' " " 运算符 '==' 不能应用于 'double[]' 和 'double' 类型的操作数"

I do not understand the correct way to check if the array contains said double I feel like it's going to be an easy fix I just can't put my finger on it.我不明白检查数组是否包含所说的 double 的正确方法我觉得这将是一个简单的修复我只是不能把我的手指放在它上面。

In general case we have to compare double values with some tolerance :一般情况下,我们必须比较具有一定tolerancedouble精度值:

 if (Math.Abs(someValue - valueToCheck) <= tolerance) {...}

When working with collections we can use Linq to query them:使用collections时,我们可以使用Linq来查询它们:

 using System.Linq;

 ...

 double tolerance = 1e-6; 

 bool contains = LocationsDown.Any(item => Math.Abs(item - curretangle) <= tolerance);

Use .Contains :使用.Contains

if (LocationsDown.Contains(curretangle))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM