Re: [eigen] Matrix2i mean

[ Thread Index | Date Index | More Archives ]

You are dividing by 2, which is an int, so by integral promotion your result will also be an int which can comfortably hold the value you are computing. See

Best regards
Janos Meny

On Tue 5. Nov 2019 at 16:12, Peter <list@xxxxxxxxxxxxxxxxx> wrote:
Dear Christoph,

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
> overflowing.

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.


Mail converted by MHonArc 2.6.19+