c# - 将 Easy Language 代码转换为 C# 代码会导致一些问题
问题描述
我正在尝试将用简单语言代码编写的数学函数转换为 C#,但我的代码中有一个非常简单但非常令人沮丧的错误,我运行计算时使用的值越多,值就会越低。我附上了代码的截图和我得到的回报。我确定这只是我的代码中的一个错字,但我找不到它。如果您需要查看其他内容,请告诉我
这是我的代码:
public static (List<double>, List<double>) CalculateEhlersRoofingFilter(List<double> valuesList, int days)
{
List<double> highPassList = new List<double>();
List<double> roofingFilterList = new List<double>();
int lowerPeriod = days;
int upperPeriod = Utils.MinOrMax((int)Math.Ceiling(lowerPeriod / 0.2083));
double alphaArg = Utils.ToRadians((0.707 * 360) / upperPeriod);
double alphaCos = Math.Cos(alphaArg);
double alpha1;
if (alphaCos != 0)
{
alpha1 = (alphaCos + Math.Sin(alphaArg) - 1) / alphaCos;
}
else
{
alpha1 = 0;
}
double a1 = Math.Exp((-1.414 * 3.14159) / lowerPeriod);
double b1 = 2 * a1 * Math.Cos(Utils.ToRadians((1.414 * 180) / lowerPeriod));
double c2 = b1;
double c3 = (-1 * a1) * a1;
double c1 = 1 - c2 - c3;
for (int i = 0; i < valuesList.Count; i++)
{
double currentValue = valuesList.ElementAt(i);
double prevValue1, prevValue2, prevFilter1, prevFilter2, prevHp1, prevHp2;
if (i >= 1)
{
prevValue1 = valuesList.ElementAt(i - 1);
prevFilter1 = roofingFilterList.ElementAt(i - 1);
prevHp1 = highPassList.ElementAt(i - 1);
}
else
{
prevValue1 = currentValue;
prevFilter1 = currentValue;
prevHp1 = currentValue;
}
if (i >= 2)
{
prevFilter2 = roofingFilterList.ElementAt(i - 2);
prevValue2 = valuesList.ElementAt(i - 2);
prevHp2 = highPassList.ElementAt(i - 2);
}
else
{
prevFilter2 = currentValue;
prevValue2 = currentValue;
prevHp2 = currentValue;
}
double highPass = (Utils.Power((1 - alpha1) / 2, 2) * (currentValue - (2 * prevValue1) + prevValue2)) + (2 * (1 - alpha1) * prevHp1) -
(Utils.Power(1 - alpha1, 2) * prevHp2);
highPassList.Add(highPass);
double roofingFilter = (c1 * ((highPass + prevHp1) / 2)) + (c2 * prevFilter1) + (c3 * prevFilter2);
roofingFilterList.Add(roofingFilter);
}
return (highPassList, roofingFilterList);
}
解决方案
如前所述,在不确切知道您要查找的内容的情况下找到问题可能具有挑战性。但是,我建议将“lowerPeriod”和“upperPeriod”更改为加倍以使计算保持一致(让值从右侧的 int 分配),并在每个常量之后放置一个“D”以强制它们被视为double 而不是潜在的 int 或 float 等...使用“2D”是“2 as a double”而不是“2”,后者是“int”,您必须担心不同数字类型的交互并希望规则能如您所愿。
推荐阅读
- google-apps-script - 在循环中从谷歌表导出到文本文件
- javascript - 无法访问函数外的变量
- typescript - 如何创建只允许来自接口的属性的类型?
- nestjs - 初始化之前无法访问“用户”-NestJs 和 typeORM
- javascript - 没有这样的文件或目录“credentials.json”
- rest - 在 URI 模板中带有 # 片段扩展的 RESTful API (RFC 6570)
- go - 如何在不更新已经存在的关联行的情况下附加到 Gorm 中的多对多关系?
- sql - HiveQL:从字符串列中删除前导空格
- c++ - 在 ::operator = 中从我自己调用类构造函数
- azure - 解释 - 通过 Microsoft Azure DevOps Pipelines 部署生产 NextJS 应用程序的问题