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. why this funny double is not a true zero?

why this funny double is not a true zero?

Scheduled Pinned Locked Moved C / C++ / MFC
databasehelptutorialquestion
5 Posts 4 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.
  • I Offline
    I Offline
    includeh10
    wrote on last edited by
    #1

    //print function

    CString fmt(double db)
    {
    CString cs;
    cs.Format("%f", db);
    return cs;
    }

    //problem

    void TheDlg::Test00()
    {
    const double dbInf = 1.0/::sin(0); //infinite
    const double dbZero = (::fabs(::cos(3.14159/2))<0.9)?0:1; //line 1

    CString cs;
    cs += fmt(dbZero)+"\\r\\n";          //line 2
    cs += fmt(dbZero \* dbInf)+"\\r\\n";  //line 3
    cs += fmt(0 \* dbInf)+"\\r\\n";       //line 4
    
    MessageBox(cs);
    

    }

    above dbZero only has 2 possible values : 0 or 1 - see line 1; output:

    0.000000
    -1.#IND00
    0.000000

    output of line 2 shows that dbZero is zero. output of line 3 shows that dbZero is not zero, since it is different from output of line 4. funny thing is that if change line 1 to:

    const double dbZero = (0.1<0.9)?0:1;
    

    Then all outputs are zeros. Why? How to make dbZero as a true zero in problem code?

    Richard Andrew x64R C E 3 Replies Last reply
    0
    • I includeh10

      //print function

      CString fmt(double db)
      {
      CString cs;
      cs.Format("%f", db);
      return cs;
      }

      //problem

      void TheDlg::Test00()
      {
      const double dbInf = 1.0/::sin(0); //infinite
      const double dbZero = (::fabs(::cos(3.14159/2))<0.9)?0:1; //line 1

      CString cs;
      cs += fmt(dbZero)+"\\r\\n";          //line 2
      cs += fmt(dbZero \* dbInf)+"\\r\\n";  //line 3
      cs += fmt(0 \* dbInf)+"\\r\\n";       //line 4
      
      MessageBox(cs);
      

      }

      above dbZero only has 2 possible values : 0 or 1 - see line 1; output:

      0.000000
      -1.#IND00
      0.000000

      output of line 2 shows that dbZero is zero. output of line 3 shows that dbZero is not zero, since it is different from output of line 4. funny thing is that if change line 1 to:

      const double dbZero = (0.1<0.9)?0:1;
      

      Then all outputs are zeros. Why? How to make dbZero as a true zero in problem code?

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      Floating point numbers are not exact. See: Comparing Floats[^]

      The difficult we do right away... ...the impossible takes slightly longer.

      1 Reply Last reply
      0
      • I includeh10

        //print function

        CString fmt(double db)
        {
        CString cs;
        cs.Format("%f", db);
        return cs;
        }

        //problem

        void TheDlg::Test00()
        {
        const double dbInf = 1.0/::sin(0); //infinite
        const double dbZero = (::fabs(::cos(3.14159/2))<0.9)?0:1; //line 1

        CString cs;
        cs += fmt(dbZero)+"\\r\\n";          //line 2
        cs += fmt(dbZero \* dbInf)+"\\r\\n";  //line 3
        cs += fmt(0 \* dbInf)+"\\r\\n";       //line 4
        
        MessageBox(cs);
        

        }

        above dbZero only has 2 possible values : 0 or 1 - see line 1; output:

        0.000000
        -1.#IND00
        0.000000

        output of line 2 shows that dbZero is zero. output of line 3 shows that dbZero is not zero, since it is different from output of line 4. funny thing is that if change line 1 to:

        const double dbZero = (0.1<0.9)?0:1;
        

        Then all outputs are zeros. Why? How to make dbZero as a true zero in problem code?

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        Just got a more consistent behaviour on Linux:

        #include <cstdio>
        #include <cmath>

        void out_double( double db )
        {
        printf("%f\n", db);
        }

        int main()
        {

        const double dbInf = 1.0/::sin(0); //infinite
        const double dbZero = (::fabs(::cos(3.14159/2))<0.9)?0:1; //line 1

        out_double( dbZero );
        out_double( dbZero * dbInf );
        out_double( 0 * dbInf );

        }

        outputs

        0.000000
        -nan
        -nan

        [update] The same program, on Windows 8, compiled with Visual Studio 2012, produces

        0.000000
        -1.#IND00
        -1.#IND00

        [/update]

        E 1 Reply Last reply
        0
        • C CPallini

          Just got a more consistent behaviour on Linux:

          #include <cstdio>
          #include <cmath>

          void out_double( double db )
          {
          printf("%f\n", db);
          }

          int main()
          {

          const double dbInf = 1.0/::sin(0); //infinite
          const double dbZero = (::fabs(::cos(3.14159/2))<0.9)?0:1; //line 1

          out_double( dbZero );
          out_double( dbZero * dbInf );
          out_double( 0 * dbInf );

          }

          outputs

          0.000000
          -nan
          -nan

          [update] The same program, on Windows 8, compiled with Visual Studio 2012, produces

          0.000000
          -1.#IND00
          -1.#IND00

          [/update]

          E Offline
          E Offline
          emersony
          wrote on last edited by
          #4

          Hey there !! >>> LOOK OUT >>> LOOK OUT >>> LOOK OUT >>> LOOK OUT >>> The problem is NOT on the dbZero - but on the dbInf. That explains everything you see. dbInf is 1 divided by zero, which is undefined. Anytime you try to use this value, you´ll have problems. That´s why you CAN print dbZero, but not dbInf or (dbZero * dbInf). Ok ?

          1 Reply Last reply
          0
          • I includeh10

            //print function

            CString fmt(double db)
            {
            CString cs;
            cs.Format("%f", db);
            return cs;
            }

            //problem

            void TheDlg::Test00()
            {
            const double dbInf = 1.0/::sin(0); //infinite
            const double dbZero = (::fabs(::cos(3.14159/2))<0.9)?0:1; //line 1

            CString cs;
            cs += fmt(dbZero)+"\\r\\n";          //line 2
            cs += fmt(dbZero \* dbInf)+"\\r\\n";  //line 3
            cs += fmt(0 \* dbInf)+"\\r\\n";       //line 4
            
            MessageBox(cs);
            

            }

            above dbZero only has 2 possible values : 0 or 1 - see line 1; output:

            0.000000
            -1.#IND00
            0.000000

            output of line 2 shows that dbZero is zero. output of line 3 shows that dbZero is not zero, since it is different from output of line 4. funny thing is that if change line 1 to:

            const double dbZero = (0.1<0.9)?0:1;
            

            Then all outputs are zeros. Why? How to make dbZero as a true zero in problem code?

            E Offline
            E Offline
            emersony
            wrote on last edited by
            #5

            Hey there !! LOOK OUT - LOOK OUT - LOOK OUT - LOOK OUT - :~ The problem is NOT on the dbZero - but on the dbInf. :confused: That explains everything you see. dbInf is 1 divided by zero, which is undefined. Anytime you try to use this value, you´ll have problems. That´s why you CAN print dbZero, but not dbInf or (dbZero * dbInf). Ok ?

            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