Convert values to values inside a range in c++ , optimized using boost or std

為{幸葍}努か 提交于 2019-12-05 17:36:32
Nikos Athanasiou

I don't think using elaborate constructs would buy you much here. Maybe making your code a bit cleaner ?

std::for_each(std::begin(vElem), std::end(vElem), [](float &val) {
    val = clamp(val, minVal, maxVal);
});

this is what a typicall clamp function returns

  for(int i=0; i<MAX; i++){
      float c = vElem[i];

      //Could convert c to an integer here, so the below uses integer operations
      //Additional cost is two multiplications (scaling up and scaling back down).

      float n = c + ((c < min) * (min - c)) + ((c > max) * (max - c));
      std::cout << n << std::endl;
  }
  • Two multiplications
  • Four additions
  • Two comparisons

I think this is faster than any of the clamp() implementations referred to in the other answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!