All Projects → night-shift → fpconv

night-shift / fpconv

Licence: BSL-1.0 License
Fast and accurate double to string conversion.

Programming Languages

d
599 projects
c
50402 projects - #5 most used programming language
objective c
16641 projects - #2 most used programming language

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.

C Api

int fpconv_dtoa(double fp, char dest[24]);
  • Writes string representation of fp to dest and returns the number of written characters
  • The emitted string will never exceed 24 characters
  • Does not null terminate the string
  • Assumes fp is an IEEE 64-bit floating point number

Example usage

void 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);
}

Why not just use 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].