delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2003/08/10/16:01:14

From: Kbwms AT aol DOT com
Message-ID: <16e.2264c067.2c67fe7c@aol.com>
Date: Sun, 10 Aug 2003 16:01:00 EDT
Subject: Re: Anomaly in printf()
To: djgpp-workers AT delorie DOT com
MIME-Version: 1.0
X-Mailer: 8.0 for Windows sub 6011
Reply-To: djgpp-workers AT delorie DOT com

--part1_16e.2264c067.2c67fe7c_boundary
Content-Type: text/plain; charset="US-ASCII"
Content-Transfer-Encoding: 7bit

In a message dated 8/10/2003 3:30:30 PM Eastern Standard Time, 
eliz AT elta DOT co DOT il writes:

> >Date: Sun, 10 Aug 2003 14:17:13 -0400
> >From: DJ Delorie <dj AT delorie DOT com>
> >
> >>Behold:
> >>
> >>Demo    = 18446744073709551615.000
> >>Demo - 1 = 18446744073709551613.000
> >
> >Ok, that's a bug.
> 
> Is it?  We are printing a long double, which IIRC has 19 significant
> digits.  The ``error'' here happens in the 20th digit, unless I'm
> mistaken.
> 

Below is what C99 has to say.  Since DJGPP/GCC supports 64-bit precision, my 
assumption is that we should produce 21 digits.  As the footnote says,

       If the minimum-width IEC 60559 extended format  (64  bits
       of precision) is supported, DECIMAL_DIG shall be at least 21.  


       F.4  Floating to integer conversion

       [#1] If the floating value is infinite  or  NaN  or  if  the
       integral part of the floating value exceeds the range of the
       integer type, then the invalid exception is raised  and  the
       resulting  value is unspecified.  Whether conversion of non-
       integer floating values whose integral part  is  within  the
       range  of  the  integer type raises the inexact exception is
       unspecified.288)

       F.5  Binary-decimal conversion

       [#1] Conversion from the widest supported IEC  60559  format
       to  decimal with DECIMAL_DIG digits and back is the identity
       function.289)

       [#2]  Conversions  involving  IEC  60559  formats follow all
       pertinent recommended practice.  In  particular,  conversion
       between  any  supported  IEC  60559  format and decimal with
       DECIMAL_DIG  or  fewer  significant  digits   is   correctly
       rounded.                                                     |

       ____________________

       288ANSI/IEEE  854,  but  not  IEC  60559  (ANSI/IEEE   754),
          directly  specifies  that floating-to-integer conversions
          raise the  inexact  exception  for  non-integer  in-range
          values.    In  those  cases  where  it  matters,  library
          functions can be used to effect such conversions with  or
          without  raising the inexact exception.  See rint, lrint,
          llrint, and nearbyint in <math.h>.

       289If the minimum-width IEC 60559 extended format  (64  bits
          of precision) is supported, DECIMAL_DIG shall be at least
          21.  If IEC 60559 double (53 bits of  precision)  is  the
          widest IEC 60559 format supported, then DECIMAL_DIG shall
          be at least 17.  (By contrast, LDBL_DIG and  DBL_DIG  are
          19 and 15, respectively, for these formats.)


--part1_16e.2264c067.2c67fe7c_boundary
Content-Type: text/html; charset="US-ASCII"
Content-Transfer-Encoding: quoted-printable

<HTML><FONT FACE=3Darial,helvetica><FONT  SIZE=3D3 FAMILY=3D"SERIF" FACE=3D"=
Georgia" LANG=3D"0">In a message dated 8/10/2003 3:30:30 PM Eastern Standard=
 Time, eliz AT elta DOT co DOT il writes:<BR>
<BR>
<BLOCKQUOTE TYPE=3DCITE style=3D"BORDER-LEFT: #0000ff 2px solid; MARGIN-LEFT=
: 5px; MARGIN-RIGHT: 0px; PADDING-LEFT: 5px"></FONT><FONT  COLOR=3D"#000000"=
 style=3D"BACKGROUND-COLOR: #ffffff" SIZE=3D2 FAMILY=3D"SANSSERIF" FACE=3D"A=
rial" LANG=3D"0">&gt;Date: Sun, 10 Aug 2003 14:17:13 -0400<BR>
&gt;From: DJ Delorie &lt;dj AT delorie DOT com&gt;<BR>
&gt;<BR>
&gt;&gt;Behold:<BR>
&gt;&gt;<BR>
&gt;&gt;Demo&nbsp;&nbsp;&nbsp; =3D 18446744073709551615.000<BR>
&gt;&gt;Demo - 1 =3D 18446744073709551613.000<BR>
&gt;<BR>
&gt;Ok, that's a bug.<BR>
<BR>
Is it?&nbsp; We are printing a long double, which IIRC has 19 significant<BR=
>
digits.&nbsp; The ``error'' here happens in the 20th digit, unless I'm<BR>
mistaken.<BR>
</BLOCKQUOTE><BR>
</FONT><FONT  COLOR=3D"#000000" style=3D"BACKGROUND-COLOR: #ffffff" SIZE=3D3=
 FAMILY=3D"SERIF" FACE=3D"Georgia" LANG=3D"0"><BR>
Below is what C99 has to say.&nbsp; Since DJGPP/GCC supports 64-bit precisio=
n, my assumption is that we should produce 21 digits.&nbsp; As the footnote=20=
says,<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If the minimum-width IEC 60559 extended=
 format&nbsp; (64&nbsp; bits<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; of precision) is supported, DECIMAL_DIG=
 shall be at least 21.&nbsp; <BR>
<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; F.4&nbsp; Floating to integer conversio=
n<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [#1] If the floating value is infinite&=
nbsp; or&nbsp; NaN&nbsp; or&nbsp; if&nbsp; the<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; integral part of the floating value exc=
eeds the range of the<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; integer type, then the invalid exceptio=
n is raised&nbsp; and&nbsp; the<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; resulting&nbsp; value is unspecified.&n=
bsp; Whether conversion of non-<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; integer floating values whose integral=20=
part&nbsp; is&nbsp; within&nbsp; the<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; range&nbsp; of&nbsp; the&nbsp; integer=20=
type raises the inexact exception is<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unspecified.288)<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; F.5&nbsp; Binary-decimal conversion<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [#1] Conversion from the widest support=
ed IEC&nbsp; 60559&nbsp; format<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; to&nbsp; decimal with DECIMAL_DIG digit=
s and back is the identity<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; function.289)<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [#2]&nbsp; Conversions&nbsp; involving&=
nbsp; IEC&nbsp; 60559&nbsp; formats follow all<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pertinent recommended practice.&nbsp; I=
n&nbsp; particular,&nbsp; conversion<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; between&nbsp; any&nbsp; supported&nbsp;=
 IEC&nbsp; 60559&nbsp; format and decimal with<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DECIMAL_DIG&nbsp; or&nbsp; fewer&nbsp;=20=
significant&nbsp; digits&nbsp;&nbsp; is&nbsp;&nbsp; correctly<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rounded.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ____________________<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 288ANSI/IEEE&nbsp; 854,&nbsp; but&nbsp;=
 not&nbsp; IEC&nbsp; 60559&nbsp; (ANSI/IEEE&nbsp;&nbsp; 754),<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; directly&nbsp; specif=
ies&nbsp; that floating-to-integer conversions<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; raise the&nbsp; inexa=
ct&nbsp; exception&nbsp; for&nbsp; non-integer&nbsp; in-range<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; values.&nbsp;&nbsp;&n=
bsp; In&nbsp; those&nbsp; cases&nbsp; where&nbsp; it&nbsp; matters,&nbsp; li=
brary<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; functions can be used=
 to effect such conversions with&nbsp; or<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; without&nbsp; raising=
 the inexact exception.&nbsp; See rint, lrint,<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; llrint, and nearbyint=
 in &lt;math.h&gt;.<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 289If the minimum-width IEC 60559 exten=
ded format&nbsp; (64&nbsp; bits<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; of precision) is supp=
orted, DECIMAL_DIG shall be at least<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 21.&nbsp; If IEC 6055=
9 double (53 bits of&nbsp; precision)&nbsp; is&nbsp; the<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; widest IEC 60559 form=
at supported, then DECIMAL_DIG shall<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; be at least 17.&nbsp;=
 (By contrast, LDBL_DIG and&nbsp; DBL_DIG&nbsp; are<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 19 and 15, respective=
ly, for these formats.)<BR>
<BR>
</FONT></HTML>
--part1_16e.2264c067.2c67fe7c_boundary--

- Raw text -


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