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. double signed comparison in C++

double signed comparison in C++

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
3 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.
  • K Offline
    K Offline
    kamal1977
    wrote on last edited by
    #1

    double a = -60 ; double b = -7500 bool flag; if (a < b) { flag = true; } For the above check flag is getting true, it is not supposed to, how can I make the double negative comparison?

    CPalliniC V 2 Replies Last reply
    0
    • K kamal1977

      double a = -60 ; double b = -7500 bool flag; if (a < b) { flag = true; } For the above check flag is getting true, it is not supposed to, how can I make the double negative comparison?

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Comparison of double values behaves as expected, of course (and, as it stands, your code doesn't compile): the program

      #include int main()
      {
      double a = -60 ;
      double b = -7500;

      bool flag = false;

      if (a < b)
      {
      flag = true;
      }

      std::cout << std::boolalpha << flag << std::endl;;
      }

      ouptus

      false

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • K kamal1977

        double a = -60 ; double b = -7500 bool flag; if (a < b) { flag = true; } For the above check flag is getting true, it is not supposed to, how can I make the double negative comparison?

        V Offline
        V Offline
        Victor Nijegorodov
        wrote on last edited by
        #3

        It is because your**

        Quote:

        bool flag;

        **was NOT initialized and contains garbage that is treated as true just because it is not zero. Try:

        double a = -60 ;
        double b = -7500

        bool flag = (a < b);

        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