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 precision lost: NEED HELP!

Floating point precision lost: NEED HELP!

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
11 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.
  • U User 393989

    I've got HUGE problems with floating point precision lost in the following code: - s*(a.y-c.y) - h*(b.y-c.y) where s = h = 0.333.... c.y = 0.675 a.y = b.y = 0 The result is about 5.e-17 (as I understand, NAN). How to overcome the problem, forex find out that precision lost occured. Thank you for help, A. M. gp123456

    T Offline
    T Offline
    toxcct
    wrote on last edited by
    #2

    try a.y = b.y = 0**.0**;


    TOXCCT >>> GEII power

    U 1 Reply Last reply
    0
    • T toxcct

      try a.y = b.y = 0**.0**;


      TOXCCT >>> GEII power

      U Offline
      U Offline
      User 393989
      wrote on last edited by
      #3

      :confused: toxcct wrote: try a.y = b.y = 0.0; Of cource, a.y & b.y are doubles too! So read a.y = 0.0, b.y = 0.0 Suppose you haven't understood my question: precision lost is a result of existing in my program double values with infinite part after floating point, e.g. that s = 0.3333... (infinite 3's after floating point) & h = 0.3333... (infinite 3's after floating point).

      T 1 Reply Last reply
      0
      • U User 393989

        :confused: toxcct wrote: try a.y = b.y = 0.0; Of cource, a.y & b.y are doubles too! So read a.y = 0.0, b.y = 0.0 Suppose you haven't understood my question: precision lost is a result of existing in my program double values with infinite part after floating point, e.g. that s = 0.3333... (infinite 3's after floating point) & h = 0.3333... (infinite 3's after floating point).

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #4

        could you give a piece of code (the equation calculation for example) ?


        TOXCCT >>> GEII power

        1 Reply Last reply
        0
        • U User 393989

          I've got HUGE problems with floating point precision lost in the following code: - s*(a.y-c.y) - h*(b.y-c.y) where s = h = 0.333.... c.y = 0.675 a.y = b.y = 0 The result is about 5.e-17 (as I understand, NAN). How to overcome the problem, forex find out that precision lost occured. Thank you for help, A. M. gp123456

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #5

          Loss of precision is an inherent problem with floating-point numbers (float and double). You should never test for equality between two floats/doubles because of this, but instead test that the difference is less than some threshhold. To get around this, you'll need to use some alternate means of manipulating floating-point numbers. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- "Just because the box has 2 gigabytes of memory doesn't mean you get to use it all!"   -- Rico Mariani, CLR perf guy

          U 1 Reply Last reply
          0
          • U User 393989

            I've got HUGE problems with floating point precision lost in the following code: - s*(a.y-c.y) - h*(b.y-c.y) where s = h = 0.333.... c.y = 0.675 a.y = b.y = 0 The result is about 5.e-17 (as I understand, NAN). How to overcome the problem, forex find out that precision lost occured. Thank you for help, A. M. gp123456

            M Offline
            M Offline
            Member 421025
            wrote on last edited by
            #6

            It would be wise to compare the result from above formula with result from -s*ay - h*by + cy*(s+h) and if results dont compare have some kind of error recovery. if calculations are not too many and time bound most wise programmers(not me) calculate with 2 formulae and compare results.

            1 Reply Last reply
            0
            • U User 393989

              I've got HUGE problems with floating point precision lost in the following code: - s*(a.y-c.y) - h*(b.y-c.y) where s = h = 0.333.... c.y = 0.675 a.y = b.y = 0 The result is about 5.e-17 (as I understand, NAN). How to overcome the problem, forex find out that precision lost occured. Thank you for help, A. M. gp123456

              T Offline
              T Offline
              Tim Smith
              wrote on last edited by
              #7

              Mike is correct. However, there is something more going on. Are you sure that "s == h"? Even with floating point imprecision, I don't see how the two parts of the expression won't cancel each other out. I tried this with initializing h and s to 1.0/3.0 and it worked properly. However, generally speaking, checking for "== 0" with floating point numbers is a VERY bad idea. Usually you need to check if "- epsilon < x < epsilon" where epsilon is your tolerance for imprecision. Tim Smith I'm going to patent thought. I have yet to see any prior art.

              U 1 Reply Last reply
              0
              • T Tim Smith

                Mike is correct. However, there is something more going on. Are you sure that "s == h"? Even with floating point imprecision, I don't see how the two parts of the expression won't cancel each other out. I tried this with initializing h and s to 1.0/3.0 and it worked properly. However, generally speaking, checking for "== 0" with floating point numbers is a VERY bad idea. Usually you need to check if "- epsilon < x < epsilon" where epsilon is your tolerance for imprecision. Tim Smith I'm going to patent thought. I have yet to see any prior art.

                U Offline
                U Offline
                User 393989
                wrote on last edited by
                #8

                You know, I'm not checking if h==s, I just execute the code. I found out that h==s using debugger. I'm interested in way to find out that precision lost occured in MS VisualC++ 7.0.

                1 Reply Last reply
                0
                • M Michael Dunn

                  Loss of precision is an inherent problem with floating-point numbers (float and double). You should never test for equality between two floats/doubles because of this, but instead test that the difference is less than some threshhold. To get around this, you'll need to use some alternate means of manipulating floating-point numbers. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- "Just because the box has 2 gigabytes of memory doesn't mean you get to use it all!"   -- Rico Mariani, CLR perf guy

                  U Offline
                  U Offline
                  User 393989
                  wrote on last edited by
                  #9

                  Michael Dunn wrote: you'll need to use some alternate means of manipulating floating-point numbers. You right! But I'd like to know which one exactly. Some useful links are welcomed.

                  1 Reply Last reply
                  0
                  • U User 393989

                    I've got HUGE problems with floating point precision lost in the following code: - s*(a.y-c.y) - h*(b.y-c.y) where s = h = 0.333.... c.y = 0.675 a.y = b.y = 0 The result is about 5.e-17 (as I understand, NAN). How to overcome the problem, forex find out that precision lost occured. Thank you for help, A. M. gp123456

                    H Offline
                    H Offline
                    Hing
                    wrote on last edited by
                    #10

                    Follow the link below, you will know why there is loss in precision: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/\_core\_why\_floating\_point\_numbers\_may\_lose\_precision.asp To solve the problem, there is a free library called MAPM which can help you to calculate precisly: http://www.tc.umn.edu/~ringx004/mapm-main.html Hope that it can help.

                    U 1 Reply Last reply
                    0
                    • H Hing

                      Follow the link below, you will know why there is loss in precision: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/\_core\_why\_floating\_point\_numbers\_may\_lose\_precision.asp To solve the problem, there is a free library called MAPM which can help you to calculate precisly: http://www.tc.umn.edu/~ringx004/mapm-main.html Hope that it can help.

                      U Offline
                      U Offline
                      User 393989
                      wrote on last edited by
                      #11

                      The problem solved itself! I've changed computer (from Celeron 800 MHz to Celeron 2GHz), and the equation doesn't affects the results! Anybody knows the reason? I heard something about bugs in FPU of P300-1000... Anyway, thank you very much for links, Hing. They are very useful for me.

                      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