Skip to content

Commit

Permalink
Printf float fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mysterymath committed Nov 30, 2023
1 parent 5597699 commit a5e0a1c
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions mos-platform/common/c/printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@
#endif

// define the largest float suitable to print with %f
// default: 1e9
// default: 1e18
#ifndef PRINTF_MAX_FLOAT
#define PRINTF_MAX_FLOAT 1e9
#define PRINTF_MAX_FLOAT 1e18
#endif

// support for the long long types (%llu or %p)
Expand Down Expand Up @@ -420,7 +420,7 @@ static size_t _ftoa(out_fct_type out, char *buffer, size_t idx, size_t maxlen,

// test for negative
bool negative = false;
if (value < 0) {
if (__builtin_signbit(value)) {
negative = true;
value = 0 - value;
}
Expand All @@ -435,7 +435,7 @@ static size_t _ftoa(out_fct_type out, char *buffer, size_t idx, size_t maxlen,
prec--;
}

int whole = (int)value;
long long whole = (long long)value;
double tmp = (value - whole) * pow10[prec];
unsigned long frac = (unsigned long)tmp;
diff = tmp - frac;
Expand Down Expand Up @@ -752,6 +752,10 @@ static int _vsnprintf(out_fct_type out, char *buffer, const size_t maxlen,
flags |= (sizeof(size_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
format++;
break;
case 'L':
// Long double is double.
format++;
break;
default:
break;
}
Expand Down

0 comments on commit a5e0a1c

Please sign in to comment.