3 ms·
It makes the case that we need Math.mean or Math.avg function that we can use in these cases rather than than reinventing it. Basically we should favor using b
by bhouston 2y ago
It makes the case that we need Math.mean or Math.avg function that we can use in these cases rather than than reinventing it.
Basically we should favor using built in functions for as much as possible because those should be reliable and tested more than ad hoc code we write. And compilers should optimize those built in functions well so there is no extra cost in using them.
- ape4 2y agoI was thinking the same. And the code would be clearer too.
- sltkr 2y agoC++ added std::midpoint() to the standard library: https://en.cppreference.com/w/cpp/numeric/midpoint https://en.cppreference.com/w/cpp/numeric/midpoint Another fun case, besides integer overflow, is negative values: in Java and C/C++ (i + j)/2 will round towards j, but i + (j - i)/2 will round towards i instead. Sometimes the difference matters.