Description: <short summary of the patch>
 TODO: Put a short summary on the line above and replace this paragraph
 with a longer explanation of this change. Complete the meta-information
 with other relevant fields (see below for details). To make it easier, the
 information below has been extracted from the changelog. Adjust it or drop
 it.
 .
 gcl (2.6.7a-1) unstable; urgency=high
 .
   * Accept Non-maintainer upload. (thanks David PrÃ©vot <taffit@debian.org>)
   * Remove unused and non DFSG-compliant gmp3/gmp.* from source.   (Closes: #695721)
   * Show translated debconf templates, thanks to Denis Barbier for the
     analysis and the proposed fixes. (Closes: #691946)
   * trim excess digits from printed floats
Author: Camm Maguire <camm@debian.org>
Bug-Debian: http://bugs.debian.org/691946
Bug-Debian: http://bugs.debian.org/695721

---
The information above should follow the Patch Tagging Guidelines, please
checkout http://dep.debian.net/deps/dep3/ to learn about the format. Here
are templates for supplementary fields that you might want to add:

Origin: <vendor|upstream|other>, <url of original patch>
Bug: <url in upstream bugtracker>
Bug-Debian: http://bugs.debian.org/<bugnumber>
Bug-Ubuntu: https://launchpad.net/bugs/<bugnumber>
Forwarded: <no|not-needed|url proving that it has been forwarded>
Reviewed-By: <name and email of someone who approved the patch>
Last-Update: <YYYY-MM-DD>

--- gcl-2.6.7a.orig/o/print.d
+++ gcl-2.6.7a/o/print.d
@@ -303,76 +303,99 @@ write_base(void)
 
 object sSAprint_nansA;
 
+#include <string.h>
+
+static int
+char_inc(char *b,char *p) {
+
+  if (b==p) {
+    if (*p=='-') {
+      p++;
+      memmove(p+1,p,strlen(p)+1);
+    }
+    *p='1';
+  } else if (*p=='9') {
+    *p='0';
+    char_inc(b,p-1);
+  } else if (*p=='.')
+    char_inc(b,p-1);
+  else (*p)++;
+
+  return 1;
+
+}
+
+#define COMP(a_,b_,c_,d_) ((d_) ? strtod((a_),(b_))==(c_) : strtof((a_),(b_))==(float)(c_))
+
+static int
+truncate_double(char *b,double d,int dp) {
+
+  char c[FPRC+9],c1[FPRC+9],*p,*pp,*n;
+  int j,k;
+
+  n=b;
+  k=strlen(n);
+
+  strcpy(c1,b);
+  for (p=c1;*p && *p!='e';p++);
+  pp=p>c1 && p[-1]!='.' ? p-1 : p;
+  for (;pp>c1 && pp[-1]=='0';pp--);
+  strcpy(pp,p);
+  if (pp!=p && COMP(c1,&pp,d,dp))
+    k=truncate_double(n=c1,d,dp);
+
+  strcpy(c,n);
+  for (p=c;*p && *p!='e';p++);
+  if (p[-1]!='.' && char_inc(c,p-1) && COMP(c,&pp,d,dp)) {
+    j=truncate_double(c,d,dp);
+    if (j<k) {
+      k=j;
+      n=c;
+    }
+  }
+
+  if (n!=b) strcpy(b,n);
+  return k;
+
+}
+
 void
-edit_double(n, d, sp, s, ep)
-int n;
-double d;
-char *s;
-int *sp;
-int *ep;
-{
-	char *p, buff[FPRC + 9];
-	int i;
+edit_double(int n, double d, int *sp, char *s, int *ep) {
 
-#ifdef IEEEFLOAT
-/*	if ((*((int *)&d +HIND) & 0x7ff00000) == 0x7ff00000)*/
-	if (!ISFINITE(d))
-           {if (sSAprint_nansA->s.s_dbind !=Cnil)
-	      {sprintf(s, "%e",d);
-	       *sp = 2;
-	       return;
-	     }
-	   else
-		FEerror("Can't print a non-number.",
-			0);}
-	else
-		sprintf(buff, "%*.*e",FPRC+8,FPRC, d);
-	if (buff[FPRC+3] != 'e') {
-		sprintf(buff, "%*.*e",FPRC+7,FPRC,d);
-		*ep = (buff[FPRC+5]-'0')*10 + (buff[FPRC+6]-'0');
-	} else
-		*ep = (buff[FPRC+5]-'0')*100 +
-		  (buff[FPRC+6]-'0')*10 + (buff[FPRC+7]-'0');
-	*sp = 1;
-	if (buff[0] == '-')
-		*sp *= -1;
-#else
-	sprintf(buff, "%*.*e",FPRC+7,FPRC, d);
-	/*  "-D.MMMMMMMMMMMMMMMe+EE"  */
-	/*   0123456789012345678901   */
-	*sp = 1;
-	if (buff[0] == '-')
-		*sp *= -1;
-	*ep = (buff[FPRC+5]-'0')*10 + (buff[FPRC+6]-'0');
-#endif
-
-	if (buff[FPRC+4] == '-')
-		*ep *= -1;
-	buff[2] = buff[1];
-	p = buff + 2;
-	if (n < FPRC+1) {
-		if (p[n] >= '5') {
-			for (i = n - 1;  i >= 0;  --i)
-				if (p[i] == '9')
-					p[i] = '0';
-				else {
-					p[i]++;
-					break;
-				}
-			if (i < 0) {
-				*--p = '1';
-				(*ep)++;
-			}
-		}
-		for (i = 0;  i < n;  i++)
-			s[i] = p[i];
-	} else {
-		for (i = 0;  i < FPRC+1;  i++)
-			s[i] = p[i];
-		for (;  i < n;  i++)
-			s[i] = '0';
-	}
-	s[n] = '\0';
+  char *p, b[FPRC + 9];
+  int i;
+  
+  if (!ISFINITE(d)) {
+    if (sSAprint_nansA->s.s_dbind !=Cnil) {
+      sprintf(s, "%e",d);
+      *sp = 2;
+      return;
+    }
+    else
+      FEerror("Can't print a non-number.",0);}
+  else
+    sprintf(b, "%*.*e",FPRC+8,FPRC, d);
+  if (b[FPRC+3] != 'e') {
+    sprintf(b, "%*.*e",FPRC+7,FPRC,d);
+    *ep = (b[FPRC+5]-'0')*10 + (b[FPRC+6]-'0');
+  } else
+    *ep = (b[FPRC+5]-'0')*100 + (b[FPRC+6]-'0')*10 + (b[FPRC+7]-'0');
+  *sp = 1;
+  if (b[0] == '-')
+    *sp *= -1;
+  if (b[FPRC+4] == '-')
+    *ep *= -1;
+
+  truncate_double(b,d,n!=7);
+
+  if (b[2]=='0') (*ep)++;
+  b[2] = b[1];
+  p = b + 2;
+  for (i=0;i<n && i<FPRC+1 && isdigit(p[i]);i++)
+      s[i] = p[i];
+  for (;i<n;i++)
+    s[i] = '0';
+  s[n] = '\0';
 }
 
 static void
@@ -405,7 +428,7 @@ bool shortp;
 			for (i = 0;  i < exp;  i++)
 				write_ch('0');
 			for (;  n > 0;  --n)
-				if (buff[n-1] != '0')
+				if (buff[n-1] != '0' && buff[n-1])
 					break;
 			if (exp == 0 && n == 0)
 				n = 1;
@@ -425,7 +448,7 @@ bool shortp;
 				write_ch('0');
 			i++;
 			for (;  n > i;  --n)
-				if (buff[n-1] != '0')
+				if (buff[n-1] != '0' && buff[n-1])
 					break;
 			for (;  i < n;  i++)
 				write_ch(buff[i]);
@@ -436,7 +459,7 @@ bool shortp;
 		write_ch('.');
 		write_ch(buff[1]);
 		for (;  n > 2;  --n)
-			if (buff[n-1] != '0')
+			if (buff[n-1] != '0' && buff[n-1])
 				break;
 		for (i = 2;  i < n;  i++)
 			write_ch(buff[i]);
