Re: [eigen] Matrix2i mean |

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

Dear Petr,
Am 05.11.19 um 00:35 schrieb Petr Kubánek:

Hi,
calculating mean on int matrix fails to produce expected results - see
below. I noticed

[...]

Obviously all similar reduxes (sum, prod) fails as well. Is there a way
how to change type used as the internal storage and result for sum.
mean calculations?

In my opinion Eigen behaves perfectly well.
The problem you face is related to the fact, that the compiler can calculate known
expression with higher precision, see the example below, where I also introduced
the argc/argv only to avoid further compiler optimizations.
Output on my machine:
-4095 12288 -4095
12288.5 12288 -4095
Best regards,
Peter
#include <Eigen/Eigen>
#include <iostream>
int main( const int argc, const char *argv[] )
{
int16_t Two = (argc>1) ? atoi(argv[1]) : 2;
int16_t Max = (argc>99) ? atoi(argv[99]) : INT16_MAX;
Eigen::Matrix<int16_t, 2, 2> m;
m <<
INT16_MAX / 2, INT16_MAX,
2, 2;
int16_t mean = ( Max/2 + Max + Two + Two ) / int16_t(4);
int16_t mean2 = Max/2;
mean2 += Max;
mean2 += Two;
mean2 += Two;
mean2 /= int16_t(4);
std::cout << m.mean() << " " << mean << " " << mean2 << std::endl;
Eigen::Matrix<double, 2, 2> md = m.cast<double>();
std::cout << md.mean() << " " << mean << " " << mean2 << std::endl;
}