public static double Log(double x, double newBase) { // Based on Python sourcecode from: // http://en.literateprograms.org/Logarithm_Function_%28Python%29 double partial = 0.5; double integer = 0; double fractional = 0.0; double epsilon = 2.22045e-16; if (x == 0.0) return double.NegativeInfinity; if ((x < 1.0) & (newBase < 1.0)) throw new ArgumentOutOfRangeException("can't compute Log"); while (x < 1.0) { integer -= 1; x *= newBase; } while (x >= newBase) { integer += 1; x /= newBase; } x *= x; while (partial >= epsilon) { if (x >= newBase) { fractional += partial; x = x / newBase; } partial *= 0.5F; x *= x; } return (integer + fractional); }
Now, I am using the above Log code for the following function:
static float calc_ev( float lux, int iso ) { // calculate EV using APEX method: // Ev = Av + Tv = Bv + Sv //lux = 19.6F; //iso = 100; // We'll use the right-hand side for this operation // Bv = log2( B/NK ) // Sv = log2( NSx ) float Sv = (float)(Log((float)0.3 * iso,10) / Log(2,10)); float Bv = (float)(Log((lux / (float) 0.3 * (float)14),10 ) / Log(2,10)); return( (float)(Bv + Sv)); }
If I run calc_ev( 19.6F, 100 ), the result is 14.7439928
Now, if I create a Windows Forms project, and use the built-in Math.Log10 function for the following function which is like the function above:
private float calc_ev(float lux, int iso) { // calculate EV using APEX method: // Ev = Av + Tv = Bv + Sv // We'll use the right-hand side for this operation // Bv = log2( B/NK ) // Sv = log2( NSx ) float Sv = (float)(Math.Log10((float)0.3 * iso) / Math.Log10(2)); float Bv = (float)(Math.Log10((lux / (float)0.3 * (float)14) / Math.Log10(2))); return ((float)(Bv + Sv)); }
With the Windows Forms app, the result for calc_ev(19.6F, 100) is 8.389544
Why are the results different? I expected the two results to be the same. I need an accurate Log value in .Net MF. The proper value is the 8.389544 value, so something wrong is happening in .Net MF. What am I doing wrong?