使用双精度数组和列表 C# 查找偏移量

本文关键字:查找 偏移量 列表 双精度 数组 | 更新日期: 2023-09-27 18:31:08

我试图找到双精度数组的偏移量(数组元素之间的差异)。我的偏移量计算公式是偏移量=((双精度[x+1]-双精度[x])*33)。我遇到的问题是我似乎无法显示偏移量X。我在这里做错了什么?有没有更简单的方法可以做到这一点?

这是我的代码:

//double[] test is an infinite array containing double values that are passed from the Client to Server via UDP
//example : double[] test={1.11,2.344,3.45,4.54,....,......,....}
//Partial codes on the Server side to calculate and display offset
List<double> array = new List<double>();
//Im trying to store just the elements of postion 0,3,6..from the double []test into list array
//ignore array1[] and array2[]
for (int j = 0; j < test.Length;) 
{ 
    array.Add(test[j]); j++;
    array1.Add(test[j]); j++; 
    array2.Add(test[j]); j++;
}
double[] gg = array.ToArray();
//Finding the offset
for (int i = 0, j = 1; i < gg.Length - 1; i++, j++)
{
    offsetX.Add(gg[j] - gg[i]);
    offsetX[i] = Math.Round(offsetX[i], 1);
    offsetX[i] = (offsetX[i] * 33);
}
Console.Write( "OffsetX:" + string.Join(",", offsetX) +"/n");

使用双精度数组和列表 C# 查找偏移量

下面的方法只是遍历在列表的初始迭代期间添加偏移量的数组

List<double> array = new List<double>() { 1.11,2.344,3.45,4.54 };
var offsets = new List<double>();
for (int i = 1; i < array.Count; i++)
    offsets.Add(array[i] - array[i-1]);

这会导致以下偏移:

Offsets: 1.234,1.106,1.09