Click to See Complete Forum and Search --> : NaN - Detection
Jochen Kauffmann
October 9th, 2000, 06:59 AM
Hey Gurus,
I want to detect a NaN value.
This ...
public double myfunction()
{
return Double.NaN;
}
...
if (function()==Double.NaN)
System.out.println("NaN");
else
System.out.println("aN");
... prints "aN". How can I detect NaN?
Thanks
Jochen
Jprisco
October 9th, 2000, 09:01 AM
Im not to sure about this as Im pretty new myself... But I think if you tried to use NaN in an arithmatic expression it would throw an ArithmeticException Allthough probably the least efficient to do this Im pretty sure you could detect NaN in that way......
52901368
Jochen Kauffmann
October 9th, 2000, 04:27 PM
Hey James,
thanks for your reply. Unfortunately there is no ArithmeticException if you try it like this
try
{
function()
}
catch(Exception e)
{
System.out.println("exception");
}
double function()
{
return Double.NaN;
}
but I found out that the following snip works
Double i = new Double(function());
if (i.isNaN())
System.out.println("NaN");
This also works fine
Double i = new Double(0.0/0.0);
if (i.isNaN())
System.out.println("NaN");
It just looks very strange. Is there any other solution?
Jprisco
October 9th, 2000, 11:43 PM
I mistakenly assumed that using NaN would throw an exception if used in an arithmetic expression ie; x=method()*2.65 ; for example
if this were the case and this were "tryed" it would serve the task.... I was unaware of the fact however that using NaN in an arithmetic expression will not throw an exception but instead will asign the variable x in this case to NaN...... Goes to show you learn something new everyday Thanks!!!!
52901368
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.