|
From: <sv...@va...> - 2015-05-20 15:14:08
|
Author: florian
Date: Wed May 20 16:14:00 2015
New Revision: 15264
Log:
Fix rounding when printing floating point numbers.
Modified:
trunk/coregrind/m_debuglog.c
trunk/none/tests/unit_debuglog.c
trunk/none/tests/unit_debuglog.stderr.exp
Modified: trunk/coregrind/m_debuglog.c
==============================================================================
--- trunk/coregrind/m_debuglog.c (original)
+++ trunk/coregrind/m_debuglog.c Wed May 20 16:14:00 2015
@@ -1006,10 +1006,17 @@
/* Silently limit the precision to 10 digits. */
if (precision > 10) precision = 10;
- /* If fracional part is not printed (precision == 0), may have to
- round up */
- if (precision == 0 && frac >= 0.5)
+ /* Determine fractional part, possibly round up */
+ ULong factor = 1;
+ for (cnt = 0; cnt < precision; ++cnt)
+ factor *= 10;
+ ULong frval = frac * factor;
+ if ((frac * factor - frval) > 0.5) // round up
+ frval += 1;
+ /* Check rounding. */
+ if (frval == factor)
ipval += 1;
+ frval %= factor;
/* Find out how many characters are needed to print the number */
@@ -1046,14 +1053,6 @@
send('.', send_arg2);
ret += 1;
- // Fractional part
- ULong factor = 1;
- for (cnt = 0; cnt < precision; ++cnt)
- factor *= 10;
- ULong frval = frac * factor;
- if ((frac * factor - frval) > 0.5) // round up
- frval += 1;
- frval %= factor;
ret += myvprintf_int64(send, send_arg2, VG_MSG_ZJUSTIFY, 10,
precision, False, frval);
}
Modified: trunk/none/tests/unit_debuglog.c
==============================================================================
--- trunk/none/tests/unit_debuglog.c (original)
+++ trunk/none/tests/unit_debuglog.c Wed May 20 16:14:00 2015
@@ -149,5 +149,13 @@
run("|%100lld|", ival);
run("|%*lld|", 13, ival);
+ value = 0.99685224;
+ run("|%3.0f|", value);
+ run("|%3.1f|", value);
+ run("|%3.2f|", value);
+ run("|%3.3f|", value);
+ run("|%3.4f|", value);
+ run("|%3.5f|", value);
+
return 0;
}
Modified: trunk/none/tests/unit_debuglog.stderr.exp
==============================================================================
--- trunk/none/tests/unit_debuglog.stderr.exp (original)
+++ trunk/none/tests/unit_debuglog.stderr.exp Wed May 20 16:14:00 2015
@@ -127,3 +127,15 @@
|%100lld| debuglog = | -1004005| wrote 102 chars
|%*lld| printf = | -1004005| wrote 15 chars
|%*lld| debuglog = | -1004005| wrote 15 chars
+|%3.0f| printf = | 1| wrote 5 chars
+|%3.0f| debuglog = | 1| wrote 5 chars
+|%3.1f| printf = |1.0| wrote 5 chars
+|%3.1f| debuglog = |1.0| wrote 5 chars
+|%3.2f| printf = |1.00| wrote 6 chars
+|%3.2f| debuglog = |1.00| wrote 6 chars
+|%3.3f| printf = |0.997| wrote 7 chars
+|%3.3f| debuglog = |0.997| wrote 7 chars
+|%3.4f| printf = |0.9969| wrote 8 chars
+|%3.4f| debuglog = |0.9969| wrote 8 chars
+|%3.5f| printf = |0.99685| wrote 9 chars
+|%3.5f| debuglog = |0.99685| wrote 9 chars
|