我如何检查双精度数组是否包含 C# 中的某个双精度数
how do i check to see if a double array contains a certain double in c#
我现在正在使用这个代码
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
}
但是上面写着
" Operator '==' cannot be applied to operands of type 'double[]' and
'double' "
我不明白检查数组是否包含所述 double 的正确方法我觉得这将是一个简单的修复方法,我只是无法确定。
使用.Contains
:
if (LocationsDown.Contains(curretangle))
在一般情况下我们必须将double
值与一些tolerance
:
进行比较
if (Math.Abs(someValue - valueToCheck) <= tolerance) {...}
使用集合时,我们可以使用Linq查询它们:
using System.Linq;
...
double tolerance = 1e-6;
bool contains = LocationsDown.Any(item => Math.Abs(item - curretangle) <= tolerance);
我现在正在使用这个代码
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
}
但是上面写着
" Operator '==' cannot be applied to operands of type 'double[]' and 'double' "
我不明白检查数组是否包含所述 double 的正确方法我觉得这将是一个简单的修复方法,我只是无法确定。
使用.Contains
:
if (LocationsDown.Contains(curretangle))
在一般情况下我们必须将double
值与一些tolerance
:
if (Math.Abs(someValue - valueToCheck) <= tolerance) {...}
使用集合时,我们可以使用Linq查询它们:
using System.Linq;
...
double tolerance = 1e-6;
bool contains = LocationsDown.Any(item => Math.Abs(item - curretangle) <= tolerance);