Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Math.Log issues


  • Please log in to reply
5 replies to this topic

#1 monewwq1

monewwq1

    Advanced Member

  • Members
  • PipPipPip
  • 104 posts

Posted 26 September 2011 - 02:09 AM

So, I am trying to use the Log function at the bottom of this page. So here is the Log code I am using:

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?

#2 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 26 September 2011 - 03:51 AM

The first function is (let's say) "limited", and not reliable. I've rewritten some better math functions in C#, even I never published them because I think they should have to implemented as native code. Hold on a couple of hours, then I'll be in the office, so I post my functions. Cheers
Biggest fault of Netduino? It runs by electricity.

#3 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 26 September 2011 - 07:04 AM

Here is the (unfinished) math library. It is a standard console application, to test the math function (respect to the PC built-in), and to compare with the Grommet's library. Feel free to improve, but I'm still unsure if all that is worthwhile...

Attached Files


Biggest fault of Netduino? It runs by electricity.

#4 Stefan W.

Stefan W.

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts

Posted 26 September 2011 - 08:53 AM

float Sv = (float)(Math.Log10((float)0.3 * iso) / Math.Log10(2));
This (and the corresponding code in your first version) is redundant - that's just the binary logarithm of the first argument.
I believe that no discovery of fact, however trivial, can be wholly useless to the race, and that no trumpeting of falsehood, however virtuous in intent, can be anything but vicious.
-- H.L. Mencken, "What I Believe"

#5 monewwq1

monewwq1

    Advanced Member

  • Members
  • PipPipPip
  • 104 posts

Posted 26 September 2011 - 01:20 PM

float Sv = (float)(Math.Log10((float)0.3 * iso) / Math.Log10(2));
This (and the corresponding code in your first version) is redundant - that's just the binary logarithm of the first argument.


That is because I need to calculate Log2 to get the Film Sensitivity. The actual equation I am trying to calculate is:

Film sensitivity (Sv) = log2( ASA / 3.125 )
(see APEX units)

This code is ported from Arduino code and Arduino does not have a built-in Log2 function, hence to get Log2, the code uses the fact that the logarithm of any base other than the natural is the natural logarithm divided by the natural logarithm of the base.

In C#, I could indeed re-write that line as:

float Sv = (float)(Math.Log((float)0.3*iso,2));

But I still don't have an answer as to why Math.Log returns the expected value, but that other .Net MF code does not. I may just have to solve and then divide by 2 for now.

#6 monewwq1

monewwq1

    Advanced Member

  • Members
  • PipPipPip
  • 104 posts

Posted 26 September 2011 - 02:11 PM

Ok, this:

            float Sv = (float)(Math.Log((float)0.3*iso,2));
            float Bv = (float)Math.Log(lux / (float)0.3 * (float)14, 2);
            return ((float)(Bv + Sv));

returns the same 14.7439928 value I am getting from the .Net MF version of Log. So it looks like initially I wrote the Windows Forms function incorrectly.

14.7439928 is not the value I expected, but that is a whole other issue. :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.