1
0
Fork 0

Added special-case handling to %E, added +inf and -inf handling, test cases

development
Martijn Jasperse 6 years ago
parent 2019bc0192
commit 56d9df0bd3

@ -316,6 +316,7 @@ static size_t _ntoa_long_long(out_fct_type out, char* buffer, size_t idx, size_t
#if defined(PRINTF_SUPPORT_FLOAT) #if defined(PRINTF_SUPPORT_FLOAT)
#include <math.h>
#if defined(PRINTF_SUPPORT_EXPONENTIAL) #if defined(PRINTF_SUPPORT_EXPONENTIAL)
// forward declaration so that _ftoa can switch to exp notation for values > PRINTF_MAX_FLOAT // forward declaration so that _ftoa can switch to exp notation for values > PRINTF_MAX_FLOAT
static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags); static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags);
@ -330,10 +331,13 @@ static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d
// powers of 10 // powers of 10
static const double pow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 }; static const double pow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
// test for NaN // test for special values
if (value != value) { if (value != value)
return _out_rev(out, buffer, idx, maxlen, "nan", 3, width, flags); // ensure value is padded as required return _out_rev(out, buffer, idx, maxlen, "nan", 3, width, flags);
} if (value < -DBL_MAX)
return _out_rev(out, buffer, idx, maxlen, "fni-", 4, width, flags);
if (value > DBL_MAX)
return _out_rev(out, buffer, idx, maxlen, (flags & FLAGS_PLUS) ? "fni+" : "fni", (flags & FLAGS_PLUS) ? 4 : 3, width, flags);
// test for very large values // test for very large values
// standard printf behavior is to print EVERY whole number digit -- which could be 100s of characters overflowing your buffers == bad // standard printf behavior is to print EVERY whole number digit -- which could be 100s of characters overflowing your buffers == bad
@ -448,9 +452,12 @@ static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d
} }
#if defined(PRINTF_SUPPORT_EXPONENTIAL) #if defined(PRINTF_SUPPORT_EXPONENTIAL)
#include <math.h>
static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags) static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags)
{ {
// check for special values
if ((value != value)||(value > DBL_MAX)||(value < -DBL_MAX))
return _ftoa(out, buffer, idx, maxlen, value, prec, width, flags);
// determine the sign // determine the sign
bool negative = value < 0; bool negative = value < 0;
if (negative) value = -value; if (negative) value = -value;

@ -1045,9 +1045,19 @@ TEST_CASE("length", "[]" ) {
TEST_CASE("float", "[]" ) { TEST_CASE("float", "[]" ) {
char buffer[100]; char buffer[100];
test::sprintf(buffer, "%8f", NAN); // using the NAN macro of math.h // test special-case floats using math.h macros
test::sprintf(buffer, "%8f", NAN);
REQUIRE(!strcmp(buffer, " nan")); REQUIRE(!strcmp(buffer, " nan"));
test::sprintf(buffer, "%8f", INFINITY);
REQUIRE(!strcmp(buffer, " inf"));
test::sprintf(buffer, "%-8f", -INFINITY);
REQUIRE(!strcmp(buffer, "-inf "));
test::sprintf(buffer, "%+8e", INFINITY);
REQUIRE(!strcmp(buffer, " +inf"));
test::sprintf(buffer, "%.4f", 3.1415354); test::sprintf(buffer, "%.4f", 3.1415354);
REQUIRE(!strcmp(buffer, "3.1415")); REQUIRE(!strcmp(buffer, "3.1415"));

Loading…
Cancel
Save