title: Generic Sqrt Implementation
body: <p>I'm using the MiscUtils library (thanks Marc G. and Jon S.) and am trying to add a generic <code>Sqrt</code> function to it.  The problem can be easily reproduced with this:</p>
  
  <pre><code>class N&lt;T&gt;
  {
      public N(T value)
      {
          Value = value;
      }
  
      public readonly T Value;
  
      public static implicit operator T(N&lt;T&gt; n)
      {
          return n.Value;
      }
  
      public static implicit operator N&lt;T&gt;(T value)
      {
          return new N&lt;T&gt;(value);
      }
  
      public static T operator /(N&lt;T&gt; lhs, T rhs)
      {
          // Operator.Divide is essentially a wrapper around 
          // System.Linq.Expressions.Expression.Divide
          return Operator.Divide(lhs.Value, rhs);
      }
  }
  
  // fails with: No coercion operator is defined 
  // between types 'System.Double' and 'N`1[System.Single]'.
  var n = new Numeric&lt;float&gt;(1f);
  var x = Operator.DivideAlternative(n, 1.0);
  
  // this works as the N&lt;T&gt; is first converted to a 
  // float via the implicit conversion operator 
  var result = n / 1.0;
  </code></pre>
  
  <p>Now, I realize <em>why</em> this is happening, but I have not yet been able to think of a way around it.  For reference, here is the current <code>Sqrt</code> implementation.  I have little experience building expression trees.</p>
  
  <pre><code>public static double Sqrt&lt;T&gt;(T value)
  {
      double oldGuess = -1;
      double guess = 1;
      while(Abs(guess - oldGuess) &gt; 1)
      {
          oldGuess = guess;
          // the first evaluated call to DivideAlternative throws
          guess = Operator.Divide(
                      Operator.AddAlternative(guess, 
                          Operator.DivideAlternative(value, guess)),
                      2);
      }
  
      return guess;
  }
  </code></pre>
  
  <p>EDIT: Ok, so I solved this on my own, but in an attempt to keep the question as simple as possible I apparently went too far and spent far too much time answering questions from confused people trying to help.</p>
  
  <p>So, this is the problem in its entirety.</p>
  
  <p>I two classes; one that performs transformations and another which performs statistical analysis of image data (pixels).  Let's focus on the latter as the problem is the same:</p>
  
  <pre><code>abstract class ImageStatistics
  {
      private readonly object _pixels;
  
      public ImageStatistics(object pixelArray)
      {
          Pixels = pixelArray;
      }
  
      // calculate the standard deviation of pixel values
      public double CalcStdDev();
  }
  </code></pre>
  
  <p>The array of pixels can be any numeric type.  In practice, it will be either <code>float</code>, <code>int</code>, <code>ushort</code>, or <code>byte</code>.  Now, because generics cannot do things like this:</p>
  
  <pre><code>public T Add&lt;T&gt;(T lhs, T rhs)
  {
      return lhs + rhs;  // oops, no operator + for T
  }
  </code></pre>
  
  <p>I cannot perform any sort of statistical analyses on the pixel values themselves without casting to the proper array type.  So, I need to have N sub-classes of <code>ImageProcessor</code> to support N pixel types.  </p>
  
  <p>Well, that sucks. I would love to just have a generic <code>ImageProcessor&lt;T&gt;</code> class which has a <code>T[]</code> of pixel data.  So, I looked into the MiscUtils library which would allow just this.  </p>