D
Dew
Hi All
The problem is related to strtod()/atof()'s well known behaviour.
strtod()/atof() add some extra digits at the end of precision while
converting from string to double. Becasue floating-point variables
cannot represent all numbers precisely, I'm looking some way around to
solve it explicitly in the code itself so that I could get the value
property = 999999999998.90000000 for precison=8 instead of
property=999999999998.90002441
Size of float and double both is 8 bytes in my machine. and I'm using
xlC compiler on AIX plateform.
Any clue will help me immensely. Thanks.
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<wchar.h>
#include<math.h>
#include<float.h>
int main(int argc, char* argv[])
{
char property[1024];
int precison=0;
memset(property, '\0', sizeof(property));
strcpy(property, argv[1]);
precison = atoi(argv[2]);
cout <<"property: \"" << property << "\"" << endl;
//sscanf ( property, "%Lf", &dl);
//sprintf(PrpValue1, "%.*lf", precison,property);
double doubleVal=0.0;
if (strstr(property,"E+") != NULL || strstr(property,"E-") != NULL)
{
doubleVal = ( long double)strtod(property, (char **)NULL);
//doubleVal = atof(property);
printf("doubleVal:%.*lf\n",precison,doubleVal);
sprintf(property, "%.*lf", precison,doubleVal);
cout <<"E+/- roperty: " << property << endl;
}
cout <<"final property: " << property << endl;
free(property);
return 0;
}
Here's the o/p.
/a.out 9.999999999989E+11 8
property: "9.999999999989E+11"
doubleVal:999999999998.90002441
E+/- roperty: 999999999998.90002441
final property: 999999999998.90002441
The problem is related to strtod()/atof()'s well known behaviour.
strtod()/atof() add some extra digits at the end of precision while
converting from string to double. Becasue floating-point variables
cannot represent all numbers precisely, I'm looking some way around to
solve it explicitly in the code itself so that I could get the value
property = 999999999998.90000000 for precison=8 instead of
property=999999999998.90002441
Size of float and double both is 8 bytes in my machine. and I'm using
xlC compiler on AIX plateform.
Any clue will help me immensely. Thanks.
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<wchar.h>
#include<math.h>
#include<float.h>
int main(int argc, char* argv[])
{
char property[1024];
int precison=0;
memset(property, '\0', sizeof(property));
strcpy(property, argv[1]);
precison = atoi(argv[2]);
cout <<"property: \"" << property << "\"" << endl;
//sscanf ( property, "%Lf", &dl);
//sprintf(PrpValue1, "%.*lf", precison,property);
double doubleVal=0.0;
if (strstr(property,"E+") != NULL || strstr(property,"E-") != NULL)
{
doubleVal = ( long double)strtod(property, (char **)NULL);
//doubleVal = atof(property);
printf("doubleVal:%.*lf\n",precison,doubleVal);
sprintf(property, "%.*lf", precison,doubleVal);
cout <<"E+/- roperty: " << property << endl;
}
cout <<"final property: " << property << endl;
free(property);
return 0;
}
Here's the o/p.
/a.out 9.999999999989E+11 8
property: "9.999999999989E+11"
doubleVal:999999999998.90002441
E+/- roperty: 999999999998.90002441
final property: 999999999998.90002441