Adi Drumea

Friday, March 16, 2007

Floating point and the CLR

I recently had to use a math library (dll) from a managed c++ library used from c# code. All seemed well, the dll funcs appeared to work, but I got wrong results from time to time. When using the same code and compiling for unmanaged, the errors would dissapear. I did a _controlfp to get the floating point status word and it was different when running in managed from unmanaged context. It turned out that the precision was only 24 bits in managed and 53 bits in unmanaged. The solution was to save the control word prior to using the dll functions and to restore it just before returning from the managed c++ library (so other code in c# does not get messed up). They say the function is not supported nor reliable so I guess there will be some more issues when we compile in release mode, but for now I'm content I have solved the problem.

0 Comments:

Post a Comment

<< Home