Add an optional second argument to math.log to specify the base

As a Roblox developer it is currently annoying to take the logarithm of a number with a base that is not 10 or e. This is because in Lua 5.1 there are only math.log (natural log) and math.log10 (log base 10). While logarithms have many uses, they’ve come up in my projects mostly because of needing base-2 logarithms for binary data and operations. Things like the PNG specification require certain data to be an exact power of 2, and it’s a lot easier to check that with logs than it is to do manually. However, math.log(x)/math.log(2) is a bit verbose and it isn’t immediately clear what the code does unless someone is familiar with base conversions. math.log(x, 2) is shorter and cleaner.

In Lua 5.2, a generic version of math.log was added that allowed the base to be specified as an optional second argument. Adding that version of the function to Roblox would alleviate the annoyance of converting bases and make code that does so much easier to read.

19 Likes