The other day I needed a way to compute a natural logarithm, but unfortunately neither Papyrus nor SKSE seem to offer this function. So I looked around for a way to evaluate or at least approximate the result, which led me to http://en.wikipedia.org/wiki/Logarithm#Power_series . The first method mentioned there (Taylor series) only works between 0 and 2, so I focused on the arctanh method. Of course, Papyrus doesn't provide arctanh either, so this is what I ended up implementing:
Float Function ApproximateNaturalLog(Float x, Float precision) precision *= 2 ; since we double the result at the end Float term = (x - 1) / (x + 1) Float step = term * term Float result = term Float divisor = 1 Float delta = precision While delta >= precision term *= step divisor += 2 delta = term / divisor result += delta EndWhile Return 2.0 * resultEndFunction
The idea is that it will continue evaluating smaller and smaller elements of the series until the difference it makes to the overall result gets small enough to be ignored.
This seems to work okay so far, but I wonder if there's some better way to go about this. Has anyone else had to compute logarithms in Papyrus before? How did you do it?