Comparing to float.NaN throws ArithmeticException

dotnet framework


  • 1. Interoperating with Word
    Hello. I need to let users edit a word document within my program. So i'm looking for a class derived from System.Windows.Forms.Control i can use to simply put in my form and let the users do whatever they want in word,and i'll process the document when they're done editing. Suggestions anyone? Thank you
  • 2. Access global variables in Active X DLL from .NET
    Hi All, I have referenced a Vb6 Active X DLL in my .NET project and I can see all my COM Classes in the DLL no problem. However I cannot see any of my global variables or public functions/subs which are in a module in the COM DLL. Is it possible to see these from my Vb .NET application? If not, what are my options? Thanks CodeMonkey.
  • 3. C# new Class does not recognized from unmanaged code
    Hi, I have developed .NET 2005 C# library with one class X and register it as COM interop. I have enabled the ComVisible in AssemblyInfo and have checked ComInterop in project build properties. I am using this class X from VBScript with CreateObject. Everything works great. The problem starts when i have added second class Y in the same project. Now when i creating object from VBScipt, i can only create class X object. For some reason i can't create the second new class Y object. I have tried to rebuild both project and solutions. Changed GUID and AssemblyVersion in AssemblyInfo. Nothing helps. Any suggestions ? Thanks, Pavel

Comparing to float.NaN throws ArithmeticException

Postby RRIMSDN » Sat, 05 Mar 2005 06:35:49 GMT

We have a DLL compiled under Borland C++ Builder 5 calling an exported C 
entry point in a Mixed Mode Managed C++ DLL. This function in turn calls 
functions in a C# assembly. The application executable is also compiled 
by Borland.

We encountered an ArithmeticException thrown from the 
System.Drawing.Font constructor, and using a disassembler, found it to 
be on the line:

