commit ee201b7: [Fix] Fix floating point printing

Vsevolod Stakhov vsevolod at highsecure.ru
Tue Apr 16 13:07:03 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-04-16 14:04:28 +0100
URL: https://github.com/rspamd/rspamd/commit/ee201b7a8c5a1116f3aae19de68520d9fc7391d3 (HEAD -> master)

[Fix] Fix floating point printing

---
 contrib/fpconv/fpconv.c | 11 ++++++-----
 contrib/fpconv/fpconv.h |  2 +-
 src/libutil/printf.c    | 20 ++++++++++++++++----
 3 files changed, 23 insertions(+), 10 deletions(-)

diff --git a/contrib/fpconv/fpconv.c b/contrib/fpconv/fpconv.c
index 3fbd55af0..12c67cfa6 100644
--- a/contrib/fpconv/fpconv.c
+++ b/contrib/fpconv/fpconv.c
@@ -99,7 +99,7 @@ static Fp multiply(Fp* a, Fp* b)
     uint64_t al_bl = (a->frac & lomask) * (b->frac & lomask);
     uint64_t ah_bh = (a->frac >> 32)    * (b->frac >> 32);
 
-    uint64_t tmp = (ah_bl & lomask) + (al_bh & lomask) + (al_bl >> 32); 
+    uint64_t tmp = (ah_bl & lomask) + (al_bh & lomask) + (al_bl >> 32);
     /* round up */
     tmp += 1U << 31;
 
@@ -206,7 +206,8 @@ static int grisu2(double d, char* digits, int* K)
     return generate_digits(&w, &upper, &lower, digits, K);
 }
 
-static int emit_digits(char* digits, int ndigits, char* dest, int K, bool neg)
+static int emit_digits(char* digits, int ndigits, char* dest, int K, bool neg,
+		bool scientific)
 {
     int exp = absv(K + ndigits - 1);
 
@@ -219,7 +220,7 @@ static int emit_digits(char* digits, int ndigits, char* dest, int K, bool neg)
     }
 
     /* write decimal w/o scientific notation */
-    if(K < 0 && (K > -7 || exp < 4)) {
+    if(scientific && (K < 0 && (K > -7 || exp < 4))) {
         int offset = ndigits - absv(K);
         /* fp < 1.0 -> write leading zero */
         if(offset <= 0) {
@@ -304,7 +305,7 @@ static int filter_special(double fp, char* dest)
     return 3;
 }
 
-int fpconv_dtoa(double d, char dest[24])
+int fpconv_dtoa(double d, char dest[24], bool scientific)
 {
     char digits[18];
 
@@ -326,7 +327,7 @@ int fpconv_dtoa(double d, char dest[24])
     int K = 0;
     int ndigits = grisu2(d, digits, &K);
 
-    str_len += emit_digits(digits, ndigits, dest + str_len, K, neg);
+    str_len += emit_digits(digits, ndigits, dest + str_len, K, neg, scientific);
 
     return str_len;
 }
diff --git a/contrib/fpconv/fpconv.h b/contrib/fpconv/fpconv.h
index 58bbcccbb..83180d41f 100644
--- a/contrib/fpconv/fpconv.h
+++ b/contrib/fpconv/fpconv.h
@@ -26,7 +26,7 @@
  *
  */
 
-int fpconv_dtoa(double fp, char dest[24]);
+int fpconv_dtoa(double fp, char dest[24], bool scientific);
 
 #endif
 
diff --git a/src/libutil/printf.c b/src/libutil/printf.c
index 9dc14441c..84dd5d272 100644
--- a/src/libutil/printf.c
+++ b/src/libutil/printf.c
@@ -947,9 +947,8 @@ rspamd_vprintf_common (rspamd_printf_append_func func,
 
 
 			case 'f':
-			case 'g':
 				f = (gdouble) va_arg (args, double);
-				slen = fpconv_dtoa (f, dtoabuf);
+				slen = fpconv_dtoa (f, dtoabuf, false);
 
 				if (frac_width != 0) {
 					const gchar *dot_pos = memchr (dtoabuf, '.', slen);
@@ -980,10 +979,16 @@ rspamd_vprintf_common (rspamd_printf_append_func func,
 
 				continue;
 
+			case 'g':
+				f = (gdouble) va_arg (args, double);
+				slen = fpconv_dtoa (f, dtoabuf, true);
+				RSPAMD_PRINTF_APPEND (dtoabuf, slen);
+
+				continue;
+
 			case 'F':
-			case 'G':
 				f = (gdouble) va_arg (args, long double);
-				slen = fpconv_dtoa (f, dtoabuf);
+				slen = fpconv_dtoa (f, dtoabuf, false);
 
 				if (frac_width != 0) {
 					const gchar *dot_pos = memchr (dtoabuf, '.', slen);
@@ -1014,6 +1019,13 @@ rspamd_vprintf_common (rspamd_printf_append_func func,
 
 				continue;
 
+			case 'G':
+				f = (gdouble) va_arg (args, long double);
+				slen = fpconv_dtoa (f, dtoabuf, true);
+				RSPAMD_PRINTF_APPEND (dtoabuf, slen);
+
+				continue;
+
 			case 'p':
 				ui64 = (uintptr_t) va_arg (args, void *);
 				hex = 2;


More information about the Commits mailing list