繁体   English   中英

一系列浮点数的约束行为?

[英]Constraint behavior for a range of floating point numbers?

我正在研究这个问题,以获得关于为给定范围实现约束/包装的想法。

从那以后我可以确定以下内容

[n1,n2)

float Wrap(float x, float lo, float hi)
{
    return x % Math.Abs(lo - hi);
}

这只适用于正数,因此建议这样做

float Constrain(float x, float lo, float hi)
{
    float t = (x - lo) % (hi - lo);

    return t < 0 ? t + hi : t + lo;
}

我仍然不确定如何从上面的代码中获得以下范围限制并希望得到一些帮助?

[n1,n2]

(n1,n2]

(n1,n2]

float constrainExclusiveInclusive(float x, float lo, float hi){
    float result = Constrain(x, lo, hi);
    if (result == lo){result = hi;}
    return result;
}

[n1,n2]

float constrainInclusiveInclusive(float x, float lo, float hi){
    float result = Constrain(x, lo, hi);
    if (result == lo){
        //somehow decide whether you want x to wrap to lo or hi
        if (moonIsInTheSeventhHouse()){
            result = lo;
        }
        else{
            result = hi;
        }
    }
    return result;
}

(n1,n2)

float constrainExclusiveExclusive(float x, float lo, float hi){
    float result = Constrain(x, lo, hi);
    if (result == lo){
        throw new ArgumentException("No answer exists for the given inputs");
    }
    return result;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM