0

I try to reproduce some algorithms from external image processing library and I have found strange floating point number substraction precision error.

In original library (which is running on 32 bit debug configuration) there is a piece of code:

double d1 = *im1 - m_Centroids[j][0];

My code is at this moment the same (also running on 32 bit debug configuration):

double d1 = *im1 - m_Centroids[j][0];

At some point of program execution (when stopped for debugging) in original library those variables have values (in VisualStudio watch window):

Original code:

*im1                                 0.113626622          float
double(*im1)                         0.11362662166357040  double
m_Centroids[j][0]                    25.6416969           float
double(m_Centroids[j][0])            25.641696929931641   double
*im1 - m_Centroids[j][0]            -25.5280704           float
double(*im1 - m_Centroids[j][0])    -25.528070449829102   double
d1                                  -25.528070308268070   double

(See the difference between the last two)

My code:

*im1                                 0.113626622          float
double(*im1)                         0.11362662166357040  double
m_Centroids[j][0]                    25.6416969           float
double(m_Centroids[j][0])            25.641696929931641   double
*im1 - m_Centroids[j][0]            -25.5280704           float
double(*im1 - m_Centroids[j][0])    -25.528070449829102   double
d1                                  -25.528070449829102   double

Also I've run original code and my code simultaneously on seperate VisualStudio instances, on the same 64 bit computer.

That difference is causing my program having slightly different results at the end, than original.

What is the cause for such difference in substraction? (Considering, it is the same machine they're running on and same configuration)

  • Are you using the x87 fpu? If so, my condolences. – EOF Oct 17 '15 at 21:52
  • Here is a stock answer to a FAQ. http://stackoverflow.com/questions/588004/is-floating-point-math-broken – Weather Vane Oct 17 '15 at 21:58
  • 1
    @WeatherVane I do not think that that answer explains why the same piece of code can produce two different results. Perhaps you should read it again. – Pascal Cuoq Oct 17 '15 at 22:05
  • 6416969-1136266=5280703. It looks like the debugger is not showing (fully) correct numbers. – wimh Oct 17 '15 at 22:08
  • 1
    Your question is specific to Visual Studio. A modern version of GCC would never produce this result as long as one used `gcc -std=c99` or `gcc -msse2 -mfpmath=sse`. You should consider tagging it as such. – Pascal Cuoq Oct 17 '15 at 22:09

1 Answers1

0

I have found a solution to achieve same results in my code from: Difference in floating point arithmetics between x86 and x64

Solution was to force my code to emit SSE instructions even for 32 bit configuration, as it was done in original library.

Once I set Streaming SIMD Extensions to (/arch:SSE), result became identical.

Community
  • 1
  • 1