Technical discussions related to Analog Devices DSPs (including Blackfin, TigerSHARC, SHARC and ADSP-21xx DSPs).
Hello all
Below I have written a code in C.
Now the problem is
The value "2.60" copying to szData using a string function.
After copying the function atof (convert string to double) i am using. But the result what i
am getting in the variable dlData not the expected one.
I am getting : 2.6000000000000001. The 1 in the last value is the error one. How i can avoid
that.
#include "stdafx.h"
#include "NumberTest.h"
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <float.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
main()
{
double dlData;
char szData[25];
strcpy(szData," 2.60");
dlData = atof(szData);
printf("%lf",dlData);
}
Thanks in Advance
sathish
---------------------------------
Office firewalls, cyber cafes, college labs, don't allow you to download CHAT? Here's a
solution!
On Thu, 17 May 2007, sathish chandra wrote: > Hello all > > Below I have written a code in C. > > Now the problem is > > The value "2.60" copying to szData using a string function. > After copying the function atof (convert string to double) i am using. But the result what i am getting in the variable dlData not the expected one. > I am getting : 2.6000000000000001. The 1 in the last value is the error one. How i can avoid that. that's roundoff when going back. In other words, you convert string to double, then to see it you have go double back to string. The conversion going back has roundoff, there's no problem with the value in ram. You can check that by dumping the raw binary data and looking up the format in the manual and do the conversion by hand. Patience, persistence, truth, Dr. mike
you can do
printf("%.2f", dlData);
it will give you them same as your original string
sathish chandra wrote:
> Hello all
>
> Below I have written a code in C.
>
> Now the problem is
>
> The value "2.60" copying to *szData* using a string function.
> After copying the function *atof* (convert string to double) i am
> using. But the result what i am getting in the variable *dlData* not
> the expected one.
> I am getting : 2.6000000000000001. The 1 in the last value is the
> error one. How i can avoid that.
>
>
>
> #include "stdafx.h"
> #include "NumberTest.h"
> #include <stdlib.h>
> #include <math.h>
> #include <stdio.h>
> #include <float.h>
> #ifdef _DEBUG
> #define new DEBUG_NEW
> #endif
>
>
> main()
> {
>
> double dlData;
>
>
> char szData[25];
>
>
> strcpy(szData," 2.60");
>
>
> dlData = atof(szData);
> printf("%lf",dlData);
>
> }
>
>
>
> Thanks in Advance
>
> sathish
>
> ------------------------------------------------------------------------
> Office firewalls, cyber cafes, college labs, don't allow you to
> download CHAT? Here's a solution!
>
<http://us.rd.yahoo.com/mail/in/ymessenger/*http://in.messenger.yahoo.com/webmessengerpromo.
php>