-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New method range_proxy::size() to know the size of the range. #3
Conversation
Hi, thanks, this is a good idea. However, the proposed change only adds the |
I can add the member return static_cast<size_t>(std::floor((*end_ - *begin_) / step_)); |
Unfortunately this yields wrong results (the correct result in all cases is 4):
|
The code below is an update for the method // null step
if (step_ == T(0)) {
return static_cast<size_t>(-1);
}
// increasing and null range
if (*end_ >= *begin_) {
if (step_ < T(0)) {
return 0;
}
return static_cast<size_t>(std::ceil(double(*end_ - *begin_) / double(step_));
}
// decreasing range
if (*end_ < *begin_) {
if (step_ > T(0)) {
return 0;
}
return static_cast<size_t>(std::ceil(double(*begin_ - *end_) / double(step_));
} I did not test this code in C++, but from tests in another language it will give your the result 4 in your examples. The code Finally, the formula used in the if (*end_ < *begin_) {
return 0;
}
return static_cast<size_t>(*end_ - *begin_); |
Right, 8 cannot be reached; so the output is 1, 3, 5, 7, which is four different numbers. And analogously for the other ranges. |
You are right... I updated the code in my previous comment to compute the good size. |
Looks good. Except the Anyway, if you write a commit I’ll merge it! |
Implementation updated from the PR discussion. Tests were added to verify the results
@klmr I finally implemented the |
* Simplify `size` implementation Author: Konrad Rudolph <konrad.rudolph@gmail.com> * Use name “iterator” consistently Author: Konrad Rudolph <konrad.rudolph@gmail.com> * Method range_proxy::step_range_proxy::size() * Implementation updated from the PR discussion. * Tests were added to verify the results Author: Arnaud Barré <alzathar@Moveck-C05.local> * New method range_proxy::size() to know the size of the range. Author: Arnaud Barré <arnaud.barre@gmail.com>
Wow, what a coincidence: almost exactly a year after your last change. Apologies for the delay! |
No description provided.