Search code examples
numbersnumerical-methodslogarithm

Is it better to compute log(x/(y*z)) or log(x)-log(y)-log(z)?


Mathematically, log(x/(y*z)) and log(x)-log(y)-log(z) are equivalent. On a computer, they will give different answers. Which is preferable to compute?


Solution

  • Log is numerical stable but division not so. Imagine y and z are very close to 0. Then y * z has a great probability to be evaluated to 0 and x / (y * z) to overflow. But log(y) even for very small y gives accurate results. So log(x)-log(y)-log(z) is expected to be more precise.