• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NaN

 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any time you would want to use NaN? Is it only a safety net?
Marilyn
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, anytime an math operation occurs on a float or double, NaN is the possible result. Such as:
double d1 = 3.5;
double d2 = 0.0;
double d3 = d1/d2;
Notice this doesn't throw any exceptions! I consider it good program design to test to ensure this hasn't occurred and handle it appropriatly for your program. The next lines of code are something like :
if(Double.isNaN(d3))
{
//do what you need
//set d3 to 0 or d1 value
//or throw a new ArithmeticException("Divide by Zero")
}
...
Hope this helps

 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Carl.
Marilyn
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was curious as to why we dont get an exception when we divide by 0.0 so I ran the above code. I didnt get the exception but the resulting d3 turned out to be infinity and not NAN.
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My appologys I didn't test the code and couldn't remember if dividing by zero was NaN or infinity. The reason that float and double math do not throw an exception is that they do have a valid reprsentation of these values to return. It's up to the programmer to determine if these are valid values for the program to continue. You can change the code to:
if(Double.isNaN(d3)| |Double.isInfinate(d3))
...
Intragral types (int, byte, long ...) throw an ArithmeticException because the cannot represent these values.
Hope this helps

[This message has been edited by Carl Trusiak (edited September 24, 2000).]
 
Harry Singh
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No Problem. I ended up learning more than I would have
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
All the three possible cases of errors in floating point arithmetic are shown below:
public class ErrorDoubleCalc
{
public static void main(String args[])
{

System.out.println( " Dividing 2.0 by 0.0 : " + 2.0/0.0 );
System.out.println( " Dividing -2.0 by 0.0 : " + -2.0/0.0 );
System.out.println( " Dividing 0.0 by 0.0 : " + 0.0/0.0 );
}
}

This is the output :

Dividing 2.0 by 0.0 : Infinity
Dividing -2.0 by 0.0 : -Infinity
Dividing 0.0 by 0.0 : NaN
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic