Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / June 2005

Tip: Looking for answers? Try searching our database.

result of (int)NaN ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tom - 14 Jun 2005 00:51 GMT
I have the following instruction in C++.NET :

  x = log10(n);
  int y = (int)(x * h);

where x, h, n are of type double, and x receives the value NaN
(because the value of number is rather small).

This results in y having value of the largest negative 32-bit integer.

What does the standard specify in this instance?
   Should an exception be thrown?
   Will the sign of Y always be known?

Right now the workaround is to test the value of n, to make sure that it is
within the range of the log10 function else force a value to y.

   -- Tom
Mike Welham - 14 Jun 2005 17:22 GMT
Hi Tom,

> I have the following instruction in C++.NET :
>
[quoted text clipped - 3 lines]
> where x, h, n are of type double, and x receives the value NaN
> (because the value of number is rather small).

The conversion from double to int will be compiled to a 'conv.i4' IL
instruction.

> This results in y having value of the largest negative 32-bit integer.
>
> What does the standard specify in this instance?
>     Should an exception be thrown?
>     Will the sign of Y always be known?

The ECMA spec (partition III, section 3.27) has this to say about conv.i4:

- "Conversion from floating-point numbers to integral values truncates
the number toward zero."

- "If overflow occurs converting a floating-point type to an integer the
value returned is unspecified."

- "No exceptions are ever thrown."

> Right now the workaround is to test the value of n, to make sure that it is
> within the range of the log10 function else force a value to y.

There is a 'conv.ovf.i4' IL instruction that will throw an
OverflowException if the conversion fails.

In C# you can ask the compiler for this behaviour by doing:

    int y = checked((int)(x * h));

I don't know how to use checked arithmetic and conversion operations in
managed C++, perhaps somebody else has that answer?

Best Regards

Mike
Tom - 15 Jun 2005 01:15 GMT
Hi, Mike.

   Could not find the 'checked' modifier in C++.NET, but
   I did find the method:         static bool  Double::IsNAN(double)
   so I can test for that.   Thanks for your help.

   -- Tom

> Hi Tom,
>
[quoted text clipped - 42 lines]
>
> Mike

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.