Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Default Is it possible to test for NaN?

    Hello. I want to test a value of a variable for an error message "NaN" (as there has been a division by 0). Can this be done?

    Many thanks

  2. #2
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Default

    I have researched this further and recognise that there is a command:
    isNan from
    Double (Java 2 Platform SE 5.0))

    The problem is I do not know how work it with my program. I have a variable called statistics[1] and I want to test to see if it is NaN

    I am very thankful for any help.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Java Code:
    Double.isNaN(doubleVar)
    returns true when it is an NaN. Or

    Java Code:
    doubleVar != doubleVar
    also returns true for an NaN. This, in fact, is the IEEE defined test for NaN and is exactly what isNaN does.

  4. #4
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Default

    I note the program says Double.isNaN(doubleVar)

    Does this mean the variable tested has to be a double?

    I have an integer called stats2[0]. I want to test it for errors (i.e. isNAN). I have tried using
    boolean error=Double.isNaN(stats2[1]);

    However, the variable always returns as false (when it is a proper value).

    Please help!

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by AJArmstron@aol.com View Post
    I note the program says Double.isNaN(doubleVar)

    Does this mean the variable tested has to be a double?

    I have an integer called stats2[0]. I want to test it for errors (i.e. isNAN). I have tried using
    boolean error=Double.isNaN(stats2[1]);

    However, the variable always returns as false (when it is a proper value).

    Please help!
    Ints can never be NaN.

    kind regards,

    Jos

  6. #6
    Join Date
    Apr 2010
    Posts
    59
    Rep Power
    0

    Default

    JosAH
    but somehow mine are!

    I worked out what I was doing wrong and have now got the test working successfully.

    Thank you.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by AJArmstron@aol.com View Post
    JosAH
    but somehow mine are!

    I worked out what I was doing wrong and have now got the test working successfully.

    Thank you.
    Didn't pay attention to the "fact" that you had an int. In any case, as you've been told ints cannot be NaN. If you claim they are than they are not ints. So, either you have a double, or you have an Integer (not an int) and it is null, and not NaN.

  8. #8
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    the bit of code from your code if displayed would be helpful.

    are you trying to divide the result by zero?.

Similar Threads

  1. how did everyone do on the ap test last tuesday?
    By blueduiker in forum New To Java
    Replies: 8
    Last Post: 05-07-2010, 01:56 AM
  2. A test-question on XSL-FO.
    By an24 in forum XML
    Replies: 0
    Last Post: 12-28-2009, 02:36 PM
  3. Connectivity Test
    By jimhaddon in forum Java Applets
    Replies: 9
    Last Post: 08-16-2008, 04:40 PM
  4. Replies: 0
    Last Post: 04-05-2008, 12:38 AM
  5. Palindrome Test
    By Ada in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 01:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •