CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: NaN - Detection

  1. #1
    Join Date
    Jul 2000
    Location
    Germany
    Posts
    5

    NaN - Detection

    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


  2. #2
    Join Date
    Apr 2000
    Location
    NJ
    Posts
    123

    Re: NaN - Detection

    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

  3. #3
    Join Date
    Jul 2000
    Location
    Germany
    Posts
    5

    Re: NaN - Detection

    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?


  4. #4
    Join Date
    Apr 2000
    Location
    NJ
    Posts
    123

    Re: NaN - Detection

    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

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured