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. Float value

Float value

Scheduled Pinned Locked Moved C / C++ / MFC
question
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.
  • J Offline
    J Offline
    john5632
    wrote on last edited by
    #1

    I am passing float value to a varibale, but I am not getting correct value float Val1=4.6; float Val2 = Val1;//getting 4.5999999 I am getting 4.5999999, How can I get the return value as 4.6?

    C D D R 4 Replies Last reply
    0
    • J john5632

      I am passing float value to a varibale, but I am not getting correct value float Val1=4.6; float Val2 = Val1;//getting 4.5999999 I am getting 4.5999999, How can I get the return value as 4.6?

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

      This is due to floating point precision. See here[^] (or google for it) for more information. If you really need more precision, you can always use a double (but I doubt that you need it).

      Cédric Moonen Software developer
      Charting control [v3.0] OpenGL game tutorial in C++

      1 Reply Last reply
      0
      • J john5632

        I am passing float value to a varibale, but I am not getting correct value float Val1=4.6; float Val2 = Val1;//getting 4.5999999 I am getting 4.5999999, How can I get the return value as 4.6?

        D Offline
        D Offline
        Dr Walt Fair PE
        wrote on last edited by
        #3

        Welcome to floating point math! Your result is typical and is why the #1 rule on doing floating point math is to never depend on the value being exact. You will need to compensate or use a higher precision number type, depending on what you are trying to accomplish. In most situations the difference in calculated values is not significant, but sometimes it is. The worst resolution problems usually occur when you subtract 2 numbers that are close, then do further manipulations. If you are checking the value with if statements, for example, then you need to check that the value is within a reasonable tolerance. Search around using Google and Bing for "floating point precision" and you should find lots of information.

        CQ de W5ALT

        Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

        1 Reply Last reply
        0
        • J john5632

          I am passing float value to a varibale, but I am not getting correct value float Val1=4.6; float Val2 = Val1;//getting 4.5999999 I am getting 4.5999999, How can I get the return value as 4.6?

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

          Read here.

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "Man who follows car will be exhausted." - Confucius

          S 1 Reply Last reply
          0
          • D David Crow

            Read here.

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "Man who follows car will be exhausted." - Confucius

            S Offline
            S Offline
            Sauro Viti
            wrote on last edited by
            #5

            Wow! Fantastic article: even those who already know the topic should read (and learn) it!

            1 Reply Last reply
            0
            • J john5632

              I am passing float value to a varibale, but I am not getting correct value float Val1=4.6; float Val2 = Val1;//getting 4.5999999 I am getting 4.5999999, How can I get the return value as 4.6?

              R Offline
              R Offline
              Rick York
              wrote on last edited by
              #6

              Deja vu all over again : http://www.codeproject.com/Messages/3665088/CString-to-double-modified.aspx[^] Complete with the same answer.

              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