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. Rounding problem

Rounding problem

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelplearning
5 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.
  • W Offline
    W Offline
    wito
    wrote on last edited by
    #1

    Hi everybody, I'm a beginner and I'm writing somthing like this double *Interesse ,*Capitale, *TassoInteresse, *Intervallo, *Totale; *Interesse = *(Capitale) * (*TassoInteresse) * (*Intervallo) /36500; *Totale = *Capitale + *Interesse; usually this work good but with some value I have rounding problem like this: *Capitale = 111111; *TassoInteresse = 2.5; *Intervallo = 365; before converting to string; *Interesse = 2777.7750000000001 *Totale = 113888.77499999999 after conversion: 2777.78 113.888.77 How can I avoid this? Thanks in advance.

    M C B W 4 Replies Last reply
    0
    • W wito

      Hi everybody, I'm a beginner and I'm writing somthing like this double *Interesse ,*Capitale, *TassoInteresse, *Intervallo, *Totale; *Interesse = *(Capitale) * (*TassoInteresse) * (*Intervallo) /36500; *Totale = *Capitale + *Interesse; usually this work good but with some value I have rounding problem like this: *Capitale = 111111; *TassoInteresse = 2.5; *Intervallo = 365; before converting to string; *Interesse = 2777.7750000000001 *Totale = 113888.77499999999 after conversion: 2777.78 113.888.77 How can I avoid this? Thanks in advance.

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      This is the normal floating point behaviour in all/most computer languages. There are numerous ways to display floating point number rounded off to a fixed value, just google for them. ( for example : http://support.microsoft.com/default.aspx?scid=kb;en-us;196652 ) but, if you need to use those values, don't rely on the text display, always keep a "real" value, i.e. if your calculation result in 2777.7750000000001, don't use the rounded value, because you will loose precision in the long run.


      Maximilien Lincourt Your Head A Splode - Strong Bad

      1 Reply Last reply
      0
      • W wito

        Hi everybody, I'm a beginner and I'm writing somthing like this double *Interesse ,*Capitale, *TassoInteresse, *Intervallo, *Totale; *Interesse = *(Capitale) * (*TassoInteresse) * (*Intervallo) /36500; *Totale = *Capitale + *Interesse; usually this work good but with some value I have rounding problem like this: *Capitale = 111111; *TassoInteresse = 2.5; *Intervallo = 365; before converting to string; *Interesse = 2777.7750000000001 *Totale = 113888.77499999999 after conversion: 2777.78 113.888.77 How can I avoid this? Thanks in advance.

        C Offline
        C Offline
        ChrisLang
        wrote on last edited by
        #3

        I don't understand what you are asking. You have a pointer to a double and set its value and that works well, but then you are changing the value when you do the conversion and don't like the result? I'd use another variable to hold the result of the conversion then you don't change the value of the original double assignment. If you want to format the string result of the coversion then that is a different question. I'd suggest not using pointers to start with until you understand what is going on unless the exercise you are doing requires you to use pointers. I'd also suggest a text like C++ from the ground up to learn the basics. Chris

        1 Reply Last reply
        0
        • W wito

          Hi everybody, I'm a beginner and I'm writing somthing like this double *Interesse ,*Capitale, *TassoInteresse, *Intervallo, *Totale; *Interesse = *(Capitale) * (*TassoInteresse) * (*Intervallo) /36500; *Totale = *Capitale + *Interesse; usually this work good but with some value I have rounding problem like this: *Capitale = 111111; *TassoInteresse = 2.5; *Intervallo = 365; before converting to string; *Interesse = 2777.7750000000001 *Totale = 113888.77499999999 after conversion: 2777.78 113.888.77 How can I avoid this? Thanks in advance.

          B Offline
          B Offline
          Branislav
          wrote on last edited by
          #4

          Try with this: char sInteresse[30], sTotale[30]; sprintf(sInteresse, "%14.14f", Interesse); sprintf(sTotale, "%14.14f", Totale);

          1 Reply Last reply
          0
          • W wito

            Hi everybody, I'm a beginner and I'm writing somthing like this double *Interesse ,*Capitale, *TassoInteresse, *Intervallo, *Totale; *Interesse = *(Capitale) * (*TassoInteresse) * (*Intervallo) /36500; *Totale = *Capitale + *Interesse; usually this work good but with some value I have rounding problem like this: *Capitale = 111111; *TassoInteresse = 2.5; *Intervallo = 365; before converting to string; *Interesse = 2777.7750000000001 *Totale = 113888.77499999999 after conversion: 2777.78 113.888.77 How can I avoid this? Thanks in advance.

            W Offline
            W Offline
            wito
            wrote on last edited by
            #5

            I use _fcvt() to convert double to string . i'm not rounding double values. double values are in previous topic:) when converted the result is 111111 + 2777.78 = 113888.77 :confused: Thanks.

            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