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. floating point

floating point

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
6 Posts 6 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.
  • N Offline
    N Offline
    nitin3
    wrote on last edited by
    #1

    void main { float a = 5.0 , b = 0.1 ; float c ; c = a + b ; if( c >= 5.1 ) { printf( "OK" ) ; } else { printf( "Not OK" ) ; } //in the above code c does not contain the the result i expected , it filled with 5.099999 and i get the output Not OK. how i can check the value of c ? please help me

    Thanks & Regards

    C N K A 4 Replies Last reply
    0
    • N nitin3

      void main { float a = 5.0 , b = 0.1 ; float c ; c = a + b ; if( c >= 5.1 ) { printf( "OK" ) ; } else { printf( "Not OK" ) ; } //in the above code c does not contain the the result i expected , it filled with 5.099999 and i get the output Not OK. how i can check the value of c ? please help me

      Thanks & Regards

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      That's because of floating point precision. Your float will never be represented exactly. Check this[^]

      Cédric Moonen Software developer
      Charting control [v1.4]

      1 Reply Last reply
      0
      • N nitin3

        void main { float a = 5.0 , b = 0.1 ; float c ; c = a + b ; if( c >= 5.1 ) { printf( "OK" ) ; } else { printf( "Not OK" ) ; } //in the above code c does not contain the the result i expected , it filled with 5.099999 and i get the output Not OK. how i can check the value of c ? please help me

        Thanks & Regards

        N Offline
        N Offline
        Nibu babu thomas
        wrote on last edited by
        #3

        nitin3 wrote:

        //in the above code c does not contain the the result i expected , it filled with 5.099999 and i get the output Not OK. how i can check the value of c ? please help me

        Try this... works for me...

        void main()
        {
        float a = 5.0f , b = 0.1f ;
        float c = a + b;

        if( c >= 5.1f )
        {
        printf( "OK" ) ;
        }
        else
        {
        printf( "Not OK" ) ;
        }
        }

        Always append "f" to a float value else it's treated as a double, earlier you were comparing a float var and a double var. Explained further -> http://nibuthomas.wordpress.com/2007/06/08/comparing-two-float-values/[^]

        Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

        1 Reply Last reply
        0
        • N nitin3

          void main { float a = 5.0 , b = 0.1 ; float c ; c = a + b ; if( c >= 5.1 ) { printf( "OK" ) ; } else { printf( "Not OK" ) ; } //in the above code c does not contain the the result i expected , it filled with 5.099999 and i get the output Not OK. how i can check the value of c ? please help me

          Thanks & Regards

          K Offline
          K Offline
          Kwanalouie
          wrote on last edited by
          #4

          floating point numbers are limited in resolution. They can get very close to the result, but not necessarily exact. You can minimize the problem by using "double" instead of "float", but that doesn't eliminate things. One quick fix is to simply use a small delta value in your computations. For example: float delta = 0.000000001; where delta is some number much smaller than you care about in your program. Then for greater than comparisons include it if (c >= (5.1 + delta)) printf("OK");

          D 1 Reply Last reply
          0
          • K Kwanalouie

            floating point numbers are limited in resolution. They can get very close to the result, but not necessarily exact. You can minimize the problem by using "double" instead of "float", but that doesn't eliminate things. One quick fix is to simply use a small delta value in your computations. For example: float delta = 0.000000001; where delta is some number much smaller than you care about in your program. Then for greater than comparisons include it if (c >= (5.1 + delta)) printf("OK");

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            Kwanalouie wrote:

            float delta = 0.000000001;

            Correctly defined as epsilon.

            "Love people and use things, not love things and use people." - Unknown

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            1 Reply Last reply
            0
            • N nitin3

              void main { float a = 5.0 , b = 0.1 ; float c ; c = a + b ; if( c >= 5.1 ) { printf( "OK" ) ; } else { printf( "Not OK" ) ; } //in the above code c does not contain the the result i expected , it filled with 5.099999 and i get the output Not OK. how i can check the value of c ? please help me

              Thanks & Regards

              A Offline
              A Offline
              Alan Balkany
              wrote on last edited by
              #6

              Another approach: Use rational numbers. Use ints for the numerator and denominator. It requires a bit more coding, but your fractional numbers will be exact.

              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