Most efficient/elegant way to clip a number?

前端 未结 11 725
予麋鹿
予麋鹿 2020-11-30 03:09

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 lo

11条回答
  •  遥遥无期
    2020-11-30 03:35

    If you wish to use xtensor, it would support multi-dimensional arrays and the solution would be very elegant.

    #include 
    #include "xtensor/xarray.hpp"
    #include "xtensor/xio.hpp"
    #include "xtensor/xview.hpp"
    #include "xtensor/xrandom.hpp"
    xt::xarray ar({2.1, 2.9, -2.1, -2.9});
    std::cout<(xt::trunc(ar))<

    //Answer is { 2, 2, -2, -2 }

提交回复
热议问题