T
tjay
Hi. I wrote some code using sprintf and atof to store a double as a
string of fixed length and to convert it back to a double variable. The
string is stored in a char buffer global variable. I'm afraid it might
contain bugs though.
If I serialize a double, I get a string of the format
"-1.0000000000000000e+212". This string gets stored in the buffer.
Then, to convert it back into a double, I pass it to the atof function.
The problem I'm afraid of, is when i serialize
"-1.0000000000000000e+123", then serialize "-1.0000000000000000e+12",
the last character in the buffer should still contain its old value,
'3'. Then when I pass it to atof, it will return
"-1.0000000000000000e+123" (the 3 at the end is not supposed to be
there).
Surprisingly, this doesn't happen when I test my code, so I was
wondering if it was due to glibc doing some magical error checking, or
there's some subtle semantics with sprintf and atof the I don't know
about.
/* Start of code ======================================= */
#include <math.h>
/* 1 sign + 1 decimal + 1 decimal point + 16 fractional
* + 5 exponent = 24 */
char buffer[24];
int main () {
/* "-1.0000000000000000+e307" */
sprintf (buffer, "% .16e", -1 * pow (1, 307));
/* "-1.0000000000000000+e30" */
sprintf (buffer, "% .16e", -1 * pow (1, 30));
/* will this give me "-1.0000000000000000+e307" or
* "-1.0000000000000000+e30"? */
printf (atof (buffer));
return 0;
}
/* End of code ======================================== */
Also, Is there a way to force the exponent to always be 3 characters
long?
TJ
string of fixed length and to convert it back to a double variable. The
string is stored in a char buffer global variable. I'm afraid it might
contain bugs though.
If I serialize a double, I get a string of the format
"-1.0000000000000000e+212". This string gets stored in the buffer.
Then, to convert it back into a double, I pass it to the atof function.
The problem I'm afraid of, is when i serialize
"-1.0000000000000000e+123", then serialize "-1.0000000000000000e+12",
the last character in the buffer should still contain its old value,
'3'. Then when I pass it to atof, it will return
"-1.0000000000000000e+123" (the 3 at the end is not supposed to be
there).
Surprisingly, this doesn't happen when I test my code, so I was
wondering if it was due to glibc doing some magical error checking, or
there's some subtle semantics with sprintf and atof the I don't know
about.
/* Start of code ======================================= */
#include <math.h>
/* 1 sign + 1 decimal + 1 decimal point + 16 fractional
* + 5 exponent = 24 */
char buffer[24];
int main () {
/* "-1.0000000000000000+e307" */
sprintf (buffer, "% .16e", -1 * pow (1, 307));
/* "-1.0000000000000000+e30" */
sprintf (buffer, "% .16e", -1 * pow (1, 30));
/* will this give me "-1.0000000000000000+e307" or
* "-1.0000000000000000+e30"? */
printf (atof (buffer));
return 0;
}
/* End of code ======================================== */
Also, Is there a way to force the exponent to always be 3 characters
long?
TJ