<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2002/12/2813f8e8be115b0bad1bc16b1e41b744"
  from="David Chase &lt;chase@w...&gt;"
  author="David Chase"
  date="2002-12-11T04:04:00"
  subject="Re: [Caml-list] float pretty-printing precision, once more."
  prev="2002/12/a76364f857529a996d6c73a20fc6f785"
  next="2002/12/17d951b73af6adf7fd25af77db4583f0"
  prev-in-thread="2002/12/2740972965db19b54afe531bdddfb74d"
  next-in-thread="2002/12/4148c2e4e503a36460efef0369cc042f"
  prev-thread="2002/12/4f26c91200b871153ac571f4e71c7d1f"
  next-thread="2002/12/b842fa0bde343ece5699d91c444d6e07"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] float pretty-printing precision, once more.">
<msg 
  url="2002/12/303524700573103127c46d2209d89f49"
  from="jeanmarc.eber@l..."
  author="jeanmarc.eber@l..."
  date="2002-12-09T23:24:23"
  subject="[Caml-list] float pretty-printing precision, once more.">
<msg 
  url="2002/12/3cb94c391a4d8d5c79e7a099c28800da"
  from="Yaron M. Minsky &lt;yminsky@C...&gt;"
  author="Yaron M. Minsky"
  date="2002-12-09T23:48:55"
  subject="Re: [Caml-list] float pretty-printing precision, once more.">
</msg>
<msg 
  url="2002/12/167a6c6cd1366f96e94bab06a3535858"
  from="Brian Hurt &lt;brian.hurt@q...&gt;"
  author="Brian Hurt"
  date="2002-12-10T00:00:02"
  subject="Re: [Caml-list] float pretty-printing precision, once more.">
</msg>
<msg 
  url="2002/12/a35e9a667edfd52c36feaaab51a66de6"
  from="David Chase &lt;chase@w...&gt;"
  author="David Chase"
  date="2002-12-10T02:14:16"
  subject="Re: [Caml-list] float pretty-printing precision, once more.">
</msg>
<msg 
  url="2002/12/197abbf7c374b28868a0af7fcee97f38"
  from="Xavier Leroy &lt;xavier.leroy@i...&gt;"
  author="Xavier Leroy"
  date="2002-12-10T09:49:32"
  subject="Re: [Caml-list] float pretty-printing precision, once more.">
</msg>
<msg 
  url="2002/12/560c45075f7edea334c281d5649ef2ac"
  from="Damien Doligez &lt;damien.doligez@i...&gt;"
  author="Damien Doligez"
  date="2002-12-10T13:09:40"
  subject="Re: [Caml-list] float pretty-printing precision, once more.">
<msg 
  url="2002/12/df3aadefe9a8021d30aa32f5dcc94da0"
  from="Jacques Carette &lt;carette@m...&gt;"
  author="Jacques Carette"
  date="2002-12-10T15:37:20"
  subject="RE: [Caml-list] float pretty-printing precision, once more.">
</msg>
<msg 
  url="2002/12/2740972965db19b54afe531bdddfb74d"
  from="Xavier Leroy &lt;xavier.leroy@i...&gt;"
  author="Xavier Leroy"
  date="2002-12-10T15:47:23"
  subject="Re: [Caml-list] float pretty-printing precision, once more.">
<msg 
  url="2002/12/2813f8e8be115b0bad1bc16b1e41b744"
  from="David Chase &lt;chase@w...&gt;"
  author="David Chase"
  date="2002-12-11T04:04:00"
  subject="Re: [Caml-list] float pretty-printing precision, once more.">
<msg 
  url="2002/12/4148c2e4e503a36460efef0369cc042f"
  from="David Chase &lt;chase@w...&gt;"
  author="David Chase"
  date="2002-12-12T01:41:48"
  subject="Re: [Caml-list] float pretty-printing precision, once more.">
</msg>
</msg>
</msg>
</msg>
</msg>
</thread>

