Added more overflow protection with logs/exps

lnVocab was calculated unsafely in one instance, and we had no safety
for log(1+exp(x))

Put all these overflow-safe functions into their own module.