• <small id='hokJU'></small><noframes id='hokJU'>

  • <i id='hokJU'><tr id='hokJU'><dt id='hokJU'><q id='hokJU'><span id='hokJU'><b id='hokJU'><form id='hokJU'><ins id='hokJU'></ins><ul id='hokJU'></ul><sub id='hokJU'></sub></form><legend id='hokJU'></legend><bdo id='hokJU'><pre id='hokJU'><center id='hokJU'></center></pre></bdo></b><th id='hokJU'></th></span></q></dt></tr></i><div id='hokJU'><tfoot id='hokJU'></tfoot><dl id='hokJU'><fieldset id='hokJU'></fieldset></dl></div>
      <bdo id='hokJU'></bdo><ul id='hokJU'></ul>

  • <tfoot id='hokJU'></tfoot>

        <legend id='hokJU'><style id='hokJU'><dir id='hokJU'><q id='hokJU'></q></dir></style></legend>

        剪辑数字的最有效/优雅的方法?

        时间:2023-08-03
        <i id='uTmVn'><tr id='uTmVn'><dt id='uTmVn'><q id='uTmVn'><span id='uTmVn'><b id='uTmVn'><form id='uTmVn'><ins id='uTmVn'></ins><ul id='uTmVn'></ul><sub id='uTmVn'></sub></form><legend id='uTmVn'></legend><bdo id='uTmVn'><pre id='uTmVn'><center id='uTmVn'></center></pre></bdo></b><th id='uTmVn'></th></span></q></dt></tr></i><div id='uTmVn'><tfoot id='uTmVn'></tfoot><dl id='uTmVn'><fieldset id='uTmVn'></fieldset></dl></div>

          <tbody id='uTmVn'></tbody>

        <legend id='uTmVn'><style id='uTmVn'><dir id='uTmVn'><q id='uTmVn'></q></dir></style></legend>

            <small id='uTmVn'></small><noframes id='uTmVn'>

            <tfoot id='uTmVn'></tfoot>

              <bdo id='uTmVn'></bdo><ul id='uTmVn'></ul>
                • 本文介绍了剪辑数字的最有效/优雅的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  给定一个实数 (n),这个实数可以是的最大值(上),以及这个实数可以是的最小值(下),我们如何最有效地裁剪 n,使其保持在下限和上限之间?

                  Given a real (n), a maximum value this real can be (upper), and a minimum value this real can be (lower), how can we most efficiently clip n, such that it remains between lower and upper?

                  当然,使用一堆 if 语句可以做到这一点,但这很无聊!更紧凑、优雅/有趣的解决方案怎么样?

                  Of course, using a bunch of if statements can do this, but that's boring! What about more compact and elegant/fun solutions?

                  我自己的快速尝试(C/C++):

                  My own quick attempt (C/C++):

                  float clip( float n, float lower, float upper )
                  {
                      n = ( n > lower ) * n + !( n > lower ) * lower;
                      return ( n < upper ) * n + !( n < upper ) * upper;
                  }
                  

                  我确信还有其他更好的方法可以做到这一点,这就是我将它放在那里的原因..!

                  I'm sure there are other, better ways to do this, that's why I'm putting this out there..!

                  推荐答案

                  乏味、陈旧、可读和最短的又如何:

                  What about boring, old, readable, and shortest yet:

                  float clip(float n, float lower, float upper) {
                    return std::max(lower, std::min(n, upper));
                  }
                  

                  ?

                  这个表达式也可以像这样泛化":

                  This expression could also be 'genericized' like so:

                  template <typename T>
                  T clip(const T& n, const T& lower, const T& upper) {
                    return std::max(lower, std::min(n, upper));
                  }
                  

                  更新

                  比利·奥尼尔补充说:

                  请注意,在 Windows 上,您可能必须定义 NOMINMAX,因为它们定义了冲突的最小和最大宏

                  Note that on windows you might have to define NOMINMAX because they define min and max macros which conflict

                  这篇关于剪辑数字的最有效/优雅的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C++ 和 OpenGL 矩阵顺序之间的混淆(行优先 vs 列优先 下一篇:在 std::floor 之后转换为 int 是否保证正确的结果

                  相关文章

                  最新文章

                  <i id='ZWNMm'><tr id='ZWNMm'><dt id='ZWNMm'><q id='ZWNMm'><span id='ZWNMm'><b id='ZWNMm'><form id='ZWNMm'><ins id='ZWNMm'></ins><ul id='ZWNMm'></ul><sub id='ZWNMm'></sub></form><legend id='ZWNMm'></legend><bdo id='ZWNMm'><pre id='ZWNMm'><center id='ZWNMm'></center></pre></bdo></b><th id='ZWNMm'></th></span></q></dt></tr></i><div id='ZWNMm'><tfoot id='ZWNMm'></tfoot><dl id='ZWNMm'><fieldset id='ZWNMm'></fieldset></dl></div>

                  <small id='ZWNMm'></small><noframes id='ZWNMm'>

                  <legend id='ZWNMm'><style id='ZWNMm'><dir id='ZWNMm'><q id='ZWNMm'></q></dir></style></legend>
                  1. <tfoot id='ZWNMm'></tfoot>

                        <bdo id='ZWNMm'></bdo><ul id='ZWNMm'></ul>