I am having a problem handling large numbers.
I need to calculate the log of a very large number. The number is the product of a series of numbers. For example: log(2x3x66x435x444) though my actual series are longer.
I am getting a math overflow because product grows very large, very quickly.
Are there special math libraries to handle huge numbers? Any ideas how I can solve this?
There is a neat mathematical solution to this problem.
Rather than obtaining the product of a series by multiplying each number, you can use their log values. The noted principle is:
For the example series (2, 3, 66, 435, 444), the brute-force product is calculated as 2 * 3 * 66 * 435 * 44 = 76,483,440.
However, you can also obtain the product from the sum of the logs. For a series (n1, n2, n3, n4,…) the product of the series is: 10 ^ (log(n1) + log(n2) + log(n3) + log(n4)…)
The sum of the values is roughly 7.8835. The product of the series is 10 ^ 7.8835 (76,483,440).
Since you’re looking for the log of the product of the series, just the sum of the individual log() values, 7.8835. That’s it.