防止错误结转的最佳方法?

发布于 2024-12-19 09:47:12 字数 849 浏览 4 评论 0原文

我有一个通过以 250 kHz 采样电压创建的大型数据数组。我想将数据以及相关时间打印到文件中。我的第一种方法是这样做(在 C# 中):

decimal dt = 1m / sampleRate;
decimal t = 0;

for(int i = 0; i < dataArray.Length; i++)
{
    Writer.WriteLine(t + "\t" + dataArray[i]);

    t += dt;  //Would using t = i * dt; be any different?
}

显然,由于浮点数学这种方法的固有性质,它开始累积错误,该错误在几秒钟的数据后变得很明显。

我解决这个问题的方法是这样的:

decimal dt = (decimal)(1 / sampleRate);
decimal t = 0;
int seconds = 0;

for(int i = 0; i < dataArray.Length; i++)
{
    if(i % sampleRate == 0)
        {
            t = seconds;
            seconds++;
        }
    Writer.WriteLine(t + "\t" + dataArray[i]);

    t += dt;  
}

它使所有内容保持足够好的同步,但看起来不是特别优雅。有更好的方法来解决这个问题吗?最后,在顶级方法中,使用 t += dt 与带小数的 t = i * dt 有什么不同吗?如果有双呢?

编辑:正如已经指出的,小数不是浮点数。我应该在这里使用十进制还是双精度?

I have a large data array created by sampling a voltage at 250 kHz. I would like to print the data along with the associated time to a file. My first approach would be to do it like this (in C#):

decimal dt = 1m / sampleRate;
decimal t = 0;

for(int i = 0; i < dataArray.Length; i++)
{
    Writer.WriteLine(t + "\t" + dataArray[i]);

    t += dt;  //Would using t = i * dt; be any different?
}

Obviously due to the inherent nature of floating point math this approach, it starts to accumulate an error that becomes significant after several seconds of data.

My approach to solve this problem would be something like this:

decimal dt = (decimal)(1 / sampleRate);
decimal t = 0;
int seconds = 0;

for(int i = 0; i < dataArray.Length; i++)
{
    if(i % sampleRate == 0)
        {
            t = seconds;
            seconds++;
        }
    Writer.WriteLine(t + "\t" + dataArray[i]);

    t += dt;  
}

Which keeps everything synced up well enough but doesn't seem particularly elegant. Is there a better way to approach this? And finally, in the top approach, is using t += dt any different from t = i * dt with a decimal? What about with a double?

EDIT: As has been pointed out, decimal is not floating point. Should I be using decimal or double here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那伤。 2024-12-26 09:47:12

由于您使用的是十进制,因此您没有进行浮点数学运算。 decimal 基本上是一个定点数。

Since you're using decimal, you are not doing floating-point math. decimal is basically a fixed-point number.

乄_柒ぐ汐 2024-12-26 09:47:12

您使用的是decimal,它是定点。您不应该出现任何错误(当然,除非您的值超出了十进制支持的范围)。

不过,您的第一个陈述是不正确的。您正在进行整数数学运算(将产生整数结果),然后转换为小数。您需要做一些不同的事情来强制正确计算:

decimal dt = 1m / sampleRate;

You're using decimal, which is fixed point. You shouldn't have any error (unless, of course, your values are out of the supported range of decimal).

Your first statement, though, is incorrect. You are doing integer math (with will result in an integer result) and then casting to a decimal. You need to do things a bit different to force the calculation properly:

decimal dt = 1m / sampleRate;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文