简体   繁体   中英

does getting -nan values when multiplying two large matrices in eigen normal? and if it is how can we avoid it?

I have have two Eigen matrices that I'm multiplying, they are of type Eigen::Matrix<long double, Eigen::Dynamic, Eigen::Dynamic> and whenever I display the product I get -nan values...

I just want to ask if it that is normal to get nan values if you have some certain values in the matrix like numbers ranging from (+,-)1.2e-100 or (+,-)1.2e100 ? do I need to use a multi-precision type instead of using long double in the Eigen::Matrix to avoid it?

EDIT:

I tried to use double instead of long double now and I'm not getting -nan values anymore in the product matrix. But I still don't know yet why is that the case when using long double

A long double is compiler and architecture dependent on how much precision you will get. Double precision is between 15 and 18 digits, with most doubles having >=16 sig figs. Long double has a minimum precision of 15 to 33 sig figs depending on how many bytes the compiler allocates it occupies.

Now, if you want to test your compiler to see the min/max value your long double can be, add

long double min = std::numeric_limits<long double>::min()
long double max = std::numeric_limits<long double>::max()
std::cout<<"Min/Max: "<<min<<","<<max<<std::endl;

Now on to the matrix part of your question. If you are multiplying 1.2e100 by -1.2e100, using a modern compiler (gunna be liberal and say newer than gcc4.8) and 64bit architecture, I would expect a long double to be able to handle this without a problem (I would expect the above code to provide a min/max of ~ +/-1e4900). Its possible that if you are printing out a long double that there is an issue with your compiler. Now if you are using gdb to print the values, I wouldn't be shocked to see nans or other oddities ( see GDB examine long double array for example)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM