diff options
author | Marco Paland <marco@paland.com> | 2019-01-26 15:57:10 +0100 |
---|---|---|
committer | Marco Paland <marco@paland.com> | 2019-01-26 15:57:10 +0100 |
commit | 80b42fef5f070c980584b45de7b4a84322c749a0 (patch) | |
tree | 2f16f1e4b4e0db2bd181e8e0271fb8c6eb925069 | |
parent | bf72dfb8dee9ee57463c7082aca520004cb7cb7c (diff) | |
download | printf-80b42fef5f070c980584b45de7b4a84322c749a0.tar.gz printf-80b42fef5f070c980584b45de7b4a84322c749a0.tar.bz2 printf-80b42fef5f070c980584b45de7b4a84322c749a0.zip |
fix(printf): fixed support of NaN
(hot) fixes #37
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | printf.c | 14 | ||||
-rw-r--r-- | test/test_suite.cpp | 4 |
3 files changed, 17 insertions, 3 deletions
@@ -28,7 +28,7 @@ Therefore I decided to write an own, final implementation which meets the follow - Support of decimal/floating number representation (with an own fast itoa/ftoa)
- Reentrant and thread-safe, malloc free, no static vars/buffers
- LINT and compiler L4 warning free, mature, coverity clean, automotive ready
- - Extensive test suite (> 350 test cases) passing
+ - Extensive test suite (> 370 test cases) passing
- Simply the best *printf* around the net
- MIT license
@@ -311,6 +311,14 @@ static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d // powers of 10
static const double pow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
+ // test for NaN
+ if (value != value) {
+ out('n', buffer, idx++, maxlen);
+ out('a', buffer, idx++, maxlen);
+ out('n', buffer, idx++, maxlen);
+ return idx;
+ }
+
// test for negative
bool negative = false;
if (value < 0) {
@@ -341,8 +349,10 @@ static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d ++whole;
}
}
- else if ((diff == 0.5) && ((frac == 0U) || (frac & 1U))) {
- // if halfway, round up if odd, OR if last digit is 0
+ else if (diff < 0.5) {
+ }
+ else if ((frac == 0U) || (frac & 1U)) {
+ // if halfway, round up if odd OR if last digit is 0
++frac;
}
diff --git a/test/test_suite.cpp b/test/test_suite.cpp index 66ba5f3..2103a5b 100644 --- a/test/test_suite.cpp +++ b/test/test_suite.cpp @@ -31,6 +31,7 @@ #include "catch.hpp"
#include <string.h>
+#include <math.h>
namespace test {
// use functions in own test namespace to avoid stdio conflicts
@@ -1019,6 +1020,9 @@ TEST_CASE("length", "[]" ) { TEST_CASE("float", "[]" ) {
char buffer[100];
+ test::sprintf(buffer, "%.4f", NAN); // using the NAN macro of math.h
+ REQUIRE(!strcmp(buffer, "nan"));
+
test::sprintf(buffer, "%.4f", 3.1415354);
REQUIRE(!strcmp(buffer, "3.1415"));
|