Re: [eigen] Matrix2i mean |

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]

*To*: eigen@xxxxxxxxxxxxxxxxxxx*Subject*: Re: [eigen] Matrix2i mean*From*: Janos Meny <janos.meny@xxxxxxxxxxxxxx>*Date*: Tue, 5 Nov 2019 16:25:00 +0100*Dkim-signature*: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20161025; h=mime-version:references:in-reply-to:reply-to:from:date:message-id :subject:to; bh=hi3e2Jruz99FgLQyEE0U3qkkWqUYrPX1OHeyOyGYnaM=; b=rCkmgqK8GrZtGK2Kb/danmgh14A/B2xkD8ng4M7/epvzZgKU9meYyLD55Ax1JxUXpJ H1MIQOl/ugtLlPFkDP/O2KA5Dxa1q4UjZ/8MprSXi3JMOSqVkgVzTYDoad/QMDh0ugzo bbe8FIIQERHHSDgVEHb02JjgM8KDgLqkO5yXSV3UBF/JLZFOzS41PCJw6PK2PuFpMFVY Nm7jvifzmFGMO2PQBbYDIan+N0nJHWGFIAEoFivzpmi+w+SMaiGZ7qF3rpQ3MplIxlHD 996iLHdSDUNzc318wBGCpmnFpBuM5TldH2p3sFXu/DEtt5d7e97lCyrA6Pg6vB2LcYiC PeeA==

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.

Even

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.

Peter

**Follow-Ups**:**Re: [eigen] Matrix2i mean***From:*Peter

**References**:**[eigen] Matrix2i mean***From:*Petr Kubánek

**Re: [eigen] Matrix2i mean***From:*Peter

**Re: [eigen] Matrix2i mean***From:*Christoph Hertzberg

**Re: [eigen] Matrix2i mean***From:*Peter

**Messages sorted by:**[ date | thread ]- Prev by Date:
**Re: [eigen] Matrix2i mean** - Next by Date:
**Re: [eigen] Matrix2i mean** - Previous by thread:
**Re: [eigen] Matrix2i mean** - Next by thread:
**Re: [eigen] Matrix2i mean**

Mail converted by MHonArc 2.6.19+ | http://listengine.tuxfamily.org/ |