if (((emSize == float.NaN) || (emSize == float.NegativeInfinity)) || 
(((emSize == float.PositiveInfinity) || (emSize <= 0f)) || (emSize > 

in the function

private void Initialize(FontFamily family, float emSize, FontStyle 
style, GraphicsUnit unit, byte gdiCharSet, bool gdiVerticalFont)

So, we pulled out the line into our own C# code, where it still throws 
an exception, and narrowed it down to the portion:

(emSize == float.NaN)

This expression throws an ArithmeticException exception with the message 
"Overflow or underflow in the arithmetic operation." The rest of the 
comparisons work correctly, and assigning float.NaN to a variable also 
works, but then trying to compare that variable to another throws the 

However, if I change the statement to float.IsNaN(emSize), it doesn't 
throw an exception. The docs for float.NaN say:
"Use IsNaN to determine whether a value is not a number. It is not 
possible to determine whether a value is not a number by comparing it to 
another value equal to NaN."

So, is Microsoft violating their own rules here?

Also, if I try a literal comparison, e.g. (15 == float.NaN), this does 
not throw.

So, what could be going wrong? When the code is called from an unmanaged 
DLL compiled with Microsoft's compiler, we don't have this problem. So 
it seems that Borland is doing something differently that causes this 
problem. Any ideas what this could be, or any way we can get around it?


Re: Comparing to float.NaN throws ArithmeticException

Postby Dave Paul » Sat, 05 Mar 2005 07:25:46 GMT

Ok, here's some more info. The second time this is attempted, it works.
So it seems like Borland is somehow not doing something it should to
initialize the floating point system? Is there some way I can ensure
that it will be initialized without triggering the exceptions and
ignoring it?

exported C



Re: Comparing to float.NaN throws ArithmeticException

Postby v-schang » Sat, 05 Mar 2005 18:18:38 GMT

Hi Dave,

Thank you for your posting. Regarding on the issue, we'll try 
finding proper resource to assist you and we will update as soon as posible.


Steven Cheng
Microsoft Online Support

Get Secure! posting is provided "AS IS", 
with no warranties, and confers no rights.)

Re: Comparing to float.NaN throws ArithmeticException

Postby Dave Paul » Sun, 06 Mar 2005 04:48:54 GMT

Here's something else interesting. If I try to do the same comparison
from managed C++ like this:

System::Single x = 15;
bool b = (x == System::Single::NaN);

Not only do I get the same exception being thrown, but I get the
compiler warning "warning C4756: overflow in constant arithmetic" on
the second line.

The reason I have this code is to trigger the first exception and
ignore it, letting subsequent attempts work corretly. So, the full code
is this:

try {
	System::Single x = 15;
	bool b = (x == System::Single::NaN);
catch (...)

It works, but in addition to the warning above, I also get a "warning
C4702: unreachable code" on the closing brace of the catch statement,
which doesn't seem to make sense


Re: Comparing to float.NaN throws ArithmeticException

Postby v-phuang » Tue, 08 Mar 2005 14:42:07 GMT


Based on my understanding, if we use the == to compare a float with 
float.IsNaN will get exception in VS.NET C#/C++.

float f=1.0f;
if(float.NaN ==f)

int _tmain()
                Console::WriteLine(S"Hello World");
	System::Single x = 15;
	bool b = (x == System::Single::NaN);
	return 0;

But I can not reproduce the problem on my side with code above.
Or do you mean that the problem only occur when you do this in a 
BorlandCompiled C++ dll?
If so, I think you may also try to post the problem in the Borland's 
support web page as below.

If I have any misunderstanding, please feel free to let me know.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! -
This posting is provided "AS IS" with no warranties, and confers no rights.

Re: Comparing to float.NaN throws ArithmeticException

Postby Dave Paul » Wed, 09 Mar 2005 01:10:08 GMT

The problem occurs in a C# dll or a Managed C++ dll, compiled by
microsoft's compiler. The problem seems to only occur when that dll is
loaded by an application that is compiled with Borland's compiler. I
don't know if this is specific to Borland's compiler, but it seems like
there should be a way to make the managed code work the same regardless
of the code that calls it.

Re: Comparing to float.NaN throws ArithmeticException

Postby v-phuang » Wed, 09 Mar 2005 14:38:08 GMT

Hi Dave,

So the problem occurred when the C#/MC++ dll compiled with MS compiler is 
called by a Borland C++ Builder 5 compiled Application.
Since we did not known how the Borland compiler works, I think the Borland 
compiled code is not same with the VC++ compiled one, that is why I suggest 
you contact Borland, I think they can investigatet why the exception will 

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! -
This posting is provided "AS IS" with no warranties, and confers no rights.

Re: Comparing to float.NaN throws ArithmeticException

Postby Dave Paul » Fri, 11 Mar 2005 03:07:47 GMT

I have posted to a Borland newsgroup as you suggested.

However, I'm still not quite sure you understand the problem. The
exception does not occur when the code is called, all of that works
correctly. Once inside of the managed code however, floating point
operations behave differently. Once inside the managed code, we're
executing the same instructions as if the function had been called by
an executable compiled by any other compiler (I would assume... I
suppose the JIT compiler could be behaving differently, which seems
like it would be an even bigger problem).

The only explanation I can think of is that there are certain steps
that take place at application initialization to somehow set up
floating point arithmetic that are either not being done or being done
differently. I can contact Borland and ask them "What does your
compiler do differently than Microsoft's", but that doesn't really help
me, and I think it's much more eefficient to ask you "What sort of
initialization steps must take place before perforoming floating point
operations in a Managed Assembly"? Whatever these steps are, they seem
to occur before the second time I try to reproduce the issue above, so
triggering the exception seems to be one way of ensuring that
everything is properly initialized, but it also seems like a lousy way
to do that.

Also, I think it is a bug that System.Drawing.Font is using == to test
for NaN instead of float.IsNaN, since the documentation explicitly says
you are not allowed to do that.

Thanks for your help,

Re: Comparing to float.NaN throws ArithmeticException

Postby v-phuang » Fri, 11 Mar 2005 16:37:10 GMT


Thank you for your reply.
So I assume your scenario is as below.
Borland compiled C++ app---> C# DLL registered as COM(using 
System.Drawing.Font) will get the exception
MS VC6 app                         ------>the same C# DLL registered as COM 
with above will work fine.

The two tests ran on the same environment.

And I assume that you are using the same C++ source code and the only 
difference is that we use two compilers.

So I guess the possiblity is that we will use two versions of CRuntime, as 
we know that it is the CRuntime that load the main function in the C++ 
source which may make the different, since the following steps, load 
COM,clr, JIT is handled by the same library on the windows machine and that 
is why I recommend that you contact Borland.

Also since the problem needed further detailed troubleshooting to see what 
is the problem(e.g. dump analyse), I think you may also try to contact 
MSPSS via the link below.

If we confirm that this is a problem in our product, the incident fee will 
be refund.

Thanks for your understanding!
If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! -
This posting is provided "AS IS" with no warranties, and confers no rights.

Re: Comparing to float.NaN throws ArithmeticException

Postby v-phuang » Sat, 12 Mar 2005 10:50:01 GMT


In addition, for the issue,

We will write email to product group on it.

Many thanks for your feedback.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! -
This posting is provided "AS IS" with no warranties, and confers no rights.

Similar Threads:

1.'System.ArithmeticException was unhandled' at System.Double.CompareTo(Double value) when comparing with Double.NaN

2.Double.NaN and SQL float data type


I use C# to fill a data field of "float" data type in SQL 2000 server. When
I use Double.NaN value, there seems to be a problem in "Query Analyzer". I
can no longer select the row, an error message: "Numeric value out of
range". However, in Enterprise Manager, I can still see the row with the
"float" data field of value: "-1.#IND".

My question: is this just a bug/flaw in query analyzer, or I am not supposed
to assign Double.NaN to a "float" data type in SQL 2000?


3.assign NaN, +Inf, -Inf to double/float

Hello everybody,

how can I assign one of the above values to a float or double?


4.Comparing different floats fails

In the following piece of code, the == operator is saying that the two
different float values are the same.  What's going on here?

float testValueA = float.MaxValue;
float testValueB = float.MaxValue - (float)1.0;
if (testValueA == testValueB)
    Console.WriteLine("Test Failed");

5.Compare a double to a float?

Is it possible to compare a double with a float in c# without casting to 
one of the types first?

It appears you cannot...

float num = 1.545f;
double dnum = 1.545;

if (dnum == num)
	Console.WriteLine("You can");
	Console.WriteLine("You cant");

6. how can I compare floating point numbers?

7. How can I add '+' or '-' before float value using float.ToString(

8. storing floating point data with using float or double

Return to dotnet framework


Who is online

Users browsing this forum: No registered users and 26 guest