Click here to Skip to main content
15,884,043 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Im working on piece of code where the a calculation needs to be done, using the values entered by the user.

But strangely, the values are being approximated. For example if the user enters 28 and 32.8, the difference which should be 12.8 is calculated as 12.7777778.. But the kind of data I'm dealing with, needs precise values.

I have declared the variable as double.
Any suggestions????
Posted
Updated 28-Mar-12 21:51pm
v2
Comments
Sergey Alexandrovich Kryukov 28-Mar-12 11:05am    
Do you think that 100% accurate representation of real number is theoretically possible? It means that you don't know what is a real number...
--SA
Sumal.V 28-Mar-12 11:12am    
Haha I do know that u cannot get 100% accuracy in calculations. But atleast the value entered by the user can match up! This will stop the accumulation of errors. And I work on latitudes and longitudes and slight change in seconds leads to many metres of difference!
Sergey Alexandrovich Kryukov 28-Mar-12 11:14am    
Don't mix up real accuracy with string representation.
--SA
Richard MacCutchan 28-Mar-12 12:02pm    
No it won't - except occasionally by accident. If you want precision use doubles; if you want accuracy use integers. Whether you like it or not, that is the only way in digital computers.
Stefan_Lang 28-Mar-12 11:08am    
Hm, there used to be a series of Pentium CPUs with that kind of odd behaviour...
;-)

Ok, assuming it's not a CPU issue, it would be helpful to see both the code that reads the data and the code you use to display it. Without that it's really hard to tell.

Please see my comment. Did you ever heard that, for example, some infinite subset of the set of real numbers are transcendental numbers? The examples are π and e. Do you know that any representations of each of such numbers in the form of the string contains infinite number of digits. Suppose you have any arbitrary text of any finite length (say, Romeo and Juliette); code it in the form of, say, a string of decimal digits in any way. The fact is: in the decimal representation of, say π, one can find exact same sub-sequence. This is mathematically proven and well-know fact. Do you feel surprised?

This is a reasonable accuracy. Please see:
http://en.wikipedia.org/wiki/Double-precision_floating-point_format[^].

—SA
 
Share this answer
 
Comments
Sumal.V 28-Mar-12 12:04pm    
thanks
Sergey Alexandrovich Kryukov 28-Mar-12 17:04pm    
You are welcome.
If you agree that this makes sense, please consider accepting this answer formally (green button) -- thanks.
--SA
Espen Harlinn 28-Mar-12 15:58pm    
5'ed!
Sergey Alexandrovich Kryukov 28-Mar-12 17:04pm    
Thank you, Espen.
--SA
RaisKazi 28-Mar-12 20:45pm    
My 5.
Try using the GNU Multiple Precision Arithmetic Library [^].

The precision of GMP is only limited by available memory.

Best regards
Espen Harlinn
 
Share this answer
 
Comments
RaisKazi 28-Mar-12 20:46pm    
My 5.
Espen Harlinn 5-Apr-12 5:10am    
Thank you RaisKazi :-D
Abhinav S 5-Apr-12 4:56am    
My 5.
Espen Harlinn 5-Apr-12 5:11am    
Thank you Abhinav :-D
I would not worry about precision too much for your application. If you are expressing latitude and longitude with C++ data type double on a normal Intel machine you will have at least 15 decimal digits of precision. An arc second represented as radian is 5 * 10**-6. So you have a precision of about 10**-9 arc seconds, each of which corresponds to 30m on the surface. So your results are precise to about 10**-7 meters, which is a tenth of a micrometer. That sounds like enough for most applications.

Another question is how to avoid all those 9s in results. 89.999999999 degrees latitude simply looks ugly. The secret to success here is rounding. When using printf, it will do the rounding for you if you specify a certain number of decimals, for example:

printf ("%.6f", 89.9999999999999);

will yield
90.000000


Now, if you convert your latitudes and longitudes to more human readable formats, like N53 34'47" then pay attention to implement the correct rounding mechanisms.
 
Share this answer
 
v2
Comments
[no name] 28-Mar-12 19:58pm    
Spot on - a simple dimensional analysis alleviates any concern.
RaisKazi 28-Mar-12 20:48pm    
My 5.
Sumal.V 29-Mar-12 5:05am    
Okay but since I need to use that value in further calculations, I declared a double variable.

double diff = 2011.8 - 1989; // diff takes a val of 22.777778 instead of 22.8
double val = printf("%.4f", diff); // but getting some strange values here!!!
nv3 29-Mar-12 7:16am    
diff is not equal to 22.8 because the value of 22.8 cannot precisely be represented as floating point value. This is due to the fact that the mantissa is always stored in base-2 and not in base-10. Hence there will always remain a tiny error in the range of 10**-15.

printf does not return the rounded value, but just outputs the rounded result to stdout. The return value of printf is int and is the number of characters written.

The point I wanted to make is: Calculate in double and then do the rounding when showing a value in the UI or on a printout.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900