double and float problem
-
Hi. my problem:
double a=98; double b=100; double c=98/100; //then c=0.9799999999998 instead 0.98
and why in delphi, vb, etc. i got the 0.98 but not in c++. I even tried the /Op compiler setting and nothing. I have readed the microsoft workaround of adding 1e-10 but does not work. What should i do?. Thanks in advance. -
Hi. my problem:
double a=98; double b=100; double c=98/100; //then c=0.9799999999998 instead 0.98
and why in delphi, vb, etc. i got the 0.98 but not in c++. I even tried the /Op compiler setting and nothing. I have readed the microsoft workaround of adding 1e-10 but does not work. What should i do?. Thanks in advance.There is a very good chance that Delphi and VB are getting the exact same result but due to how they are displaying the number you as seeing a different result. Floating point math is not exact. Tim Smith I'm going to patent thought. I have yet to see any prior art.
-
Hi. my problem:
double a=98; double b=100; double c=98/100; //then c=0.9799999999998 instead 0.98
and why in delphi, vb, etc. i got the 0.98 but not in c++. I even tried the /Op compiler setting and nothing. I have readed the microsoft workaround of adding 1e-10 but does not work. What should i do?. Thanks in advance.double c=98/100; would give c = 0.0 as 98/100 is integer arithmetic, it is then case to double! ;) OK, double c=(a/b) + 0.0000000001; will give c = 0.98 NOTE: This work around only makes the answer correct to 10dps. If you change everything to float you will probably get a different answer. These calculations all depend on the format of the floating point number and its precision. Ant. I'm hard, yet soft.
I'm coloured, yet clear.
I'm fuity and sweet.
I'm jelly, what am I? Muse on it further, I shall return! - David Williams (Little Britain) -
Hi. my problem:
double a=98; double b=100; double c=98/100; //then c=0.9799999999998 instead 0.98
and why in delphi, vb, etc. i got the 0.98 but not in c++. I even tried the /Op compiler setting and nothing. I have readed the microsoft workaround of adding 1e-10 but does not work. What should i do?. Thanks in advance.How a
double
(orfloat
) is stored in memory and how it is displayed are two different things. http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html http://www.fact-index.com/f/fl/floating\_point\_1.html (see Problems with floating-point) http://www.hal-pc.org/~clyndes/computer-arithmetic/floats.html
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
-
How a
double
(orfloat
) is stored in memory and how it is displayed are two different things. http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html http://www.fact-index.com/f/fl/floating\_point\_1.html (see Problems with floating-point) http://www.hal-pc.org/~clyndes/computer-arithmetic/floats.html
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen