Andrew Que Sites list Photos
Projects Contact
Main

No articles for week of March 02, 2015

Falling back to closest date.

February 17, 2015

Scope in case statements

I ran into this scenario a several years ago on the job. This is C99 code, but it will not compile.

int value;

// ...

switch ( value )
{
  case 1:
    int accumulator = 1;
    break;

  case 2:
    int accumulator = 2;
    break;
}

It is a good practice to define and use variables as close to where they are needed and limit their scope as much as possible. My understanding is that this helps the compiler as registers optimization is easier if the life span of a variable is clearly define.

Why this code won't compile has to do with scope.  A case statement will not create a private area of scope.

Adding redundant braces fixes this problem:
int value;

// ...

switch ( value )
{
  case 1:
  {
    int accumulator = 1;
    break;
  }

  case 2:
  {
    int accumulator = 2;
    break;
  }
}

What is happening here is that the variable accumulator in the first example is within the scope of the switch statement, where as with the second example, accumulator scope is limited to the case.  The case/break statement does not define a block of scope.

I always use the redundant braces around case statements. However, when I discovered this problem, I was working for a company that didn't like the redundant braces and told me not to use them. While it was often difficult for me to abandon what I considered good coding practices, it was useful in finding "gotchas" caused by not-as-good practices.  Various companies I have worked for have different policies on coding standards.  Some were really good, but most were either non-existent or poor to the point of being useless.  While developing coding standards can become a religious war I think it is more useful to have a good standard than no standard at all.  This is especially useful for new programmers who generally have no common practices and could easily stumble on a problem like this.

1 comment has been made.

From Suck n Fail

February 25, 2015 at 11:14 AM

Interesting example of variable scope! If so much of it is poor, why do companies even bother with coding standards?

February 16, 2015

Even More Polynomial Regression Online Interface Additions

   A few days ago I added support for doing three linearizable functions to my online polynomial regression calculator.  Today I finished adding support for weighting terms.  This provides the ability to unequally weight data going into the regression calculation.
   Pictured is Desirae playing the part of the dead (but adorable) Little Blond Dame checking the door for those party-pooping lawmen.  She took a few rounds on St. Valentine's Day a few years back, but comes to visit when it is Friday the 13th.

February 15, 2015

Failing to use Taylor Series to Calculating Arcsine

Usually when I fail to accomplish some task I don't bother to write about it. However I think it might be interesting to write about some of my failed approaches to problems. Today I was looking into implementing a function to calculate the inverse sine function (arcsin) to arbitrary precision. I've done this in the past for log, exponential, sine, cosine, and pi. Right now, however, I have no inverse trigonometric functions.

The easiest way to implement any continuous function is to use it's Maclaurin series (the Taylor series centered at zero). This is easy to find for most functions, and here is the series for inverse sine:

This series suffers from a problem that many series do—it converges slowly when x is near 1 or -1. I typically address this by using the Taylor series with some different points to solve the problem. The first natural choice here is 1 and -1 since these are problem points. In order to calculate the Taylor series we need a general equation for the nth derivative. After some searching around I found this equation:

That's a mess, but we can now apply the Taylor series to this. Recall the Taylor series expansion for a function:

So we want to take the nth derivative for 1 and -1. However, it quickly becomes apparent that something bad is going to happen if we do this.

Reduce:

And that's a problem—one cannot divide by 0. Using -1 is no better because squaring negative one is just one, and results in the exact same problem.

Here my approach simply cannot be applied, and I will have to look into other methods to address the issue of slow converges. I have not taken any formal numerical analysis and lack the an understanding of the tools one would typically use to tackle this kind of problem. However that hasn't stopped me in the past. So I will need to think on it more.

   Attended a physics demonstration today with Pokie, Mike, Mira, and Mallory. It was mainly for kids, but I thought it was great. The demonstrations were all fun science and the kind of thing I use to love when I was younger. I hope it inspires some of those kids in the audience to pursue a study of science.

February 12, 2015

PHP Polynomial Regression Class 1.2 Release

   Today I finished the changes to the PHP Polynomial Regression Class and released version 1.2.  I've added two new fetchers.  First is weighted regression.  The second is the addition of linearized regression functions.  The linearized regression functions where added February 8th, 2015 as I wrote about their addition to the online calculator.  Both sets of functions were implemented quickly and the longest part was cleanup and documentation needed before release.
   My polynomial regression site is the most popular site on DrQue.net, just slightly ahead of the King's Quest Walkthrough.  As such I thought I'd use some downtime I have at work to add to the library.  There are some more fetchers I plan to add, but this is a good starting point.
   This is my old bench top power supply, and the one I fixed.  I had replaced it because I thought it was done for.  The variable power side no longer worked.  However I didn't get rid of it because it had 3 working fixed voltages: 5, 12 and -12 VDC.  This also meant I could get 24 VDC by connecting across the 12 and -12.  And should I desire such a voltage, 17 VDC by going from 5 to -12.  I have often used it as a fixed power supply.  Now that the variable side is fixed I have a range from 2-20 VDC as well.  This supply is not as versatile as my primary bench supply, but since it is now fully functional is still a good supply.

February 08, 2015

Polynomial Regression Online Interface Additions

   Updated the Polynomial Regression Online Interface and added support for doing three linearizable functions.  These are functions where simple adjustments can be made that allows them to become linear equations.  I have written about how this works in the past and a few people mentioned to be I should add this support to my online interface.  The functions I added those commonly implemented in spreadsheet software.
  • y = a eb x
  • y = a ln x + b
  • y = a xb
   Implementing the functions wasn't the problem.  Doing it in a clean manner and getting all the comment-driven documentation correct took the most time.  I'm not sure I like it fully yet, but I think the functions are ready for beta.  I'm going to clean up the package and release it, but before I do I have some more ideas for things to add.