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