i am trying to port a particular piece of code from solaris to Linux. During the process i found that the precision on linux is different and it is in extended precision and we need to set it to double precision explicitly. To achieve this found fpu_control.h library, functions FPU_GETCW and FPU_SETCW functions. But even after that the precision is not being set properly. the code snippet
long double power = 1.0;
#ifdef __linux
fpu_control_t mask;
_FPU_GETCW(mask);
mask &= ~(_FPU_EXTENDED & _FPU_SINGLE);
mask |= _FPU_DOUBLE;
_FPU_SETCW(mask);
power *= 0.1;
#endif
when i print power the value is power = 0.1000000000000000055511151231257827
however I was expecting power to have an value 0.1 Also i have use -DDouble while compiling. Can someone point me whats going wrong.
You specifically request a
long double, while you supposedly want plaindouble. If your hardware is an Intel x86/x86-64 CPU, calculations going through the FPU are performed on 80-bits precision.Otherwise: try using something like the gcc flag:
-mfpmath=sse, which will stop using the FPU and your operations will be performed with 64-bit (aka double) precision.Note:
It is very possible that even in Solaris you were getting an inexact representation for
0.1(there isn't an exact one), but the way the value was output hid this inexactness by printing up to a specified number of decimal digits.