diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2010-11-17 12:58:53 -0800 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2010-11-17 12:59:32 -0800 |
commit | 0c7219c6481808cbf45495502d55bac81f5c1112 (patch) | |
tree | 2879aed7a53e05869d70dcf544971cb1912d2b1d /lib/ftoastr.c | |
parent | cc98e1ec101be0b8d260025b5e40bfec7f4b1ac5 (diff) | |
download | gnulib-0c7219c6481808cbf45495502d55bac81f5c1112.tar.gz |
ftoastr: new module, for lossless conversion of floats to short strings
* lib/ftoastr.h, lib/ftoastr.c, lib/dtoastr.c, lib/ldtoastr.c:
* modules/ftoastr: New files.
Diffstat (limited to 'lib/ftoastr.c')
-rw-r--r-- | lib/ftoastr.c | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/lib/ftoastr.c b/lib/ftoastr.c new file mode 100644 index 0000000000..25e0705e42 --- /dev/null +++ b/lib/ftoastr.c @@ -0,0 +1,89 @@ +/* floating point to accurate string + + Copyright (C) 2010 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +/* Written by Paul Eggert. */ + +#include "ftoastr.h" + +#include "intprops.h" +#include <float.h> +#include <stdio.h> +#include <stdlib.h> + +#if LENGTH == 3 +# define FLOAT long double +# define FLOAT_DIG LDBL_DIG +# define FLOAT_MIN LDBL_MIN +# define FLOAT_PREC_BOUND _GL_LDBL_PREC_BOUND +# define FTOASTR ldtoastr +# define STRTOF strtold +#elif LENGTH == 2 +# define FLOAT double +# define FLOAT_DIG DBL_DIG +# define FLOAT_MIN DBL_MIN +# define FLOAT_PREC_BOUND _GL_DBL_PREC_BOUND +# define FTOASTR dtoastr +# define STRTOF strtod +#else +# define LENGTH 1 +# define FLOAT float +# define FLOAT_DIG FLT_DIG +# define FLOAT_MIN FLT_MIN +# define FLOAT_PREC_BOUND _GL_FLT_PREC_BOUND +# define FTOASTR ftoastr +# define STRTOF strtof +#endif + +int +FTOASTR (char *buf, size_t bufsize, int flags, int width, FLOAT x) +{ + /* The following method is simple but slow. + For ideas about speeding things up, please see: + + Florian Loitsch, Printing floating-point numbers quickly and accurately + with integers. ACM SIGPLAN notices 46, 6 (June 2010), 233-243 + <http://dx.doi.org/10.1145/1809028.1806623>. */ + + char format[sizeof "%-+ 0*.*Lg"]; + FLOAT abs_x = x < 0 ? -x : x; + int prec; + + char *p = format; + *p++ = '%'; + + /* Support flags that generate output parsable by strtof. */ + *p = '-'; p += (flags & FTOASTR_LEFT_JUSTIFY ) != 0; + *p = '+'; p += (flags & FTOASTR_ALWAYS_SIGNED ) != 0; + *p = ' '; p += (flags & FTOASTR_SPACE_POSITIVE) != 0; + *p = '0'; p += (flags & FTOASTR_ZERO_PAD ) != 0; + + *p++ = '*'; + *p++ = '.'; + *p++ = '*'; + *p = 'L'; p += 2 < LENGTH; + *p++ = flags & FTOASTR_UPPER_E ? 'G' : 'g'; + *p = '\0'; + + for (prec = abs_x < FLOAT_MIN ? 1 : FLOAT_DIG; ; prec++) + { + int n = snprintf (buf, bufsize, format, width, prec, x); + if (n < 0 + || FLOAT_PREC_BOUND <= prec + || (n < bufsize && STRTOF (buf, NULL) == x)) + return n; + } +} |