diff mbox

[C++,/,RFC] PR 33067

Message ID 4E92D995.2070907@oracle.com
State New
Headers show

Commit Message

Paolo Carlini Oct. 10, 2011, 11:40 a.m. UTC
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.

////////////////////

Comments

Paolo Carlini Oct. 10, 2011, 12:13 p.m. UTC | #1
. looks like we want to do something else, not printing the number at 
all. See audit trail.

Paolo.
Joseph Myers Oct. 10, 2011, 10:44 p.m. UTC | #2
On Mon, 10 Oct 2011, Paolo Carlini wrote:

> +  // The fraction 643/2136 approximates log10(2) to 7 significant digits.

Whatever the conclusion on the approach to take, note that we don't use 
C++ comments in GCC at present.
Jason Merrill Oct. 11, 2011, 1:04 a.m. UTC | #3
On 10/10/2011 12:40 PM, Paolo Carlini wrote:
> +  // The fraction 643/2136 approximates log10(2) to 7 significant digits.
> +  int max_digits10 = 2 + (is_decimal ? fmt->p : fmt->p * 643L / 2136);

Please cite N1822 in the comment and convert it to C syntax.  OK with 
that change.

Jason
diff mbox

Patch

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');