do computation in log to prevent overflow