|Re: [eigen] Matrix2i mean|
[ Thread Index |
| More lists.tuxfamily.org/eigen Archives
Am 05.11.19 um 14:31 schrieb Christoph Hertzberg:
We _could_ provide something like `stable_mean()` which with some sophisticated algorithm works around overflows. If larger types are available, this could just cast to that type before summing.
actually, this would also be interesting for the scalar products in general, namely a different
type for accumulating the sums within a scalar product, e.g. as yet another template parameter for the matrices.
The reason that this line worked as you expected:
double mean = (INT16_MAX / 2.0 + INT16_MAX + 2 + 2) / 4.0;
is that due to the `2.0` in `INT16_MAX / 2.0` the expression gets converted to `double` and stays to be `double` even after adding more integers or `uint16_t`. And `double` is perfectly capable of adding your values without
I think it's more subtle than that.
int16_t Two = 2;
int16_t Max = INT16_MAX;
int16_t mean = ( Max/2 + Max + Two + Two ) / int16_t(4);
doesn't produce an overflow.