Comparison Against NaN

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • better_cs_now@yahoo.com

    Comparison Against NaN

    Hello all,

    The code below outputs "FALSE" on my platform. Does the language
    guarantee this behavior?

    Thanks,
    Dave


    #include <iostream>
    #include <limits>

    using namespace std;

    int main()
    {
    double Result;

    try
    {
    Result = numeric_limits< double>::quiet_ NaN(); // 1.#QNAN

    if (Result == numeric_limits< double>::quiet_ NaN())
    cout << "TRUE" << endl;
    else
    cout << "FALSE" << endl;
    }
    catch(...)
    {
    cout << "Exception! !!" << endl;
    }
    }
  • robertwessel2@yahoo.com

    #2
    Re: Comparison Against NaN

    On Jul 10, 2:33 pm, "better_cs_...@ yahoo.com"
    <better_cs_...@ yahoo.comwrote:
    Hello all,
    >
    The code below outputs "FALSE" on my platform. Does the language
    guarantee this behavior?
    >
    Thanks,
    Dave
    >
    #include <iostream>
    #include <limits>
    >
    using namespace std;
    >
    int main()
    {
            double Result;
    >
            try
            {
                    Result = numeric_limits< double>::quiet_ NaN(); // 1.#QNAN
    >
                    if (Result == numeric_limits< double>::quiet_ NaN())
                            cout << "TRUE" << endl;
                    else
                            cout << "FALSE" << endl;
            }
            catch(...)
            {
                    cout << "Exception! !!" << endl;
            }
    >
    >
    >
    }

    C99 does if the implementation defines __STDC_IEC_559_ _. C89/90 says
    nothing about that, although your implementation might, nor does C99
    if that macro is not defined.

    Comment

    • robertwessel2@yahoo.com

      #3
      Re: Comparison Against NaN

      On Jul 10, 3:59 pm, "robertwess...@ yahoo.com"
      <robertwess...@ yahoo.comwrote:
      C99 does if the implementation defines __STDC_IEC_559_ _.  C89/90 says
      nothing about that, although your implementation might, nor does C99
      if that macro is not defined.

      And for some reason I though I was replying in comp.lang.c, not .c+
      +...

      Anyway, the current C++ standard is in the same boat as C89/90,
      although the next standard will (likely) include the C99 IEEE math
      support option.

      Comment

      • joseph  cook

        #4
        Re: Comparison Against NaN

        On Jul 10, 3:33 pm, "better_cs_...@ yahoo.com"
        <better_cs_...@ yahoo.comwrote:
        Hello all,
        >
        The code below outputs "FALSE" on my platform. Does the language
        guarantee this behavior?
        <snip>

        No,
        But the general idea of all types of NaN is that they always
        compare false, even to each other.

        Joe Cook

        Comment

        • Paavo Helde

          #5
          Re: Comparison Against NaN

          "better_cs_now@ yahoo.com" <better_cs_now@ yahoo.comkirjut as:
          Hello all,
          >
          The code below outputs "FALSE" on my platform. Does the language
          guarantee this behavior?
          If you ask so, then no (the presence of a NaN value is not required by
          the standard). However, maybe you wanted to ask another question: does
          the language guarantee that comparing NaN with itself yields false; in
          this case, the answer is yes, these comparison rules are part of the
          definition of a NaN.

          hth
          Paavo
          >
          Thanks,
          Dave
          >
          >
          #include <iostream>
          #include <limits>
          >
          using namespace std;
          >
          int main()
          {
          double Result;
          >
          try
          {
          Result = numeric_limits< double>::quiet_ NaN(); // 1.#QNAN
          >
          if (Result == numeric_limits< double>::quiet_ NaN())
          cout << "TRUE" << endl;
          else
          cout << "FALSE" << endl;
          }
          catch(...)
          {
          cout << "Exception! !!" << endl;
          }
          }

          Comment

          Working...