Friday, March 19, 2021

What mathematics do you need to know for computer programming?

Computer programming has, over the last couple of decades, quite quickly become easily available and feasible for all computer users. Many a beginner programmer becomes fascinated at the prospect of creating programs of their own, especially games.

However, quite soon they encounter several hurdles along the way. Computer programming is not necessarily as simple and easy as it might sound. The fancier results you want, the more you need to know. Not only do you need to know the programming language you want to use and the various libraries available to do the necessary tasks to achieve the desired goal, but programming requires higher-level "meta-knowledge" about computing science, such as practical knowledge about algorithms data containers and... math.

Indeed, math is quite ubiquitous in most computer programming, and especially in game programming. Modern tools and game engines might make this easier and remove the burden of coming up with the math necessary for the most common actions that happen in a video game, but sooner or later the requirement of some math knowledge will be encountered.

Many a beginner programmer has found out the hard way that their dismissal of math in elementary and high school as "boring", "unnecessary" and "I will never need any of this in real life" comes to bite them in the posterior once they try their hands at computer programming. Quite soon they find themselves searching the internet for the basic math knowledge that they did not pay attention to in school.

But the question arises: What kind of math is needed and useful in programming, especially game programming? If one had to concentrate on the most useful parts of math, what would it be, and what could be skipped?

I have thought of this question quite many times, and I have come up with a list:

Rather obviously, basic arithmetic is absolutely ubiquitous in everything, including programming. However, this should not stop just at the primary school level arithmetic, as doing arithmetic in bases other than base-10 becomes surprisingly important knowledge in programming. Most importantly having a good grasp of base-2 (ie. binary) and base-16 arithmetic, and the conversions between all three bases, is of crucial importance.

Linear algebra is quintessential to game programming (and almost any programming that involves any sort of graphics). Having a good grasp of linear algebra is extremely useful. In fact, having a good grasp of elementary algebra in general helps a lot.

Very closely related is analytic geometry, which use in video game programming ought to be quite self-evident.

Likewise trigonometry will quite often turn out very useful. All of these share in common that they deal with calculations done in two and three-dimensional Cartesian space, which is what computer games (and almost any program dealing with graphics) deal with.

An example of a field of math that, in my experience, is very rarely if ever needed in practical programming is calculus. You can probably skip all of calculus completely and it's unlikely you'll find that as a hindrance in the vast majority of programming. (There may be some very specialized programs for very specialized tasks that may require calculus, but they tend to be very rare and far-removed from most common programming needs.) Of course it's good to know at least something about calculus for general knowledge, to have at least a grasp of what it's about and the generic principles, but in my experience being good at calculus is completely unnecessary in the vast, vast majority of programming tasks.

(Perhaps the closest thing you may ever need calculus for in graphical programming is if the need arises to find out a formula for the slope of a function, which may be needed for things like raytracing, but even then this tends to be very niche. At a very minimum one should know that a slope function can be calculated using calculus, even if one doesn't know the exact method for deriving it. This knowledge will at least guide you to the right direction, if the need ever arises.)