aboutsummaryrefslogtreecommitdiffstats
path: root/printf.c
diff options
context:
space:
mode:
authorMarco Paland <marco@paland.com>2018-08-21 12:37:46 +0200
committerMarco Paland <marco@paland.com>2018-08-21 12:37:46 +0200
commite6b5331a36a5815cc36a9b2872f29234efee72cb (patch)
tree53519bcfd31b2663c749f86ddc1cfdcc388ea911 /printf.c
parent61de9c0cb0738e51625b6071a88921fecd591180 (diff)
downloadprintf-e6b5331a36a5815cc36a9b2872f29234efee72cb.tar.gz
printf-e6b5331a36a5815cc36a9b2872f29234efee72cb.tar.bz2
printf-e6b5331a36a5815cc36a9b2872f29234efee72cb.zip
fix(printf): fix floating point precision limit
Return the correct count of precision digits now. Fixes #22
Diffstat (limited to 'printf.c')
-rw-r--r--printf.c18
1 files changed, 11 insertions, 7 deletions
diff --git a/printf.c b/printf.c
index 0fd6569..c28cf13 100644
--- a/printf.c
+++ b/printf.c
@@ -277,13 +277,14 @@ static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d
value = 0 - value;
}
- // limit precision
+ // set default precision to 6, if not set explicitly
if (!(flags & FLAGS_PRECISION)) {
- prec = 6U; // by default, precesion is 6
+ prec = 6U;
}
- if (prec > 9U) {
- // precision of >= 10 can lead to overflow errors
- prec = 9U;
+ // limit precision to 9, cause a prec >= 10 can lead to overflow errors
+ while ((len < PRINTF_FTOA_BUFFER_SIZE) && (prec > 9U)) {
+ buf[len++] = '0';
+ prec--;
}
int whole = (int)value;
@@ -325,10 +326,13 @@ static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d
else {
unsigned int count = prec;
// now do fractional part, as an unsigned number
- do {
+ while (len < PRINTF_FTOA_BUFFER_SIZE) {
--count;
buf[len++] = (char)(48U + (frac % 10U));
- } while ((len < PRINTF_FTOA_BUFFER_SIZE) && (frac /= 10U));
+ if (!(frac /= 10U)) {
+ break;
+ }
+ }
// add extra 0s
while ((len < PRINTF_FTOA_BUFFER_SIZE) && (count-- > 0U)) {
buf[len++] = '0';