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. Calculating decimal places -

Calculating decimal places -

Scheduled Pinned Locked Moved C / C++ / MFC
helpperformance
16 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.
  • P ptr_Electron

    Hi Below code does some memory corruption due to which access violation happens at sub subsequent steps If I remove this block of code every thing is working fine. Unable to figure out the issue in the below code. Please help

    while ((int)tax % 10 != 0)
    {
    tax *= 10;
    decimals++;
    }

    decimalpoints = decimals-1

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

    This is not going to work as you might expect. For example, if tax has an initial value of 8.0, it may actually be stored in memory as 8.00000000001 or something similar. What exactly are you trying to accomplish? A better solution may be just an explanation away.

    "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

    "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

    P 2 Replies Last reply
    0
    • P ptr_Electron

      Hi Below code does some memory corruption due to which access violation happens at sub subsequent steps If I remove this block of code every thing is working fine. Unable to figure out the issue in the below code. Please help

      while ((int)tax % 10 != 0)
      {
      tax *= 10;
      decimals++;
      }

      decimalpoints = decimals-1

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

      Such code looks flawed. What is the type of the tax variable?

      In testa che avete, signor di Ceprano?

      P 1 Reply Last reply
      0
      • D David Crow

        This is not going to work as you might expect. For example, if tax has an initial value of 8.0, it may actually be stored in memory as 8.00000000001 or something similar. What exactly are you trying to accomplish? A better solution may be just an explanation away.

        "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

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        P Offline
        P Offline
        ptr_Electron
        wrote on last edited by
        #6

        I want to find the number of decimal point. for example 8.995 is the tax amount. I want to count number of digits after decimal point; in this case I want to get 3

        1 Reply Last reply
        0
        • CPalliniC CPallini

          Such code looks flawed. What is the type of the tax variable?

          P Offline
          P Offline
          ptr_Electron
          wrote on last edited by
          #7

          Tax variable is of type Double

          CPalliniC 1 Reply Last reply
          0
          • D David Crow

            This is not going to work as you might expect. For example, if tax has an initial value of 8.0, it may actually be stored in memory as 8.00000000001 or something similar. What exactly are you trying to accomplish? A better solution may be just an explanation away.

            "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

            "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

            P Offline
            P Offline
            ptr_Electron
            wrote on last edited by
            #8

            I want to find the number of decimal point. for example 8.995 is the tax amount. I want to count number of digits after decimal point; in this case I want to get 3

            D 1 Reply Last reply
            0
            • P ptr_Electron

              I want to find the number of decimal point. for example 8.995 is the tax amount. I want to count number of digits after decimal point; in this case I want to get 3

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

              ptr_Electron wrote:

              for example 8.995 is the tax amount.

              Which could be stored in memory as 8.9949999999999992 or something similar. Then you would get 16 (or maybe 15, I forget) as an answer.

              "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

              "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

              P 1 Reply Last reply
              0
              • P ptr_Electron

                Tax variable is of type Double

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

                Then your code is apparently flawed.

                In testa che avete, signor di Ceprano?

                1 Reply Last reply
                0
                • D David Crow

                  ptr_Electron wrote:

                  for example 8.995 is the tax amount.

                  Which could be stored in memory as 8.9949999999999992 or something similar. Then you would get 16 (or maybe 15, I forget) as an answer.

                  "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

                  "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                  P Offline
                  P Offline
                  ptr_Electron
                  wrote on last edited by
                  #11

                  May I know the correct approach please.

                  D 1 Reply Last reply
                  0
                  • P ptr_Electron

                    May I know the correct approach please.

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

                    It's still unclear as to what you are trying to accomplish. Once you have the number of digits in the mantissa, then what? Again, if you would provide details for your end-game, we may be able to offer a better solution. Manipulating floating point numbers on a binary computer is not a trivial task.

                    "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

                    "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                    P 1 Reply Last reply
                    0
                    • D David Crow

                      It's still unclear as to what you are trying to accomplish. Once you have the number of digits in the mantissa, then what? Again, if you would provide details for your end-game, we may be able to offer a better solution. Manipulating floating point numbers on a binary computer is not a trivial task.

                      "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

                      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                      P Offline
                      P Offline
                      ptr_Electron
                      wrote on last edited by
                      #13

                      Thanks for response. a given decimal number. I would the count of decimal points. For example 6.67 -? I need 2 6.9876 I need 4 1.67890 I need 5 Count of digits after decimal point

                      D L 2 Replies Last reply
                      0
                      • P ptr_Electron

                        Thanks for response. a given decimal number. I would the count of decimal points. For example 6.67 -? I need 2 6.9876 I need 4 1.67890 I need 5 Count of digits after decimal point

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

                        ptr_Electron wrote:

                        6.67 -? I need 2 6.9876 I need 4 1.67890 I need 5

                        Which are stored in memory as:

                        6.6699999999999999
                        6.9875999999999996
                        1.6789000000000001

                        or something very similar. Notice all the mantissas are the same length? Is this just a trivial exercise for you, or are you ultimately planning on doing something with the result?

                        "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

                        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                        1 Reply Last reply
                        0
                        • P ptr_Electron

                          Both are not pointers. one issue I found with this is the loop iterates for 19 times for the value of tax = 8.995. by end of the loop decimal = 19. I except decimal = 3 by end of the loop

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

                          You are unlikely to get a valid answer doing it like that. You should use something like sprintf to convert the double value to a string and set the actual number of decimal places that you want in the format string.

                          1 Reply Last reply
                          0
                          • P ptr_Electron

                            Thanks for response. a given decimal number. I would the count of decimal points. For example 6.67 -? I need 2 6.9876 I need 4 1.67890 I need 5 Count of digits after decimal point

                            L Offline
                            L Offline
                            leon de boer
                            wrote on last edited by
                            #16

                            As per what David has explained all doubles are actually stored subtly different to what you think because they round. I just want to extend why they round. They round because the computer works in base 2 (0 and 1's) and you are working in decimal base 10 (0,1,2,..9) 10 does not work as a power of 2 you can go either side 2x2x2=8 OR 2x2x2x2=16 so any base 10 decimal fraction when written in base 2 will likely round Double-precision floating-point format - Wikipedia[^] Assuming you are on a standard Microsoft compiler many will be rounded at 52 bits long and we have no way to know what length you actually typed in. So usually when writing doubles to screen you fix the decimal places Here is how the standard print function does it the %.3f means take float write to 3 decimal places

                            printf("Double value: %.3f\n", 3.1234543747321475);

                            I have made a randomly long value but if you execute it only puts out 3 decimal places. There are many conversion routines in C/C++ to convert them in fixed decimal places to screen, buffers etc. So generally you fix the length at display or while the number is in string format, you can't work the problem in reverse the moment it stores the original length is lost. So basically once stored there is no way to count the decimal places .. you can't do what you asked. Lastly should add this is nothing to do with C, any language that stores numbers as doubles behaves that way.

                            In vino veritas

                            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