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. How can i get the correct decimal value without truncation ?

How can i get the correct decimal value without truncation ?

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

    Hi! I m working on a c++ project in which I have two values one is 987654321123456789 and another is 1000000000000000000. My problem is that I m dividing first value by second value which is a decimal type value in result, when I do this I store the result in a double value after typecasting the above calculation by double but it give wrong result. The result is .98755432112346 which is wrong the exact result is .987554321123456789. The code which I use is as -. double dd = double(987554321123456789 /1000000000000000000) After calculation dd = .98755432112346 which is wrong because the actual result is .987554321123456789. So how I do this calculation so that I get the correct result please help me regarding this Thanks

    CPalliniC L M 4 Replies Last reply
    0
    • N nitin_pro

      Hi! I m working on a c++ project in which I have two values one is 987654321123456789 and another is 1000000000000000000. My problem is that I m dividing first value by second value which is a decimal type value in result, when I do this I store the result in a double value after typecasting the above calculation by double but it give wrong result. The result is .98755432112346 which is wrong the exact result is .987554321123456789. The code which I use is as -. double dd = double(987554321123456789 /1000000000000000000) After calculation dd = .98755432112346 which is wrong because the actual result is .987554321123456789. So how I do this calculation so that I get the correct result please help me regarding this Thanks

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      nitin_pro wrote:

      So how I do this calculation so that I get the correct result please help me regarding this

      You can't. Approximation it's the very nature of floating point numbers (possibly because memory locations can hold only a discrete set of values). See [^]. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

      In testa che avete, signor di Ceprano?

      N 1 Reply Last reply
      0
      • N nitin_pro

        Hi! I m working on a c++ project in which I have two values one is 987654321123456789 and another is 1000000000000000000. My problem is that I m dividing first value by second value which is a decimal type value in result, when I do this I store the result in a double value after typecasting the above calculation by double but it give wrong result. The result is .98755432112346 which is wrong the exact result is .987554321123456789. The code which I use is as -. double dd = double(987554321123456789 /1000000000000000000) After calculation dd = .98755432112346 which is wrong because the actual result is .987554321123456789. So how I do this calculation so that I get the correct result please help me regarding this Thanks

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        The FPU (x87) uses 80-bit fields for its operations for loading and storing back and forth from the x87 stack. However the Microsoft run-time library sets the default internal precision of the math coprocessor to 64 bits in Windows XP. This can be controlled by the function _controlfp which is documented here: http://msdn2.microsoft.com/en-us/library/e9b52ceh(VS.80).aspx[^] There are some documents where Microsoft claims the precision can be changed to the full 80 bits such as this document: http://support.microsoft.com/kb/q263213/[^] These documents conflict with some of the statements in this document: http://msdn2.microsoft.com/en-us/library/y0ybw9fy(VS.80).aspx[^] Such things are common in the MSDN. I personally believe its a documentation error or perhaps they are not explaining all of the details. I have never tried it nor tested it however. Try it and see. Best Wishes, -David Delaune

        1 Reply Last reply
        0
        • N nitin_pro

          Hi! I m working on a c++ project in which I have two values one is 987654321123456789 and another is 1000000000000000000. My problem is that I m dividing first value by second value which is a decimal type value in result, when I do this I store the result in a double value after typecasting the above calculation by double but it give wrong result. The result is .98755432112346 which is wrong the exact result is .987554321123456789. The code which I use is as -. double dd = double(987554321123456789 /1000000000000000000) After calculation dd = .98755432112346 which is wrong because the actual result is .987554321123456789. So how I do this calculation so that I get the correct result please help me regarding this Thanks

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          i forgot to mention the /Op compiler switch for older compilers which also modifies FPU results. It is documented here: http://msdn2.microsoft.com/en-us/library/aa278532(VS.60).aspx[^] I believe in modern compilers VS 8.0 and above that this was superceeded by the switch /fp to set floating point behavior. It is documented here: http://msdn2.microsoft.com/en-us/library/e7s85ffb.aspx[^] Best Wishes, -David Delaune

          1 Reply Last reply
          0
          • N nitin_pro

            Hi! I m working on a c++ project in which I have two values one is 987654321123456789 and another is 1000000000000000000. My problem is that I m dividing first value by second value which is a decimal type value in result, when I do this I store the result in a double value after typecasting the above calculation by double but it give wrong result. The result is .98755432112346 which is wrong the exact result is .987554321123456789. The code which I use is as -. double dd = double(987554321123456789 /1000000000000000000) After calculation dd = .98755432112346 which is wrong because the actual result is .987554321123456789. So how I do this calculation so that I get the correct result please help me regarding this Thanks

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

            And another link for you... :) [29.16] Why is floating point so inaccurate?[^]

            --Mike-- Visual C++ MVP :cool: LINKS~! CP SearchBar v3.0 | C++ Forum FAQ Dunder-Mifflin, this is Pam.

            1 Reply Last reply
            0
            • CPalliniC CPallini

              nitin_pro wrote:

              So how I do this calculation so that I get the correct result please help me regarding this

              You can't. Approximation it's the very nature of floating point numbers (possibly because memory locations can hold only a discrete set of values). See [^]. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

              N Offline
              N Offline
              nitin_pro
              wrote on last edited by
              #6

              Hi! Actually my problem is that when I perform double dd = double(987554321123456789 /1000000000000000000) this calculation the result is truncated up to 15 digit and it gives result dd = 0.98755432112346 but I want result completely means up to 18 digit may be it’s the limitation of Double data type that it can only store result up to 15 digit bt if it is tn I need some other data type which can store complete result yp to 18 digit. Pls help me to store complete result manse 0.987554321123456789 without truncation .

              CPalliniC 1 Reply Last reply
              0
              • N nitin_pro

                Hi! Actually my problem is that when I perform double dd = double(987554321123456789 /1000000000000000000) this calculation the result is truncated up to 15 digit and it gives result dd = 0.98755432112346 but I want result completely means up to 18 digit may be it’s the limitation of Double data type that it can only store result up to 15 digit bt if it is tn I need some other data type which can store complete result yp to 18 digit. Pls help me to store complete result manse 0.987554321123456789 without truncation .

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                Why don't you keep it inside long long assuming implicit multiplication by 10^18?

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                In testa che avete, signor di Ceprano?

                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