Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Strange behaviour with numeric_limits<double>::quiet_NaN ()

Strange behaviour with numeric_limits<double>::quiet_NaN ()

Scheduled Pinned Locked Moved C / C++ / MFC
c++debugginghelpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sylv33
    wrote on last edited by
    #1

    Hello, Ok, here's the (very simplified) code that's been causing me headaches :

    double f = numeric_limits<double>::quiet_NaN ();
    int n = (f == numeric_limits<double>::quiet_NaN ());

    Can someone *please* explain why n is always 0 here?? After the second line is executed, the debugger shows:

    std::numeric_limits<double>::quiet_NaN returned 1.#QNAN00000000000 double
    f 1.#QNAN00000000000 double

    In case this is some kind of processor issue, I'll should also mention that I'm using VC++ 2005 on an AMD Turion 64 running Windows XP. Thx ! Sly

    A CPalliniC 2 Replies Last reply
    0
    • S Sylv33

      Hello, Ok, here's the (very simplified) code that's been causing me headaches :

      double f = numeric_limits<double>::quiet_NaN ();
      int n = (f == numeric_limits<double>::quiet_NaN ());

      Can someone *please* explain why n is always 0 here?? After the second line is executed, the debugger shows:

      std::numeric_limits<double>::quiet_NaN returned 1.#QNAN00000000000 double
      f 1.#QNAN00000000000 double

      In case this is some kind of processor issue, I'll should also mention that I'm using VC++ 2005 on an AMD Turion 64 running Windows XP. Thx ! Sly

      A Offline
      A Offline
      Adam Roderick J
      wrote on last edited by
      #2

      if you have condition of exceptipon say a case of divide by zero of double then you get this value 1.#QNAN00000000000. Where NAN represents Not a Number([^]) Now in this case of divide by zero double numerator = 0; double denominator = 0; double d = static_cast<double>(numerator)/static_cast<double>(denominator); value of d is not infinite, but it cannot be calculated, so it given as NaN. well about the second line int n = (f == numeric_limits<double>::quiet_NaN ()); the value of n is compiler dependent so it will change accordingly. for Vs 2005 it may be 0 for some other earlier versions it may be 1.

      Величие не Бога может быть недооценена.

      modified on Thursday, March 4, 2010 6:31 AM

      1 Reply Last reply
      0
      • S Sylv33

        Hello, Ok, here's the (very simplified) code that's been causing me headaches :

        double f = numeric_limits<double>::quiet_NaN ();
        int n = (f == numeric_limits<double>::quiet_NaN ());

        Can someone *please* explain why n is always 0 here?? After the second line is executed, the debugger shows:

        std::numeric_limits<double>::quiet_NaN returned 1.#QNAN00000000000 double
        f 1.#QNAN00000000000 double

        In case this is some kind of processor issue, I'll should also mention that I'm using VC++ 2005 on an AMD Turion 64 running Windows XP. Thx ! Sly

        CPalliniC Online
        CPalliniC Online
        CPallini
        wrote on last edited by
        #3

        See [^]:

        A comparison with a NaN always returns an unordered result even when comparing with itself. The comparison predicates are either signaling or non-signaling, the signaling versions signal an invalid exception for such comparisons. The equality and inequality predicates are non-signaling so x = x returning false can be used to test if x is a quiet NaN. The other standard comparison predicates all signal if they receive a NaN operand, the standard also provides non-signaling versions of these other predicates. The predicate isNaN(x) determines if a value is a NaN and never signals an exception.

        :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        In testa che avete, signor di Ceprano?

        S 1 Reply Last reply
        0
        • CPalliniC CPallini

          See [^]:

          A comparison with a NaN always returns an unordered result even when comparing with itself. The comparison predicates are either signaling or non-signaling, the signaling versions signal an invalid exception for such comparisons. The equality and inequality predicates are non-signaling so x = x returning false can be used to test if x is a quiet NaN. The other standard comparison predicates all signal if they receive a NaN operand, the standard also provides non-signaling versions of these other predicates. The predicate isNaN(x) determines if a value is a NaN and never signals an exception.

          :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          S Offline
          S Offline
          Sylv33
          wrote on last edited by
          #4

          Thanks to the both of you for the explanation! I ending up changing all the relevant code to use isnan(f) instead of direct comparisons and everything works fine now but I was still wondering why a direct comparison with a NaN always returned 0 on my compiler. Now I understand! :-D

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups