Fast and accurate double to string conversion.
61
stars
10
commits
D
primary language
Dec 14, 2023
updated
Minimalistic C / D implementation of Fabian Loitsch's Grisu-algorithm [pdf]. Grisu converts floating point numbers to an optimal decimal string representation without loss of precision.
int fpconv_dtoa(double fp, char dest[24]);
fp to dest and returns the number of written charactersfp is an IEEE 64-bit floating point numbervoid print(double d)
{
char buf[24 + 1]; /* reserve space for null terminator */
int str_len = fpconv_dtoa(d, buf);
buf[str_len] = '\0';
printf("%s", buf);
}
snprintf?Convert doubles faster to shortest strings without precision loss.
Average processing time on a mix of "long" and "short" doubles in nanoseconds:
short long
snprintf %g : 515 700
%0.18g : 989 1171
%f : 737 3047
fpconv_dtoa : 165 193
snprintf overhead : 71
Measured with gcc-4.8 -O3 and glibc 2.17.
D
59.9%
C
40.1%
Fast and accurate double to string conversion.
61
stars
10
commits
D
primary language
Dec 14, 2023
updated
Minimalistic C / D implementation of Fabian Loitsch's Grisu-algorithm [pdf]. Grisu converts floating point numbers to an optimal decimal string representation without loss of precision.
int fpconv_dtoa(double fp, char dest[24]);
fp to dest and returns the number of written charactersfp is an IEEE 64-bit floating point numbervoid print(double d)
{
char buf[24 + 1]; /* reserve space for null terminator */
int str_len = fpconv_dtoa(d, buf);
buf[str_len] = '\0';
printf("%s", buf);
}
snprintf?Convert doubles faster to shortest strings without precision loss.
Average processing time on a mix of "long" and "short" doubles in nanoseconds:
short long
snprintf %g : 515 700
%0.18g : 989 1171
%f : 737 3047
fpconv_dtoa : 165 193
snprintf overhead : 71
Measured with gcc-4.8 -O3 and glibc 2.17.
D
59.9%
C
40.1%