是否有一个“规范”? 组合 min() 和 max() 的函数的名称?
我发现我经常最终编写一个我总是称之为“clamp()
”的函数,它是 min()
和 max( )
。 该函数有标准的“规范”名称吗?
它总是看起来像这样:
function clamp($val, $lower, $upper)
{
if ($val < $lower)
return $lower;
else if ($val > $upper)
return $upper;
else
return $val;
}
或者简单地使用内置的 min()
和 max()
函数:
function clamp($val, $lower, $upper)
{
return max($lower, min($upper, $val));
}
存在变化:您还可以检查无效输入,其中 下> upper
,并抛出异常或反转输入。 或者您可以忽略输入的顺序并将其称为三中位数函数,但这可能会令人困惑。
I find that I frequently end up writing a function that I always call "clamp()
", that is kind of a combination of min()
and max()
. Is there a standard, "canonical" name for this function?
It always looks something like this:
function clamp($val, $lower, $upper)
{
if ($val < $lower)
return $lower;
else if ($val > $upper)
return $upper;
else
return $val;
}
Or simply using built-in min()
and max()
functions:
function clamp($val, $lower, $upper)
{
return max($lower, min($upper, $val));
}
Variations exist: You can also check for invalid input, where lower > upper
, and either throw an exception or reverse the inputs. Or you can ignore order of the inputs and call it a median-of-three function, but that can be confusing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
clamp是个好名字。
让我们把它定为标准。
clamp is a good name.
Let us make it the standard.
在某些语言中,您有函数 limit
num = limit(val, min, max)
In some languages you have the function limit
num = limit(val, min, max)
我们在这里使用
pin
。 实际上,我们使用pin
来表示简单的范围,使用clamp
来表示其他内容。We use
pin
here. Actually, we usepin
for simple ranges andclamp
for other stuff.我只想选择一个函数名称“rangeCheck”
I'd just go for a function name "rangeCheck"
那么绑定呢?
或者约束?
What about bound?
Or constrain?
您对 InRangeClosestTo(Number, RangeLowerBound, RangeUpperBound) 或 ClosestInRange(Number, LowerBoundOfRange, UpperBoundOfRange) 有何看法? 他们的意思是“给我最接近数字的范围的元素”,我希望这是显而易见的。
这个概念比 Clamp 更精确,它有两侧但不多,或者是 Limit 或 Bound,如果数字不在范围内,则可能不想返回任何内容,
对我来说,它们比我看到的其余部分更清晰; 虽然理解它们可能需要几秒钟的时间,但你只需要推理出名字,最多简单地看一下评论即可确认; 当你看到它有多么精确时,真是太好了(它很精确,对吧?)。
您可能只会怀疑该范围是否包含在内,但我认为大多数人会正确地认为它是包含在内的。 或者,您可以使用 InInclRangeClosestTo 和 InExclRangeClosestTo,尽管我没有看到独占范围有很多用途。
当然,如果您想使用它们,您应该有一个自动完成的 IDE。
What do you think of things like InRangeClosestTo(Number, RangeLowerBound, RangeUpperBound), or ClosestInRange(Number, LowerBoundOfRange, UpperBoundOfRange)? They mean 'Get me the element of the range closest to the number', as I hope is obvious.
The concept is more precise than a Clamp that yeah has two sides but not much more, or a Limit or Bound that might not want to return anything if the number is not within the range,
To me they are clearer then the rest I saw; although it can take a couple of seconds to understand them, you only need to reason about the name, and at most have a brief look at the comment for confirmation; and it's nice when you see how precise it is (it is precise, right?).
You might only have doubts on whether the range is inclusive or not, but I think most people would correctly assume it's inclusive. Alternatively you might use InInclRangeClosestTo and InExclRangeClosestTo, althought I don't see a lot of uses for exclusive ranges.
Of course you should have an auto-completing IDE if you wanted to use them.
中位数
因为它概括为更多值。
median
Because it generalizes to more values.