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. divide by 10 problem

divide by 10 problem

Scheduled Pinned Locked Moved C / C++ / MFC
help
8 Posts 5 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
    IGeorgeI
    wrote on last edited by
    #1

    This has been a problem of mine for years. It appears to be compiler specific which is why I put it in this section. Run this piece of code double test = 3.0/10.0; The result of test is .299999999999999 I know why this happens. I was just wondering how some of you have solved this problem. :-D Thanks for any input

    George W Software Developer www.zsystems.ca

    D C J 3 Replies Last reply
    0
    • I IGeorgeI

      This has been a problem of mine for years. It appears to be compiler specific which is why I put it in this section. Run this piece of code double test = 3.0/10.0; The result of test is .299999999999999 I know why this happens. I was just wondering how some of you have solved this problem. :-D Thanks for any input

      George W Software Developer www.zsystems.ca

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

      IGeorgeI wrote:

      I know why this happens. I was just wondering how some of you have solved this problem.

      If you know why it happens, then you must also know it has no solution. Floating-point numbers behave that way on binary machines.


      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

      "Judge not by the eye but by the heart." - Native American Proverb

      I 1 Reply Last reply
      0
      • D David Crow

        IGeorgeI wrote:

        I know why this happens. I was just wondering how some of you have solved this problem.

        If you know why it happens, then you must also know it has no solution. Floating-point numbers behave that way on binary machines.


        "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

        "Judge not by the eye but by the heart." - Native American Proverb

        I Offline
        I Offline
        IGeorgeI
        wrote on last edited by
        #3

        Of course but when you are dealing with high precision numbers, you have to implement workarounds to solve the problem. I was just wondering how others have handled it. You simply cannot leave it like that when dealing with mathematical engines written in c++. The error will carry through and get worse. So I just wanted to see what others are doing. For your information, the c# compiler does solve the problem and does give you to correct base 10 answer of .30

        George W Software Developer www.zsystems.ca

        D realJSOPR 2 Replies Last reply
        0
        • I IGeorgeI

          Of course but when you are dealing with high precision numbers, you have to implement workarounds to solve the problem. I was just wondering how others have handled it. You simply cannot leave it like that when dealing with mathematical engines written in c++. The error will carry through and get worse. So I just wanted to see what others are doing. For your information, the c# compiler does solve the problem and does give you to correct base 10 answer of .30

          George W Software Developer www.zsystems.ca

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

          IGeorgeI wrote:

          For your information, the c# compiler does solve the problem and does give you to correct base 10 answer of .30

          Perhaps it makes use of some sort of epsilon value. See here for more.


          "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

          "Judge not by the eye but by the heart." - Native American Proverb

          1 Reply Last reply
          0
          • I IGeorgeI

            This has been a problem of mine for years. It appears to be compiler specific which is why I put it in this section. Run this piece of code double test = 3.0/10.0; The result of test is .299999999999999 I know why this happens. I was just wondering how some of you have solved this problem. :-D Thanks for any input

            George W Software Developer www.zsystems.ca

            C Offline
            C Offline
            Chris Losinger
            wrote on last edited by
            #5

            IGeorgeI wrote:

            I was just wondering how some of you have solved this problem.

            add a tiny amount to the result of the division, then truncate. aka. 'round up' yes, it biases the results in one direction, but them's the breaks.

            image processing toolkits | batch image processing | blogging

            1 Reply Last reply
            0
            • I IGeorgeI

              Of course but when you are dealing with high precision numbers, you have to implement workarounds to solve the problem. I was just wondering how others have handled it. You simply cannot leave it like that when dealing with mathematical engines written in c++. The error will carry through and get worse. So I just wanted to see what others are doing. For your information, the c# compiler does solve the problem and does give you to correct base 10 answer of .30

              George W Software Developer www.zsystems.ca

              realJSOPR Online
              realJSOPR Online
              realJSOP
              wrote on last edited by
              #6

              IGeorgeI wrote:

              For your information, the c# compiler does solve the problem and does give you to correct base 10 answer of .30

              First, this isn't a C# forum. Second, If you're doing this in C++, you're going to have to write some code to "fix" it. Write yourself a rounding function that will round to the nearest whole number. It ain't rocket science. Third, you're arguing with someone that's trying to help you. Not very smart, even for a C# programmer.

              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

              I 1 Reply Last reply
              0
              • realJSOPR realJSOP

                IGeorgeI wrote:

                For your information, the c# compiler does solve the problem and does give you to correct base 10 answer of .30

                First, this isn't a C# forum. Second, If you're doing this in C++, you're going to have to write some code to "fix" it. Write yourself a rounding function that will round to the nearest whole number. It ain't rocket science. Third, you're arguing with someone that's trying to help you. Not very smart, even for a C# programmer.

                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                I Offline
                I Offline
                IGeorgeI
                wrote on last edited by
                #7

                I am not a C# programmer by trade. I am a C/C++/Java/MFC developer. 1) I posted my C++ question in the C++ section of this forum. I merely added the fact about the C# compiler to help other people. You must have misunderstood my post. 2)I have written work arounds for this problem, I just wanted to see how other people have handled this problem out of pure interest. We can all become better programmers if we want to. You must have misunderstood my post. 3) At no point was I arguing with him. Why would I argue with somebody who is clearly trying to help me?? You must have misunderstood my post. I would suggest that in the future you should try reading the post more closely to avoid misunderstandings like this. If you want, you can contact me anytime and I will help you with that. Kindest Regards

                George W Software Developer www.zsystems.ca

                1 Reply Last reply
                0
                • I IGeorgeI

                  This has been a problem of mine for years. It appears to be compiler specific which is why I put it in this section. Run this piece of code double test = 3.0/10.0; The result of test is .299999999999999 I know why this happens. I was just wondering how some of you have solved this problem. :-D Thanks for any input

                  George W Software Developer www.zsystems.ca

                  J Offline
                  J Offline
                  John R Shaw
                  wrote on last edited by
                  #8

                  First of all I want to state that I have read the other posts and would have ignored that one about C#. This is not a compiler specific problem, it is a hardware problem and I do not know any solution. Although there is an excellent book called “Numerical Recipes in …” (very old now) that probably addresses these issues. If Microsoft has introduced a way to solve it, via software, they have not been vocal about it. The only thing I can think of is rounding up when you see all those ‘9’s’.

                  INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                  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