title: Why does "dtoa.c" contain so much code?
body: <p>I'll be the first to admit that my overall knowledge of low level programming is a bit sparse. I understand  many of the core concepts but I do not use them on a regular basis.
  That being said I was absolutely astounded at how much code was needed for <a href="http://www.netlib.org/fp/dtoa.c" rel="noreferrer">dtoa.c</a>.</p>
  
  <p>For the past couple months I have been working on an ECMAScript implementation in C# and I've been slowing filling in the holes in my engine. Last night I started working on <strong>Number.prototype.toString</strong> which is described in section <strong>15.7.4.2</strong> of the <a href="http://es5.github.io/#x15.7.4.2" rel="noreferrer">ECMAScript specification</a> <a href="http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf" rel="noreferrer">(pdf)</a>. In section <strong>9.8.1</strong>, NOTE 3 offers a link to <em>dtoa.c</em> but I was looking for a challenge so I waited to view it. The following is what I came up with. </p>
  
  <pre><code>private IDynamic ToString(Engine engine, Args args)
  {
      var thisBinding = engine.Context.ThisBinding;
      if (!(thisBinding is NumberObject) &amp;&amp; !(thisBinding is NumberPrimitive))
      {
          throw RuntimeError.TypeError("The current 'this' must be a number or a number object.");
      }
  
      var num = thisBinding.ToNumberPrimitive();
  
      if (double.IsNaN(num))
      {
          return new StringPrimitive("NaN");
      }
      else if (double.IsPositiveInfinity(num))
      {
          return new StringPrimitive("Infinity");
      }
      else if (double.IsNegativeInfinity(num))
      {
          return new StringPrimitive("-Infinity");
      }
  
      var radix = !args[0].IsUndefined ? args[0].ToNumberPrimitive().Value : 10D;
  
      if (radix &lt; 2D || radix &gt; 36D)
      {
          throw RuntimeError.RangeError("The parameter [radix] must be between 2 and 36.");
      }
      else if (radix == 10D)
      {
          return num.ToStringPrimitive();
      }
  
      var sb = new StringBuilder();
      var isNegative = false;
  
      if (num &lt; 0D)
      {
          isNegative = true;
          num = -num;
      }
  
      var integralPart = Math.Truncate(num);
      var decimalPart = (double)((decimal)num.Value - (decimal)integralPart);
      var radixChars = RadixMap.GetArray((int)radix);
  
      if (integralPart == 0D)
      {
          sb.Append('0');
      }
      else
      {
          var integralTemp = integralPart;
          while (integralTemp &gt; 0)
          {
              sb.Append(radixChars[(int)(integralTemp % radix)]);
              integralTemp = Math.Truncate(integralTemp / radix);
          }
      }
  
      var count = sb.Length - 1;
      for (int i = 0; i &lt; count; i++)
      {
          var k = count - i;
          var swap = sb[i];
          sb[i] = sb[k];
          sb[k] = swap;
      }
  
      if (isNegative)
      {
          sb.Insert(0, '-');
      }
  
      if (decimalPart == 0D)
      {
          return new StringPrimitive(sb.ToString());
      }
  
      var runningValue = 0D;
      var decimalIndex = 1D;
      var decimalTemp = decimalPart;
  
      sb.Append('.');
      while (decimalIndex &lt; 100 &amp;&amp; decimalPart - runningValue &gt; 1.0e-50)
      {
          var result = decimalTemp * radix;
          var integralResult = Math.Truncate(result);
          runningValue += integralResult / Math.Pow(radix, decimalIndex++);
          decimalTemp = result - integralResult;
          sb.Append(radixChars[(int)integralResult]);
      }
  
      return new StringPrimitive(sb.ToString());
  }
  </code></pre>
  
  <p>Can anyone with more experience in low level programming explain why <em>dtoa.c</em> has roughly 40 times as much code? I just cannot imagine C# being that much more productive.</p>