2011-01-09 01:20:28 +00:00
|
|
|
/* floating point to accurate string
|
|
|
|
|
2024-01-02 01:47:10 +00:00
|
|
|
Copyright (C) 2010-2024 Free Software Foundation, Inc.
|
2011-01-09 01:20:28 +00:00
|
|
|
|
|
|
|
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
|
2022-02-23 19:11:52 +00:00
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
2011-01-09 01:20:28 +00:00
|
|
|
(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
|
2017-09-13 09:07:03 +00:00
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
2011-01-09 01:20:28 +00:00
|
|
|
|
|
|
|
/* Written by Paul Eggert. */
|
|
|
|
|
|
|
|
/* This code can misbehave on some buggy or older platforms, when
|
|
|
|
operating on arguments on floating types other than 'double', or
|
|
|
|
when given unusual combinations of options. Gnulib's
|
|
|
|
snprintf-posix module works around many of these problems.
|
|
|
|
|
|
|
|
This code relies on sprintf, strtod, etc. operating accurately;
|
|
|
|
otherwise, the resulting strings could be inaccurate or too long. */
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "ftoastr.h"
|
|
|
|
|
|
|
|
#include <float.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
Update from Gnulib
This incorporates:
2020-06-27 getloadavg: don’t depend on fopen-gnu
2020-06-25 c-dtoastr, c-ldtoastr: new modules
2020-06-01 getloadavg: fix double-increment bug
2020-06-01 tempname: use getrandom, not getentropy
2020-05-31 tempname: merge from glibc and coreutils
2020-05-31 getentropy: work around a macOS and Solaris problem
2020-05-31 fnmatch: merge from glibc
2020-05-30 unistd: remove conflicting declaration of getrandom
2020-05-30 don't assume that UNICODE is not defined
2020-05-29 fix compilation error on native Windows
2020-05-28 avoid dynamic loading of Windows API functions when possible
2020-05-28 at-internal: make more robust in multithreaded applications
2020-05-28 getloadavg: make more robust in multithreaded applications
2020-05-27 getloadavg: make more robust in multithreaded applications
2020-05-26 count-one-bits: fix MSVC specific code
2020-05-25 getentropy, getrandom: new modules
2020-05-24 open, openat: really support O_CLOEXEC
2020-05-23 verify: document ‘assume’ better
2020-05-21 regex: configure better with "clang -fsanitize=leak"
2020-05-21 memmem: configure better with "clang -fsanitize=undefined"
2020-05-19 ftoastr: fix ifndef typo
* build-aux/config.guess, build-aux/config.sub, doc/misc/texinfo.tex:
* lib/count-one-bits.h, lib/ftoastr.c, lib/ftoastr.h:
* lib/getloadavg.c, lib/gettimeofday.c, lib/libc-config.h:
* lib/open.c, lib/openat-proc.c, lib/tempname.c, lib/tempname.h:
* lib/unistd.in.h, lib/verify.h, m4/memmem.m4, m4/regex.m4:
* m4/unistd_h.m4:
Update from Gnulib.
* lib/getrandom.c, lib/sys_random.in.h:
* m4/getrandom.m4, m4/sys_random_h.m4:
New files, copied from Gnulib.
* lib/gnulib.mk.in, m4/gnulib-comp.m4: Regenerate.
2020-06-27 17:00:17 +00:00
|
|
|
#ifdef C_LOCALE
|
|
|
|
# include "c-snprintf.h"
|
|
|
|
# include "c-strtod.h"
|
|
|
|
# define PREFIX(name) c_ ## name
|
|
|
|
#else
|
|
|
|
# define PREFIX(name) name
|
|
|
|
#endif
|
|
|
|
|
2011-01-09 01:20:28 +00:00
|
|
|
#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
|
Update from Gnulib
This incorporates:
2020-06-27 getloadavg: don’t depend on fopen-gnu
2020-06-25 c-dtoastr, c-ldtoastr: new modules
2020-06-01 getloadavg: fix double-increment bug
2020-06-01 tempname: use getrandom, not getentropy
2020-05-31 tempname: merge from glibc and coreutils
2020-05-31 getentropy: work around a macOS and Solaris problem
2020-05-31 fnmatch: merge from glibc
2020-05-30 unistd: remove conflicting declaration of getrandom
2020-05-30 don't assume that UNICODE is not defined
2020-05-29 fix compilation error on native Windows
2020-05-28 avoid dynamic loading of Windows API functions when possible
2020-05-28 at-internal: make more robust in multithreaded applications
2020-05-28 getloadavg: make more robust in multithreaded applications
2020-05-27 getloadavg: make more robust in multithreaded applications
2020-05-26 count-one-bits: fix MSVC specific code
2020-05-25 getentropy, getrandom: new modules
2020-05-24 open, openat: really support O_CLOEXEC
2020-05-23 verify: document ‘assume’ better
2020-05-21 regex: configure better with "clang -fsanitize=leak"
2020-05-21 memmem: configure better with "clang -fsanitize=undefined"
2020-05-19 ftoastr: fix ifndef typo
* build-aux/config.guess, build-aux/config.sub, doc/misc/texinfo.tex:
* lib/count-one-bits.h, lib/ftoastr.c, lib/ftoastr.h:
* lib/getloadavg.c, lib/gettimeofday.c, lib/libc-config.h:
* lib/open.c, lib/openat-proc.c, lib/tempname.c, lib/tempname.h:
* lib/unistd.in.h, lib/verify.h, m4/memmem.m4, m4/regex.m4:
* m4/unistd_h.m4:
Update from Gnulib.
* lib/getrandom.c, lib/sys_random.in.h:
* m4/getrandom.m4, m4/sys_random_h.m4:
New files, copied from Gnulib.
* lib/gnulib.mk.in, m4/gnulib-comp.m4: Regenerate.
2020-06-27 17:00:17 +00:00
|
|
|
# define FTOASTR PREFIX (ldtoastr)
|
2017-03-03 16:45:10 +00:00
|
|
|
# define PROMOTED_FLOAT long double
|
Update from Gnulib
This incorporates:
2020-06-27 getloadavg: don’t depend on fopen-gnu
2020-06-25 c-dtoastr, c-ldtoastr: new modules
2020-06-01 getloadavg: fix double-increment bug
2020-06-01 tempname: use getrandom, not getentropy
2020-05-31 tempname: merge from glibc and coreutils
2020-05-31 getentropy: work around a macOS and Solaris problem
2020-05-31 fnmatch: merge from glibc
2020-05-30 unistd: remove conflicting declaration of getrandom
2020-05-30 don't assume that UNICODE is not defined
2020-05-29 fix compilation error on native Windows
2020-05-28 avoid dynamic loading of Windows API functions when possible
2020-05-28 at-internal: make more robust in multithreaded applications
2020-05-28 getloadavg: make more robust in multithreaded applications
2020-05-27 getloadavg: make more robust in multithreaded applications
2020-05-26 count-one-bits: fix MSVC specific code
2020-05-25 getentropy, getrandom: new modules
2020-05-24 open, openat: really support O_CLOEXEC
2020-05-23 verify: document ‘assume’ better
2020-05-21 regex: configure better with "clang -fsanitize=leak"
2020-05-21 memmem: configure better with "clang -fsanitize=undefined"
2020-05-19 ftoastr: fix ifndef typo
* build-aux/config.guess, build-aux/config.sub, doc/misc/texinfo.tex:
* lib/count-one-bits.h, lib/ftoastr.c, lib/ftoastr.h:
* lib/getloadavg.c, lib/gettimeofday.c, lib/libc-config.h:
* lib/open.c, lib/openat-proc.c, lib/tempname.c, lib/tempname.h:
* lib/unistd.in.h, lib/verify.h, m4/memmem.m4, m4/regex.m4:
* m4/unistd_h.m4:
Update from Gnulib.
* lib/getrandom.c, lib/sys_random.in.h:
* m4/getrandom.m4, m4/sys_random_h.m4:
New files, copied from Gnulib.
* lib/gnulib.mk.in, m4/gnulib-comp.m4: Regenerate.
2020-06-27 17:00:17 +00:00
|
|
|
# define STRTOF PREFIX (strtold)
|
2011-01-09 01:20:28 +00:00
|
|
|
#elif LENGTH == 2
|
|
|
|
# define FLOAT double
|
|
|
|
# define FLOAT_DIG DBL_DIG
|
|
|
|
# define FLOAT_MIN DBL_MIN
|
|
|
|
# define FLOAT_PREC_BOUND _GL_DBL_PREC_BOUND
|
Update from Gnulib
This incorporates:
2020-06-27 getloadavg: don’t depend on fopen-gnu
2020-06-25 c-dtoastr, c-ldtoastr: new modules
2020-06-01 getloadavg: fix double-increment bug
2020-06-01 tempname: use getrandom, not getentropy
2020-05-31 tempname: merge from glibc and coreutils
2020-05-31 getentropy: work around a macOS and Solaris problem
2020-05-31 fnmatch: merge from glibc
2020-05-30 unistd: remove conflicting declaration of getrandom
2020-05-30 don't assume that UNICODE is not defined
2020-05-29 fix compilation error on native Windows
2020-05-28 avoid dynamic loading of Windows API functions when possible
2020-05-28 at-internal: make more robust in multithreaded applications
2020-05-28 getloadavg: make more robust in multithreaded applications
2020-05-27 getloadavg: make more robust in multithreaded applications
2020-05-26 count-one-bits: fix MSVC specific code
2020-05-25 getentropy, getrandom: new modules
2020-05-24 open, openat: really support O_CLOEXEC
2020-05-23 verify: document ‘assume’ better
2020-05-21 regex: configure better with "clang -fsanitize=leak"
2020-05-21 memmem: configure better with "clang -fsanitize=undefined"
2020-05-19 ftoastr: fix ifndef typo
* build-aux/config.guess, build-aux/config.sub, doc/misc/texinfo.tex:
* lib/count-one-bits.h, lib/ftoastr.c, lib/ftoastr.h:
* lib/getloadavg.c, lib/gettimeofday.c, lib/libc-config.h:
* lib/open.c, lib/openat-proc.c, lib/tempname.c, lib/tempname.h:
* lib/unistd.in.h, lib/verify.h, m4/memmem.m4, m4/regex.m4:
* m4/unistd_h.m4:
Update from Gnulib.
* lib/getrandom.c, lib/sys_random.in.h:
* m4/getrandom.m4, m4/sys_random_h.m4:
New files, copied from Gnulib.
* lib/gnulib.mk.in, m4/gnulib-comp.m4: Regenerate.
2020-06-27 17:00:17 +00:00
|
|
|
# define FTOASTR PREFIX (dtoastr)
|
2017-03-03 16:45:10 +00:00
|
|
|
# define PROMOTED_FLOAT double
|
2011-01-09 01:20:28 +00:00
|
|
|
#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
|
Update from Gnulib
This incorporates:
2020-06-27 getloadavg: don’t depend on fopen-gnu
2020-06-25 c-dtoastr, c-ldtoastr: new modules
2020-06-01 getloadavg: fix double-increment bug
2020-06-01 tempname: use getrandom, not getentropy
2020-05-31 tempname: merge from glibc and coreutils
2020-05-31 getentropy: work around a macOS and Solaris problem
2020-05-31 fnmatch: merge from glibc
2020-05-30 unistd: remove conflicting declaration of getrandom
2020-05-30 don't assume that UNICODE is not defined
2020-05-29 fix compilation error on native Windows
2020-05-28 avoid dynamic loading of Windows API functions when possible
2020-05-28 at-internal: make more robust in multithreaded applications
2020-05-28 getloadavg: make more robust in multithreaded applications
2020-05-27 getloadavg: make more robust in multithreaded applications
2020-05-26 count-one-bits: fix MSVC specific code
2020-05-25 getentropy, getrandom: new modules
2020-05-24 open, openat: really support O_CLOEXEC
2020-05-23 verify: document ‘assume’ better
2020-05-21 regex: configure better with "clang -fsanitize=leak"
2020-05-21 memmem: configure better with "clang -fsanitize=undefined"
2020-05-19 ftoastr: fix ifndef typo
* build-aux/config.guess, build-aux/config.sub, doc/misc/texinfo.tex:
* lib/count-one-bits.h, lib/ftoastr.c, lib/ftoastr.h:
* lib/getloadavg.c, lib/gettimeofday.c, lib/libc-config.h:
* lib/open.c, lib/openat-proc.c, lib/tempname.c, lib/tempname.h:
* lib/unistd.in.h, lib/verify.h, m4/memmem.m4, m4/regex.m4:
* m4/unistd_h.m4:
Update from Gnulib.
* lib/getrandom.c, lib/sys_random.in.h:
* m4/getrandom.m4, m4/sys_random_h.m4:
New files, copied from Gnulib.
* lib/gnulib.mk.in, m4/gnulib-comp.m4: Regenerate.
2020-06-27 17:00:17 +00:00
|
|
|
# define FTOASTR PREFIX (ftoastr)
|
2017-03-03 16:45:10 +00:00
|
|
|
# define PROMOTED_FLOAT double
|
2011-06-15 18:50:18 +00:00
|
|
|
# if HAVE_STRTOF
|
|
|
|
# define STRTOF strtof
|
|
|
|
# endif
|
2011-01-09 01:20:28 +00:00
|
|
|
#endif
|
|
|
|
|
2019-02-02 21:13:45 +00:00
|
|
|
/* On pre-C99 hosts, approximate strtof with strtod. This
|
2011-01-09 01:20:28 +00:00
|
|
|
may generate one or two extra digits, but that's better than not
|
2011-06-15 18:50:18 +00:00
|
|
|
working at all. */
|
|
|
|
#ifndef STRTOF
|
Update from Gnulib
This incorporates:
2020-06-27 getloadavg: don’t depend on fopen-gnu
2020-06-25 c-dtoastr, c-ldtoastr: new modules
2020-06-01 getloadavg: fix double-increment bug
2020-06-01 tempname: use getrandom, not getentropy
2020-05-31 tempname: merge from glibc and coreutils
2020-05-31 getentropy: work around a macOS and Solaris problem
2020-05-31 fnmatch: merge from glibc
2020-05-30 unistd: remove conflicting declaration of getrandom
2020-05-30 don't assume that UNICODE is not defined
2020-05-29 fix compilation error on native Windows
2020-05-28 avoid dynamic loading of Windows API functions when possible
2020-05-28 at-internal: make more robust in multithreaded applications
2020-05-28 getloadavg: make more robust in multithreaded applications
2020-05-27 getloadavg: make more robust in multithreaded applications
2020-05-26 count-one-bits: fix MSVC specific code
2020-05-25 getentropy, getrandom: new modules
2020-05-24 open, openat: really support O_CLOEXEC
2020-05-23 verify: document ‘assume’ better
2020-05-21 regex: configure better with "clang -fsanitize=leak"
2020-05-21 memmem: configure better with "clang -fsanitize=undefined"
2020-05-19 ftoastr: fix ifndef typo
* build-aux/config.guess, build-aux/config.sub, doc/misc/texinfo.tex:
* lib/count-one-bits.h, lib/ftoastr.c, lib/ftoastr.h:
* lib/getloadavg.c, lib/gettimeofday.c, lib/libc-config.h:
* lib/open.c, lib/openat-proc.c, lib/tempname.c, lib/tempname.h:
* lib/unistd.in.h, lib/verify.h, m4/memmem.m4, m4/regex.m4:
* m4/unistd_h.m4:
Update from Gnulib.
* lib/getrandom.c, lib/sys_random.in.h:
* m4/getrandom.m4, m4/sys_random_h.m4:
New files, copied from Gnulib.
* lib/gnulib.mk.in, m4/gnulib-comp.m4: Regenerate.
2020-06-27 17:00:17 +00:00
|
|
|
# define STRTOF PREFIX (strtod)
|
2011-01-09 01:20:28 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* On hosts where it's not known that snprintf works, use sprintf to
|
|
|
|
implement the subset needed here. Typically BUFSIZE is big enough
|
|
|
|
and there's little or no performance hit. */
|
Update from Gnulib
This incorporates:
2020-06-27 getloadavg: don’t depend on fopen-gnu
2020-06-25 c-dtoastr, c-ldtoastr: new modules
2020-06-01 getloadavg: fix double-increment bug
2020-06-01 tempname: use getrandom, not getentropy
2020-05-31 tempname: merge from glibc and coreutils
2020-05-31 getentropy: work around a macOS and Solaris problem
2020-05-31 fnmatch: merge from glibc
2020-05-30 unistd: remove conflicting declaration of getrandom
2020-05-30 don't assume that UNICODE is not defined
2020-05-29 fix compilation error on native Windows
2020-05-28 avoid dynamic loading of Windows API functions when possible
2020-05-28 at-internal: make more robust in multithreaded applications
2020-05-28 getloadavg: make more robust in multithreaded applications
2020-05-27 getloadavg: make more robust in multithreaded applications
2020-05-26 count-one-bits: fix MSVC specific code
2020-05-25 getentropy, getrandom: new modules
2020-05-24 open, openat: really support O_CLOEXEC
2020-05-23 verify: document ‘assume’ better
2020-05-21 regex: configure better with "clang -fsanitize=leak"
2020-05-21 memmem: configure better with "clang -fsanitize=undefined"
2020-05-19 ftoastr: fix ifndef typo
* build-aux/config.guess, build-aux/config.sub, doc/misc/texinfo.tex:
* lib/count-one-bits.h, lib/ftoastr.c, lib/ftoastr.h:
* lib/getloadavg.c, lib/gettimeofday.c, lib/libc-config.h:
* lib/open.c, lib/openat-proc.c, lib/tempname.c, lib/tempname.h:
* lib/unistd.in.h, lib/verify.h, m4/memmem.m4, m4/regex.m4:
* m4/unistd_h.m4:
Update from Gnulib.
* lib/getrandom.c, lib/sys_random.in.h:
* m4/getrandom.m4, m4/sys_random_h.m4:
New files, copied from Gnulib.
* lib/gnulib.mk.in, m4/gnulib-comp.m4: Regenerate.
2020-06-27 17:00:17 +00:00
|
|
|
#ifdef C_LOCALE
|
|
|
|
# undef snprintf
|
|
|
|
# define snprintf c_snprintf
|
|
|
|
#elif ! GNULIB_SNPRINTF
|
2011-01-09 01:20:28 +00:00
|
|
|
# undef snprintf
|
|
|
|
# define snprintf ftoastr_snprintf
|
|
|
|
static int
|
|
|
|
ftoastr_snprintf (char *buf, size_t bufsize, char const *format,
|
|
|
|
int width, int prec, FLOAT x)
|
|
|
|
{
|
2017-03-03 16:45:10 +00:00
|
|
|
PROMOTED_FLOAT promoted_x = x;
|
2011-01-09 01:20:28 +00:00
|
|
|
char width_0_buffer[LENGTH == 1 ? FLT_BUFSIZE_BOUND
|
|
|
|
: LENGTH == 2 ? DBL_BUFSIZE_BOUND
|
|
|
|
: LDBL_BUFSIZE_BOUND];
|
|
|
|
int n = width;
|
|
|
|
if (bufsize < sizeof width_0_buffer)
|
|
|
|
{
|
2017-03-03 16:45:10 +00:00
|
|
|
n = sprintf (width_0_buffer, format, 0, prec, promoted_x);
|
2011-01-09 01:20:28 +00:00
|
|
|
if (n < 0)
|
|
|
|
return n;
|
|
|
|
if (n < width)
|
|
|
|
n = width;
|
|
|
|
}
|
|
|
|
if (n < bufsize)
|
2017-03-03 16:45:10 +00:00
|
|
|
n = sprintf (buf, format, width, prec, promoted_x);
|
2011-01-09 01:20:28 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
#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:
|
|
|
|
|
2017-04-27 22:21:34 +00:00
|
|
|
Andrysco M, Jhala R, Lerner S. Printing floating-point numbers:
|
|
|
|
a faster, always correct method. ACM SIGPLAN notices - POPL '16.
|
Update from Gnulib
This incorporates:
2019-09-22 Update some URLs
2019-09-15 fcntl-h: fix compilation error of creat.c on MSVC
2019-09-15 creat: new module
2019-09-15 access: new module
2019-09-09 Add option to assume best, not worst, when cross-compiling.
* build-aux/config.guess, build-aux/config.sub, doc/misc/texinfo.tex:
* lib/careadlinkat.c, lib/careadlinkat.h, lib/count-leading-zeros.h:
* lib/count-trailing-zeros.h, lib/diffseq.h, lib/fcntl.in.h:
* lib/ftoastr.c, lib/get-permissions.c:
* lib/ieee754.in.h, lib/inttypes.in.h, lib/mktime.c, lib/open.c:
* lib/pathmax.h, lib/pipe2.c, lib/stddef.in.h, lib/stdint.in.h:
* lib/stdlib.in.h, lib/str-two-way.h, lib/string.in.h, lib/time.in.h:
* lib/timegm.c, lib/unistd.in.h, m4/canonicalize.m4:
* m4/extern-inline.m4, m4/fcntl_h.m4, m4/fdopendir.m4:
* m4/getgroups.m4, m4/getopt.m4, m4/gettimeofday.m4:
* m4/gnulib-common.m4, m4/largefile.m4:
* m4/lstat.m4, m4/memmem.m4, m4/mktime.m4, m4/nocrash.m4, m4/open.m4:
* m4/pselect.m4, m4/putenv.m4, m4/readlink.m4, m4/regex.m4:
* m4/symlink.m4, m4/unistd_h.m4, m4/utimens.m4, m4/utimes.m4:
Copy from Gnulib.
* lib/gnulib.mk.in, m4/gnulib-comp.m4: Regenerate.
* m4/open-slash.m4: New file, copied from Gnulib.
2019-09-23 06:50:59 +00:00
|
|
|
2016;51(1):555-67 <https://doi.org/10.1145/2914770.2837654>; draft at
|
Merge from Gnulib
This incorporates:
2017-09-16 manywarnings: port to GCC on 64-bit MS-Windows
2017-09-13 all: Replace many more http URLs by https URLs
* build-aux/config.guess, build-aux/config.sub:
* build-aux/gitlog-to-changelog, doc/misc/texinfo.tex:
* lib/allocator.h, lib/count-leading-zeros.h:
* lib/count-trailing-zeros.h, lib/dup2.c, lib/filevercmp.c:
* lib/fstatat.c, lib/fsync.c, lib/ftoastr.c, lib/ftoastr.h:
* lib/intprops.h, lib/signal.in.h, lib/stdio-impl.h, lib/stdio.in.h:
* lib/unistd.in.h, lib/utimens.c, m4/alloca.m4, m4/extern-inline.m4:
* m4/fstatat.m4, m4/gnulib-common.m4, m4/manywarnings.m4:
* m4/std-gnu11.m4, m4/sys_types_h.m4, m4/vararrays.m4:
Copy from Gnulib.
* lib/gnulib.mk.in: Regenerate.
2017-09-17 19:56:00 +00:00
|
|
|
<https://cseweb.ucsd.edu/~lerner/papers/fp-printing-popl16.pdf>. */
|
2011-01-09 01:20:28 +00:00
|
|
|
|
2017-03-03 16:45:10 +00:00
|
|
|
PROMOTED_FLOAT promoted_x = x;
|
2011-01-09 01:20:28 +00:00
|
|
|
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++)
|
|
|
|
{
|
2017-03-03 16:45:10 +00:00
|
|
|
int n = snprintf (buf, bufsize, format, width, prec, promoted_x);
|
2011-01-09 01:20:28 +00:00
|
|
|
if (n < 0
|
|
|
|
|| FLOAT_PREC_BOUND <= prec
|
|
|
|
|| (n < bufsize && STRTOF (buf, NULL) == x))
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|