delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2003/08/13/10:07:35

Date: Wed, 13 Aug 2003 11:01:11 +0300 (EET DST)
From: Esa A E Peuha <peuha AT cc DOT helsinki DOT fi>
Sender: peuha AT sirppi DOT helsinki DOT fi
To: djgpp-workers AT delorie DOT com
Subject: Re: Anomaly in printf()
In-Reply-To: <46.3c4de930.2c6a2e90@aol.com>
Message-ID: <Pine.OSF.4.51.0308131046310.2694@sirppi.helsinki.fi>
References: <46 DOT 3c4de930 DOT 2c6a2e90 AT aol DOT com>
MIME-Version: 1.0
Reply-To: djgpp-workers AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp-workers AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

On Tue, 12 Aug 2003 Kbwms AT aol DOT com wrote:

> In a message dated 8/12/2003 3:17:57 AM Eastern Standard Time,
> eliz AT elta DOT co DOT il writes:
>
> > Deduction, perhaps mistaken: after the first iteration thru this
> > loop:
> >
> >    for (; integer &&p >= startp; ++expcnt)
> >    {
> >   tmp = modfl(integer * 0.1L , &integer);
> >   *p-- = tochar((int)((tmp + .01L) * 10));
> >    }
> >
> > the value of `integer' had a .375 as its fraction.
>
> A mistake is in multiplying by 0.1 instead of dividing by 10 and taking the
> remainder.

I agree.  This should definitely divide by ten to produce as accurate
result as possible.  Multiplying by 0.1 will cause errors when the
number has large absolute value.  Could someone test the following patch
before I commit it?

Index: doprnt.c
===================================================================
RCS file: /cvs/djgpp/djgpp/src/libc/ansi/stdio/doprnt.c,v
retrieving revision 1.16
diff -p -u -r1.16 doprnt.c
--- doprnt.c	30 Jun 2003 20:38:15 -0000	1.16
+++ doprnt.c	13 Aug 2003 07:58:17 -0000
@@ -587,7 +587,7 @@ cvtl(long double number, int prec, int f
    */
   for (; integer && p >= startp; ++expcnt)
   {
-    tmp = modfl(integer * 0.1L , &integer);
+    tmp = modfl(integer / 10.0L , &integer);
     *p-- = tochar((int)((tmp + .01L) * 10));
   }
   switch(fmtch)

-- 
Esa Peuha
student of mathematics at the University of Helsinki
http://www.helsinki.fi/~peuha/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019