[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#632031: convert: gray colorspace creates broken postscript



On Wed, 29 Jun 2011 15:37:08 +0200
Bastien ROUCARIES <roucaries.bastien@gmail.com> wrote:

> Could you post also the printf patch and the rational for this patch ?

The reason for this patch is to deal better (for our concerns, i.e.
printing of amounts) with the rounded printing of non-representable
floating point numbers.

You would expect

$ printf '%.2f\n' 0.355

to print 0.36 but it does print 0.35. This is because 0.355 is not
representable as IEEE 754 floating point number and is rounded to the
next representable number (0.3549999...) before passing it as parameter
to printf.

The patch causes printf to take the next greater representable
floating point number of every passed floating point parameter and use
that for the output. This has proven to produce better results for us.


Best regards,
Christoph


diff --git a/stdio-common/printf_fp.c b/stdio-common/printf_fp.c
index 403636a..5a9d66d 100644
--- a/stdio-common/printf_fp.c
+++ b/stdio-common/printf_fp.c
@@ -466,6 +466,24 @@ ___printf_fp (FILE *fp,
       return done;
     }
 
+  /* Account for non-representable decimal numbers.  "Critical" numbers
+     that are just below a non-representable decimal number will bei rounded
+     better.  Most conversions will not change by adding a small amount to
+     the mantissa.  */
+  if (fpnum.dbl != 0.0)
+  {
+    mp_limb_t cy = __mpn_add_1 (fp_input, fp_input, fracsize, 1);
+
+    if (to_shift == 1? cy:
+        fp_input[fracsize - 1]
+        & (((mp_limb_t) 1) << (1 + BITS_PER_MP_LIMB - to_shift)))
+      {
+        ++exponent;
+        (void) __mpn_rshift (fp_input, fp_input, fracsize, 1);
+        fp_input[fracsize - 1]
+         |= ((mp_limb_t) 1) << (BITS_PER_MP_LIMB - to_shift);
+      }
+  }
 
   /* We need three multiprecision variables.  Now that we have the exponent
      of the number we can allocate the needed memory.  It would be more

Reply to: