# Programming and Math

If you're a programmer of any quality, I think you also need to have a firm grasp of mathematics. For at least 95% of the cases (I submit that there are some great programmers who really aren't good at math-in-general), there's just no escaping it.I answered a question a while back on Yahoo Answers. The person asked:

When are we ever going to need math like Algebra, Geometry, Calculus, etc in life?

They continued with saying how they plan to go into massage therapy. My response:

Math builds analytical and problem solving skills. That's the simplest explanation for why I think everyone should take up to Algebra 2.

You're right, -you- as a massage therapist probably won't use the math you're learning right now very often. There might be related topics in anatomy and so forth, I don't really know. But I as a programmer use math all the time, especially what you learn in algebra 2 (covered in more detail with linear algebra). "We" need math as a species, but that doesn't mean every individual needs math to the same level. Also note that the math in high school is meant to be foundational--it's used as a tool later on, rather than the actual subject.

Your question is like asking "When are we going to need a hammer in life?" when all you've been trained to do is pound nails in boards. The answer is -you- aren't likely to need the hammer if you don't do much construction, but others will need it to help build a house, and humanity isn't going to get on nicely without houses.

A programmer who isn't using math, isn't really programming. A programmer who doesn't even understand boolean algebra, isn't a good programmer. The people who

*do nothing else*but make websites aren't good programmers, either. There's a reason for the stigma web developers have: they're on average horrible. It's only after learning much can one come back and be decent at web application designing, and even then strong math skills are highly sought after. The most interesting web work is when your application could really run on anything, but the web is just your interface.

Observe the DigiPen list of courses for an RTIS degree (the traditional one they've offered). It requires 7 math courses, most of them above the level you can get in high school (and even then only with AP classes). If that isn't evidence that

**you need math to do game programming**, I don't know what is.

Just even consider the

*trivial*application of reflecting a projectile off a circular object. One simplistic thing to do is say "angle in, angle out" with respect to the normal from the center of the circle to the line tangent with the collision point, recalling simple physics for light waves reflecting off flat mirrors. But dealing with angles in code is annoying. How would one do the reflection, and can it be done without using angles? Well, if you've studied linear algebra, the answer is

*immediately*obvious: use a reflection matrix.

def normalize(vec):

"Normalize 2D vectors"

mag = 1.0 * sqrt(vec[0]**2 + vec[1]**2)

return [vec[0] / mag, vec[1] / mag]

def reflection(normal):

"Returns the reflection matrix in the line ax + by = 0"

a = normal[0]

b = normal[1]

frac = 1.0 / (a**2 + b**2)

row1 = [(b**2 - a**2) / frac, -2*a*b / frac]

row2 = [row1[1], (a**2 - b**2) / frac]

return [row1, row2]

def matrix_mult(A, x):

"Returns the result of multiplying matrix A with vector x"

row1 = A[0][0] * x[0] + A[0][1] * x[1]

row2 = A[1][0] * x[0] + A[1][1] * x[1]

return [row1, row2]

...

def reflect_bullet(self, bullet):

vec2bullet = [

bullet.rect.centerx - self.rect.centerx,

bullet.rect.centery - self.rect.centery

]

# Reverse the y so that the normal points out from the circle.

# (It would in math world, but rect points are relative to screen.)

vec2bullet[1] = -vec2bullet[1]

A_r = reflection(vec2bullet)

A_rx = matrix_mult(A_r, bullet.direction)

bullet.direction = normalize(A_rx)

That's a trivial implementation. I recommend using a vector and/or matrix class instead of hardcoding it directly like that, though.

The whole idea of vectors is fundamental to game programming, too.

What about other software programming? Clearly video game programming requires math. Web programming doesn't require as much, but it definitely has lots of logic requirements (especially when you're using SQL and making

*interesting*applications instead of trivial ones), and learning math definitely improves your ability to reason and to think logically about something. But overall web programming is fundamentally easier than other forms of programming, so it's not a strong indicator of the strength of a programmer. Any programming dealing with algorithms, encryption, compression, optimization, audio manipulation, AI, and reasoning will benefit from the programmer who has a strong math background. I submit some web applications will have these features, but that's not what is traditionally meant by web application. For those I'm talking about forums, e-commerce sites, and blogs. Twitter, Facebook, Ebay. Google search is a web application but you can bet they have a lot of neat math behind the scenes, because fundamentally it's a searching program with a web interface.

At Coding Horror, Jeff has a nice post and a bunch of good comments on this issue. No, you don't need to know every math under the sun to be good at programming, but knowing more math is a

*very effective*way of increasing your skill. Abstraction is certainly a necessary programming skill, and I'm confident math helps with that as well. Physics is another useful course.

I think just-in-time learning is insanely useful, but I agree with this commenter that it's not all-important:

"Maybe I'm a hopeless optimist, but I think most programmers are smart enough to learn whatever math they need just in time to attack the problem at hand."

I would dispute that. I have observed that learning math concepts is not just like learning a piece of information - it takes time, sometimes even years for your brain to familiarize with mathematical concepts.

Also, very often there are mathematical tools you could apply for a given programming problem - if only you knew about the existence of these tools. I'm not talking about 3D rendering engines here but more like everyday business applications. Understanding propositional and predicate logic can help a lot implementing common business rules. And it's not a complex concept at all but still it takes some time to figure it out and it is a part of CS undergraduate programmes for a reason.

Math is supposed to be challenging, and only a lucky few can internalize it very quickly.

Must all programmers learn calculus? No, but it will help. Not nearly as much as other branches of math, but still. Just-in-time learning is also useless when you have no idea where to look. Say you need to find the area of a square. If you're in elementary, you might go a brute force way and count up all the little square-units that make up the big square. If you've had geometry, you know that A = s^2, and you're done. You also learned a bunch of useful equations for areas and volumes of well-defined shapes, and even if you can't remember them all right now you know where to look if you ever came across such a problem.

So you come to another problem of finding the volume of a curvy, 3D object. You're lost. You try and fit well-defined shapes into it, like spheres and cubes and pyramids, and add up the areas of all those things you can squeeze in there. A very brute force way, and you'll get an approximate answer at best. But if you've had calculus, you'll realize "Hey, we did something like this. I can fairly simply get an exact answer." I don't remember the exact formulas and methods (it was my least favorite part of calculus), but if I run into this problem it wouldn't be hard for me to look up.

People with no calculus wouldn't even likely imagine to use calculus, and if they did, calculus isn't something you can learn "just in time" if you've had no experience with it before.

Being good at math does not directly cause being good at programming, but for people who practice both arts, there's a high correlation. More and more though, the two fields are becoming mutually dependent. Mathematicians use computers for proof-generating and other complicated mathy things, but programming it isn't a big deal. Programmers use math to help them think about things better, to provide a larger toolbox, and to solve many specific problems. The programming is the main thing, not the math, but the math makes the programming better.

Where am I? I don't consider myself a great programmer, but I consider myself on the path. I'm generally good with math, and I'm generally good with programming, and both skills are increasing. If you're not improving, you're not a good programmer, period.

#### Posted on 2009-12-28 by Jach

**Tags:** math, programming

**Permalink:** https://www.thejach.com/view/id/65

**Trackback URL:** https://www.thejach.com/view/2009/12/programming_and_math

## Recent Posts

2019-02-092019-02-092018-09-142018-08-152018-08-14