Re: [eigen] Matrix2i mean |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
- To: eigen@xxxxxxxxxxxxxxxxxxx
- Subject: Re: [eigen] Matrix2i mean
- From: Christoph Hertzberg <chtz@xxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 5 Nov 2019 16:36:21 +0100
- Dkim-signature: v=1; a=rsa-sha256; c=simple/simple; d=uni-bremen.de; s=2019; t=1572968182; i=@uni-bremen.de; bh=/M53sIeFRenmpsB+BXV88YxUV8u368xGRiqI8SCM+5k=; h=To:References:From:Date:In-Reply-To; b=h3zq2qi4/8aDYpekOpOjp59JY31b4d7TofNKAKQSlBaDLz5P7LH8GvYLAjDC/zjF0 kLf83E0iNFcVbpm5NfQcjhMDibsSGrjaL0FVtTy0UHcywfw6hv+1NY6I/rgZl1o72V S29ialApmcnUszSNC5xy9CmtX7pLTSHZEdPLheoAYvSMsdZIaEZU8GOPWIFiSOnY3S I/kkFDnnnwRC5Dc1MnbDXX0FxMlFpTvNvnJIPosqjuH8WgRlhqOWCBc9QRMpQNqv5M Gyg/sTiD3KMLH2HbjtrmCgID39I/2iVQdvWR9OABAYWjc/fk1xLTBAgC7pBXvARNk6 ov3ZBZvU1hDaQ==
On 05/11/2019 16.11, Peter wrote:
[...]
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.
Adding another template parameter to basic types is not an option. This
would break ABI and API compatibility (even if the parameter has a default).
You could create your own custom type `my_int16` for which `my_int16 *
my_int16` results in a `my_int32` (this needs to be told to Eigen,
similar to how real*complex products are handled).
[...]
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.
Yes, because `Max/2` gets implicitly converted to `int`. Actually, even
adding two `int16_t` get implicitly converted to `int` (search for
integer promotion rules -- I don't know them entirely either). And
dividing an `int` by an `int16_t` results in an `int` which is only
afterwards converted to `int16_t`.
In contrast, Eigen::DenseBase::sum() does something (more or less)
equivalent to:
T sum = T(0);
for(Index i=0; i< size(); ++i)
sum+=coeff(i);
return sum;
i.e., after each addition the result gets reduced to the scalar type of
the matrix, thus it will immediately overflow.
And mean() essentially just takes the return value of `sum()` and
divides it by the size.
Christoph
--
Dr.-Ing. Christoph Hertzberg
Besuchsadresse der Nebengeschäftsstelle:
DFKI GmbH
Robotics Innovation Center
Robert-Hooke-Straße 5
28359 Bremen, Germany
Postadresse der Hauptgeschäftsstelle Standort Bremen:
DFKI GmbH
Robotics Innovation Center
Robert-Hooke-Straße 1
28359 Bremen, Germany
Tel.: +49 421 178 45-4021
Zentrale: +49 421 178 45-0
E-Mail: christoph.hertzberg@xxxxxxx
Weitere Informationen: http://www.dfki.de/robotik
-------------------------------------------------------------
Deutsches Forschungszentrum für Künstliche Intelligenz GmbH
Trippstadter Strasse 122, D-67663 Kaiserslautern, Germany
Geschäftsführung:
Prof. Dr. Jana Koehler (Vorsitzende)
Dr. Walter Olthoff
Vorsitzender des Aufsichtsrats:
Prof. Dr. h.c. Hans A. Aukes
Amtsgericht Kaiserslautern, HRB 2313
-------------------------------------------------------------