www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - D gets the rounding right, too

http://www.reddit.com/r/programming/comments/cb8qv/incorrectly_rounded_conversions_in_gcc_and_glibc/


0.500000000000000166533453693773481063544750213623046875
  Correct = 0x1.0000000000002p-1
  dmd =     0x1.0000000000002p-1
  strtod =  0x1.0000000000002p-1

3.518437208883201171875e13
  Correct = 0x1.0000000000002p+45
  dmd =     0x1.0000000000002p+45
  strtod =  0x1.0000000000002p+45

62.5364939768271845828
  Correct = 0x1.f44abd5aa7ca4p+5
  dmd =     0x1.f44abd5aa7ca4p+5
  strtod =  0x1.f44abd5aa7ca4p+5

8.10109172351e-10
  Correct = 0x1.bd5cbaef0fd0cp-31
  dmd =     0x1.bd5cbaef0fd0cp-31
  strtod =  0x1.bd5cbaef0fd0cp-31

1.50000000000000011102230246251565404236316680908203125
  Correct = 0x1.8p+0
  dmd =     0x1.8p+0
  strtod =  0x1.8p+0

9007199254740991.4999999999999999999999999999999995
  Correct = 0x1.fffffffffffffp+52
  dmd =     0x1.fffffffffffffp+52
  strtod =  0x1.fffffffffffffp+52

and the code:


import std.c.stdio;
import std.c.stdlib;

const NUM_CONVERT = 6;

int main ()
{
  uint i;

  char decimal[NUM_CONVERT][60] = [
   "0.500000000000000166533453693773481063544750213623046875",
   "3.518437208883201171875e13",
   "62.5364939768271845828",
   "8.10109172351e-10",
   "1.50000000000000011102230246251565404236316680908203125",
   "9007199254740991.4999999999999999999999999999999995"
  ];

  double gcc_conversion[NUM_CONVERT] = [
   0.500000000000000166533453693773481063544750213623046875,
   3.518437208883201171875e13,
   62.5364939768271845828,
   8.10109172351e-10,
   1.50000000000000011102230246251565404236316680908203125,
   9007199254740991.4999999999999999999999999999999995
  ];

  char correct[NUM_CONVERT][30] = [
   "0x1.0000000000002p-1",
   "0x1.0000000000002p+45",
   "0x1.f44abd5aa7ca4p+5",
   "0x1.bd5cbaef0fd0cp-31",
   "0x1.8p+0",
   "0x1.fffffffffffffp+52"
  ];

  for (i=0; i < NUM_CONVERT; i++)
  {
   printf ("%s\n",decimal[i].ptr);
   printf (" Correct = %s\n",correct[i].ptr);
   printf (" dmd =     %a\n",gcc_conversion[i]);
   printf (" strtod =  %a\n\n",strtod(decimal[i].ptr,null));
  }
  return 0;
}
Jun 06 2010