<contents>
At 04:47 PM 12/10/2002 +0100, Xavier Leroy wrote:
&gt;If that was really the best approximation, there would be nothing to
&gt;argue.  But both 0.10000000000000001 and 0.1 read back as identical
&gt;floats, so the latter should be printed instead, but sprintf (on
&gt;Linux at least) is too stupid to realize this.

Gdtoa will supply you with the non-stupid answer, if you care
to ask it.  Again -- this I know well, I have used gdtoa to get
specification-conforming double-&gt;String conversion in a Java
VM/library.  Here is a test program that demonstrates how to
use gdtoa in "minimum unambiguous result length" mode.  This
assumes int is 32 bits long:

#include "gdtoa.h"
/* Always define to zero or one.  It is zero on Pentium. */
#define LITTLE_ENDIAN 1
FPI d_convert = { 53, 1-1023-53+1, 2046-1023-53+1, FPI_Round_near, 0};
union pun {
  double d;
  int i[2];
};
char * test_gdtoa(double d, int mode, int ndigits, int * plength, int * pdecpt) {
  union pun convert;
  convert.d = d;
  {
  int i0 = convert.i[0^LITTLE_ENDIAN];
  int i1 = convert.i[1^LITTLE_ENDIAN];
  
  int isNeg = i0 &lt; 0;
  int m0 = i0 &amp; 0xfffff;
  int e0 = ((unsigned)i0 &gt;&gt; 20) &amp; 0x7ff; 
  int int_args[8];
  char * ptr_args[1];
  char * result;
  int length;
  
  /* NaN or Inf */
  if (e0 == 0x7ff) {
    if ((m0 | i1) != 0) {
      *plength = -1;
      return "NaN";
    } else {
      *plength = -1;
      return isNeg ? "-Infinity" : "Infinity";
    }
  }
  
  /* +/- 0 */
  if ((m0 | i1 | e0) == 0) {
    *plength = -1;
    return isNeg ? "-0.0" : "0.0";
  }
  
  /* If the exponent is larger than zero,
     then the leading "1" in the mantisssa
     is implicit.  Otherwise, the number is
     denormalized, and the exponent is
     really 1. */
  
  if (e0 != 0) m0 |= 0x100000;
  else e0 = 1;
  
  /* Adjust exponent to remove bias and to
     make the mantissa all "integer", e.g.
     12345e-4 instead of 1.2345e+0 */
  e0 -= 0x3ff + 52;
  int_args[0] = i1;
  int_args[1] = m0;
  int_args[2] = STRTOG_Normal;
  result = gdtoa(&amp;d_convert, /* fpi */
                 e0,         /* be   */
                 int_args + 0, /* bits */
                 int_args + 2, /* kindP */
                 mode,         /* mode */
                 ndigits,      /* ndigits */
                 int_args + 3, /* decpt */
                 ptr_args + 0); /* rve */
  *pdecpt = int_args[3];
  length = ptr_args[0] - result;
  if (length == 0) {
      *plength = -1;
    return isNeg ? "-0.0" : "0.0";
  }
  *plength = length;
  return result;
  }
}

static test_one(double d) {
  int decpt;
  int length;
  char * result = test_gdtoa(d, 0, 0, &amp;length, &amp;decpt);

  if (length == -1) {
    printf("test of %f returns special value %s\n", d, result);
  } else {
    printf("test of %f returns regular value %s, decpt %d, length %d\n", d, result, decpt, length);
  }
}

int main(int argc, char ** argv) {
  test_one(1.0);
  test_one(1.0/0.0);
  test_one(-1.0/0.0);
  test_one(-1.0/0.0 + 1.0/0.0);
  test_one(0.0);
  test_one(-0.0);
  test_one(-1.0);
  test_one(0.25);
  test_one(4.0);
  test_one(8192.125);
  test_one(0.1);
  test_one(0.01);
  test_one(0.9);
  test_one(0.09);
  test_one(0.2);
  test_one(0.02);
  test_one(0.3);
  test_one(0.03);
}


David Chase


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

</contents>

</message>

