我正在尝试检查双倍(时间)> 1可以被另一个双(samplePeriod)整除,其是> 1。 0和< 1.以下代码适用于samplePeriods> 1,但不是< 1.任何想法?感谢。
/// <summary>
/// Check if a sample is within 1/10 of a samplePeriod from being on a samplePeriod
/// </summary>
/// <param name="time"> The time of the sample </param>
/// <param name="samplePeriod"> The period between samples </param>
/// <returns> True if the sample is on a sample period, false if not </returns>
public static bool SampleIsOnSamplePeriod(double time, double samplePeriod)
{
if (Math.Abs(time%samplePeriod - 0d) > (samplePeriod/10))
{
return false;
}
return true;
}
答案 0 :(得分:1)
阅读您的评论后“允许的错误是样本期间的十分之一,因此时间可以在+/- 0.01之内,视为真实。超出+/- 0.01的值将是错误的。“,我真的认为你想要:
if ((time%samplePeriod > samplePeriod*0.9d) || (time%samplePeriod < samplePeriod*0.1d)
答案 1 :(得分:0)
public static bool SampleIsOnSamplePeriod(double time, double samplePeriod)
{
double mod1 = (time / samplePeriod) % 1;
return (mod1 < .1) || (mod1 > .9);
}
基本上你将time
除以samplePeriod
,然后按1进行修改。如果它“足够接近”,则此值应介于0和0.1之间或介于0.9和1之间。 / p>
支票的左侧是检查[0,0.1]。右边是[0.9,1]。
答案 2 :(得分:0)
public static bool SampleIsOnSamplePeriod(double time, double samplePeriod)
{
var d = time / samplePediod;
return Math.Abs(d - Math.Round(d)) <= 0.1;
}