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?
我发现我经常最终编写一个我总称为“clamp()”的函数,这是min()和max()的组合。这个函数有一个标准的“规范”名称吗?
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:
或者只是使用内置的min()和max()函数:
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.
存在变化:您还可以检查无效输入,其中lower> upper,并且抛出异常或反转输入。或者您可以忽略输入的顺序并将其称为三个中值函数,但这可能会令人困惑。
8 个解决方案
#1
clamp is a good name.
钳是个好名字。
Let us make it the standard.
让我们把它作为标准。
#2
In some languages you have the function limit
在某些语言中,您具有功能限制
num = limit(val, min, max)
num = limit(val,min,max)
#3
clip(val, lo, hi)
#4
We use pin
here. Actually, we use pin
for simple ranges and clamp
for other stuff.
我们在这里使用pin。实际上,我们使用pin作为简单范围并使用其他东西钳位。
#5
I'd just go for a function name "rangeCheck"
我只想找一个函数名“rangeCheck”
#6
median
Because it generalizes to more values.
因为它推广到更多的价值观。
#8
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.
您如何看待InRangeClosestTo(Number,RangeLowerBound,RangeUpperBound)或ClosestInRange(Number,LowerBoundOfRange,UpperBoundOfRange)等内容?他们的意思是“让我得到最接近数字的范围的元素”,我希望这很明显。
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,
这个概念比Clamp更准确,是因为它有两个但不是更多,或者一个限制或束缚,如果数量不在范围内,可能不想返回任何东西,
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.
你可能只怀疑范围是否具有包容性,但我认为大多数人会正确地认为它具有包容性。或者你可以使用InInclRangeClosestTo和InExclRangeClosestTo,虽然我没有看到很多用于独占范围。
Of course you should have an auto-completing IDE if you wanted to use them.
当然,如果你想使用它们,你应该有一个自动完成的IDE。
#1
clamp is a good name.
钳是个好名字。
Let us make it the standard.
让我们把它作为标准。
#2
In some languages you have the function limit
在某些语言中,您具有功能限制
num = limit(val, min, max)
num = limit(val,min,max)
#3
clip(val, lo, hi)
#4
We use pin
here. Actually, we use pin
for simple ranges and clamp
for other stuff.
我们在这里使用pin。实际上,我们使用pin作为简单范围并使用其他东西钳位。
#5
I'd just go for a function name "rangeCheck"
我只想找一个函数名“rangeCheck”
#6
median
Because it generalizes to more values.
因为它推广到更多的价值观。
#7
#8
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.
您如何看待InRangeClosestTo(Number,RangeLowerBound,RangeUpperBound)或ClosestInRange(Number,LowerBoundOfRange,UpperBoundOfRange)等内容?他们的意思是“让我得到最接近数字的范围的元素”,我希望这很明显。
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,
这个概念比Clamp更准确,是因为它有两个但不是更多,或者一个限制或束缚,如果数量不在范围内,可能不想返回任何东西,
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.
你可能只怀疑范围是否具有包容性,但我认为大多数人会正确地认为它具有包容性。或者你可以使用InInclRangeClosestTo和InExclRangeClosestTo,虽然我没有看到很多用于独占范围。
Of course you should have an auto-completing IDE if you wanted to use them.
当然,如果你想使用它们,你应该有一个自动完成的IDE。