Only in oil country...

by Codewiz51 October 22, 2008 07:57
“The meek shall inherit the earth - but not its mineral rights.”
-J. Paul Getty

Tags:

Life

DCDFLIB Cleaning Up Labels

by Codewiz51 October 19, 2008 10:34

I thought the following code clean up might be worth illustrating. In the dcdflib method beta_inc, there are a number of constructs similar to the following:

    if (x == 0.0) goto S210;
    if (y == 0.0) goto S230;
    if (a == 0.0) goto S240;
    if (b == 0.0) goto S220;

S210:
    //
    //  TERMINATION OF THE PROCEDURE
    //
    if (a == 0.0)
    {
        // x == 0 && a == 0
        ierr = 6;
        return;
    }
S220:
    w = 0.0;
    w1 = 1.0;
    return;
S230:
    if (b == 0.0)
    {
        ierr = 7;
        return;
    }
S240:
    w = 1.0;
    w1 = 0.0;
    return;

When I approach modifying this sort of code, I find it helpful to trace code execution and add comments to the code to help me keep track of the conditions that apply when a code block is executed. Here is the code with my comments added:

    // If we get to this point, we know that x + y == 1
    // a and b are not negative
    // a and b are not both zero
    if (x == 0.0) goto S210;
    if (y == 0.0) goto S230;
    if (a == 0.0) goto S240;
    if (b == 0.0) goto S220;

S210:
    //
    //  TERMINATION OF THE PROCEDURE
    //
    // At this point, all we know is x == 0.0
    // The next test will fall through to label S220 if a != 0
    if (a == 0.0)
    {
        // x == 0 && a == 0
        ierr = 6;
        return;
    }
S220:
    // Two conditions can cause this code to execute
    // x == 0 and a != 0
    // or b == 0
    // There is a timing issue. The test for b == 0 occurs after
    // 'if' statements 2 & 3 have executed.
    w = 0.0;
    w1 = 1.0;
    return;
S230:
    // At this point, all we know is y == 0.0
    // The next test will fall through to label S240 if b != 0
    if (b == 0.0)
    {
        ierr = 7;
        return;
    }
S240:
    // Two conditions can cause this code to execute
    // y == 0 and b != 0
    // or a == 0
    w = 1.0;
    w1 = 0.0;
    return;

I used my notes from the above code block to layout a new set of if test blocks. The final code block is shown below. This code is not intended to be efficient, but to exactly imitate the existing code using modern constructs:

    if (x == 0.0)
    {
        if (a == 0.0)
        {
            ierr = 6;
            return;
        }
        else
        {
            w = 0.0;
            w1 = 1.0;
            return;
        }
    }

    if (y == 0.0)
    {
        if (b == 0.0)
        {
            ierr = 7;
            return;
        }
        else
        {
            w = 1.0;
            w1 = 0.0;
            return;
        }
    }

    if (a == 0.0)
    {
        w = 1.0;
        w1 = 0.0;
        return;
    }

    if (b == 0.0)
    {
        w = 0.0;
        w1 = 1.0;
        return;
    }

This code reduces to the following:

    if (x == 0.0)
    {
        if (a == 0.0)
        {
            ierr = 6;
            return;
        }
        else
        {
            w = 0.0;
            w1 = 1.0;
            return;
        }
    }

    if ((y == 0.0) || (a == 0.0))
    {
        if (b == 0.0)
        {
            ierr = 7;
            return;
        }
        else
        {
            w = 1.0;
            w1 = 0.0;
            return;
        }
    }

    if (b == 0.0)
    {
        w = 0.0;
        w1 = 1.0;
        return;
    }
 

How can we be assured that this code faithfully reproduces the original code paths? We have to return to my notes concerning what we know about the parameters a, b, x, and y. The notes are simply paraphrases of input parameter tests performed before any calculations take place. We know that x + y = 1 and we know that if a = 0, then b cannot be 0, similarly, if b = 0, then a cannot be zero.

    // If we get to this point, we know that x + y == 1
    // a and b are not negative
    // a and b are not both zero

The code does bring up an interesting issue. Should we continue to return an ierr for backward compatibility, or should the code raise execptions when input errors occur? As always, commments are welcome.

As this topic unfolds, I'll make my notes available here.

Weekend Odds and Ends

by Codewiz51 October 18, 2008 12:41

I've managed to get another small release of dcdflib posted for download. This release introduces some code clean up for several low level routines used throughout the code.

I am also working on some new code for WinROTT. Hopefully, that will be ready this weekend.

Stuff I had to learn the hard way

by Codewiz51 October 15, 2008 21:35

When drying habanero peppers in a food dehydrator, be sure to place the food dehydrator OUTSIDE (or be prepared for the wrath of your family.)

If you accidently pass your hand over the food dehydrator exhaust while drying habanero peppers, WASH YOUR HANDS IMMEDIATELY. There is enough pepper oil in the exhaust to burn the tar out of your eyes and face.

After you dry habanero peppers in your food dehydrator, make sure to wipe down all surfaces with alcohol. There is enough residual oil on surfaces of the unit to HURT YOU.

If you grind habanero peppers in a mill, be prepared for pain. Do it outside and stand upwind from the mill. Wear chemical proof rubber gloves and a mask. Eye protection is recommended.

Freshly dehydrated habanero peppers have a lot more oil in them than the stuff you buy in stores. Be careful.

Microsoft Development Skills Assessment

by Codewiz51 October 13, 2008 22:28

My boss came across these skills assessment tests for Microsoft .Net development: Individual Assessments: Microsoft Visual Studio. I am normally not a big fan of these tests, but I think you may find them useful, both in assessing your own skills, and as potential interview questions. Some of the material is old (there were several Passport authentication questions), and there were no questions on SSL or using Smartcards for web authentication.

I found the tests to be quite humbling. Embarassed

Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen | Modified by Mooglegiant


Disclaimer

This blog represents my personal hobby, observations and views. It does not represent the views of my employer, clients, especially my wife, children, in-laws, clergy, the dog, the cats or my daughter's horse. In fact, I am not even sure it represents my views when I take the time to reread postings.

All comments are moderated for content.

© Copyright 2008-2010