ericniebler / range-v3

Range library for C++14/17/20, basis for C++20's std::ranges
Other
4.11k stars 440 forks source link

linear_distribute improvement #1679

Closed MaciejPatro closed 2 years ago

MaciejPatro commented 2 years ago

linear_distribute has an unexpected behavior for integral values when number of values to be calculated is greater than distance between from and to. My expectation was similar to numpy behavior where calculated values are "evenly" distributed with integral precision.

Expectation:

linear_distribute(0, 10, 20) == linear_distribute(0.0, 10.0, 20) | transform(convert_to<int>{})

Current implementation:

linear_distribute(0, 3, 7)     = [0, 0,   0, 0,   1, 2,   3]
linear_distribute(0.0, 3.0, 7) = [0, 0.5, 1, 1.5, 2, 2.5, 3]

Improved implementation:

linear_distribute(0, 3, 7)     = [0, 0,   1, 1,   2, 2,   3]
linear_distribute(0.0, 3.0, 7) = [0, 0.5, 1, 1.5, 2, 2.5, 3]
ericniebler commented 2 years ago

Sorry for the delay. Would you be able to fix the precision warning?

../include/range/v3/view/linear_distribute.hpp:78:51: error: implicit conversion from 'long' to 'double' may lose precision [-Werror,-Wimplicit-int-float-conversion]
                    from_ = T(to_ - (delta_ * (n_ - 1)));
                                            ~  ~~~^~~

And as a heads up, I have rebased this PR. You'll need to fetch it fresh.