Hi,
reporter complains that, for:
struct T {} t;
bool b = 1.1 < t;
we output (on x86_64-linux):
33067.C:2:18: error: no match for ‘operator<’ in
‘1.100000000000000088817841970012523233890533447265625e+0 < t’
which is clearly pretty dumb. In my opinion, a definite improvement
would be following:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1822.pdf
thus output a "minimal" number of decimal digits. If I apply the
attached patchlet, I get:
33067.C:2:18: error: no match for ‘operator<’ in ‘1.1000000000000001e+0 < t’
which looks much better to me. Comments? Does the idea make sense to
everybody?
Thanks,
Paolo.
PS: I didn't carefully check the decimal floating point case, maybe we
could do better.
////////////////////
Index: c-family/c-pretty-print.c
===================================================================
--- c-family/c-pretty-print.c (revision 179739)
+++ c-family/c-pretty-print.c (working copy)
@@ -1018,8 +1018,19 @@ pp_c_enumeration_constant (c_pretty_printer *pp, t
static void
pp_c_floating_constant (c_pretty_printer *pp, tree r)
{
+ const struct real_format *fmt
+ = REAL_MODE_FORMAT (TYPE_MODE (TREE_TYPE (r)));
+
+ REAL_VALUE_TYPE floating_cst = TREE_REAL_CST (r);
+ bool is_decimal = floating_cst.decimal;
+
+ // The fraction 643/2136 approximates log10(2) to 7 significant digits.
+ int max_digits10 = 2 + (is_decimal ? fmt->p : fmt->p * 643L / 2136);
+
real_to_decimal (pp_buffer (pp)->digit_buffer, &TREE_REAL_CST (r),
- sizeof (pp_buffer (pp)->digit_buffer), 0, 1);
+ sizeof (pp_buffer (pp)->digit_buffer),
+ max_digits10, 1);
+
pp_string (pp, pp_buffer(pp)->digit_buffer);
if (TREE_TYPE (r) == float_type_node)
pp_character (pp, 'f');