PRB: System.Arithmetic Exception Error When You Change the Floating-Point Control Register in a Managed Application

Article translations Article translations
Article ID: 326219 - View products that this article applies to.
This article was previously published under Q326219
Expand all | Collapse all

On This Page


When you change the floating-point control register in managed applications, you may receive exception error messages such as the following in the Microsoft .NET Framework common language runtime:
An unhandled exception of type 'System.ArithmeticException' occurred in
Additional information: Overflow or underflow in the arithmetic operation.


The common language runtime and the .NET Framework assume that the floating-point control register is set to the default state.


If a managed application calls into unmanaged code that must modify the floating-point control register, the unmanaged code must return the floating-point register to its default state before it returns program control to the managed application.

For example, if you use an Intel system, you can use the following statement before you return program control to the managed application to reset the floating-point control register:
	_controlfp(_CW_DEFAULT, 0xfffff);
      //See the "Steps to Reproduce the Behavior" section of this article.


This behavior is by design.


Steps to Reproduce the Behavior

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, click New, and then click Project.
  3. In the New Project dialog box, click either Visual Basic Projects or Visual C# Projects under Project Types, and then click Windows Application under Templates. By default, Form1 is created.
  4. Open the .cs or .vb file to see the code for the Form1.Main function.
  5. Change the Form1.Main function from
       static void Main() 
            Application.Run(new Form1());
      [DllImport("msvcr70.dll", CallingConvention = CallingConvention.Cdecl)]
          public static extern int _controlfp(int n, int mask);
       static void Main() 
            const int _EM_OVERFLOW = 0x00000004;
            const int _MCW_EM = 0x0008001F;
            _controlfp(_EM_OVERFLOW, _MCW_EM);
            Application.Run(new Form1());
  6. Build and run the project.


Article ID: 326219 - Last Review: May 20, 2003 - Revision: 3.0
  • Microsoft Common Language Runtime (included with the .NET Framework) 1.0
kbprb kbnofix KB326219
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

Give Feedback


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from