There has been a lot of instances where we receive a question about a mathematical formula not working correctly in a program or script they are writing. How does one go about solving such a problem? Well, I will provide some great tips to get you started here on this entry of the Programmer’s Underground!
Mathematical formulas are all around us for all sorts of things we do in life. From how long it takes a ball to fall off a bridge to how much something costs, to how much interest will be paid out over the lifetime of a loan. Sometimes we get into the hypothetical and math theory just to solve something as simple as where to place a button on a GUI interface dynamically. You don’t necessarily have to be Einstein to understand all the math out there, but you should be fully aware of how most formulas work and how to convert them into a computer equivalent. So here are a few tips you should use when converting formulas into something you need for a program
1) Know that your formula is correct – You don’t know how many times I have seen someone say “my formula is not giving out correct results”. Well probably 90% of the time it is because the formula you have is incorrect or not correct for the situation you are attempting to apply it to. Always double check that your formula is exactly the one you need to fit the problem you are trying to solve. For instance, if you are using a compound interest formula, know how to set it up based on monthly compoundings if you need monthly or how to set it up if you need yearly compounding. Don’t try to use a yearly one on a situation where you need monthly or vice versa.
2) Know your order of precedence – Another 5% percent of the time you are having problems because the formula is not evaluating in the correct order. Perhaps you have setup the formula so that a multiplication comes after an addition instead of the other way around. To combat this problem, feel free to use parenthesis as need to force evaluations. Make sure that everything is evaluating in the correct order. Even break up the lines into separate steps if need be. No one said the equation has to be all one one line. Use a variable or two in a function to hold a value temporarily (call it something besides just “temp”) and use it into another step of the formula. Some of the most complex formulas I have had to write over 4 – 10 lines going step by step. If you are following a math book, they break it down into steps, so just mimic the steps in computing.
3) Watch your syntax – Another 3% of the problems I see are with regard to syntax. Not all math formulas are going to be word for word equivalent in computing. Something like 3(5+2) in math is going to make a compiler think that “3” is a function you are trying to call with 5+2 being the parameter. You will need to add the multiplication sign and make it 3 * (5+2) and maybe need to follow step 2 and encase this all in another set of parenthesis so that it will evaluate properly if used in another equation like 4 + (3 * (5+2)).
4) Make sure that your test results are correct – The last 2% is a rare occasion but it does happen frequently, make sure the results you expect are actually correct! There has been a few times where someone will come to me and ask why something is not producing the right results only to find out that their results are actually incorrect and while the formula is working properly they try to alter the formula to fit bad results. So how do you make sure your results are right? Well, you can solve it by hand and either put the formula into a state where it is easy to solve (like if you are using x and y to find a y intercept, set x to zero and solve for y). I also like to check math books occasionally. Sometimes I will even use an example they provided to test my algorithm.
You will find that if you check the formula for correctness, know your precedence order and use it to your advantage, watch your syntax and test it against results you know for sure are correct you can accurately apply just about any equation to your programming projects. One last tip I have for you is regarding how to structure your program to help facilitate an equation. Often times I will break the equation out and put it in a separate “helper” function which I can call the parameters. This does three things for me. 1) It isolates the function so that if I need to tweak the equation I have to modify one function only and the function can be named after the equation like getQuadriatic() and 2) It simplifies the equation a bit because in that function I can go ahead and feel free to break the equation down into multiple steps and have the function still look neat. Most equation functions like this will not go beyond 10 steps so in the end the function will be complete and very elegant. 3) It makes the function perfect for reuse later. After the equation is complete, I could store it away in some archive of math functions, maybe even a new math library and use it for other projects without having to know much more than what parameters and their types I have to pass to the function. You could even build a whole other Math class if need be.
I hope this entry helps you with applying your math equations to your projects. You don’t have to be a pro with math, just know what your equations mean and how to break them down.