是否有一个“规范”? 组合 min() 和 max() 的函数的名称?

发布于 2024-07-13 15:12:11 字数 590 浏览 4 评论 0原文

我发现我经常最终编写一个我总是称之为“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 技术交流群。

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

发布评论

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

评论(8

放我走吧 2024-07-20 15:12:11

clamp是个好名字。

让我们把它定为标准。

clamp is a good name.

Let us make it the standard.

偏闹i 2024-07-20 15:12:11

在某些语言中,您有函数 limit

num = limit(val, min, max)

In some languages you have the function limit

num = limit(val, min, max)

没有心的人 2024-07-20 15:12:11
clip(val, lo, hi)
clip(val, lo, hi)
一花一树开 2024-07-20 15:12:11

我们在这里使用pin。 实际上,我们使用 pin 来表示简单的范围,使用 clamp 来表示其他内容。

We use pin here. Actually, we use pin for simple ranges and clamp for other stuff.

一身仙ぐ女味 2024-07-20 15:12:11

我只想选择一个函数名称“rangeCheck”

I'd just go for a function name "rangeCheck"

一瞬间的火花 2024-07-20 15:12:11

那么绑定呢?

bound(min, val, max)

或者约束

constrain(val, min, max)

What about bound?

bound(min, val, max)

Or constrain?

constrain(val, min, max)
给我一枪 2024-07-20 15:12:11

您对 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.

七度光 2024-07-20 15:12:11

中位数

因为它概括为更多值。

median

Because it generalizes to more values.

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