English 中文(简体)
Math Overflow -- Handling Large Numbers
原标题:
  • 时间:2009-11-14 17:33:15
  •  标签:
  • math

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:

log(a*b) = log(a) + log(b)

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)...)

log(2) = 0.30103
log(3) = 0.47712
log(66) = 1.8195
log(435) = 2.6384
log(444) = 2.6474

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.

问题回答

Um: log(a*b) = log(a) + log(b)

Use Python ;)

$python
Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math.log(2*3*66*435*444)
18.15258480477539
>>>




相关问题
Maths in LaTex table of contents

I am trying to add a table of contents for my LaTex document. The issue I am having is that this line: subsubsection{The expectation of (X^2)} Causes an error in the file that contains the ...

Math Overflow -- Handling Large Numbers

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 ...

Radial plotting algorithm

I have to write an algorithm in AS3.0 that plots the location of points radially. I d like to input a radius and an angle at which the point should be placed. Obviously I remember from geometry ...

Subsequent weighted algorithm for comparison

I have two rows of numbers ... 1) 2 2 1 0 0 1 2) 1.5 1 0 .5 1 2 Each column is compared to each other. Lower values are better. For example Column 1, row 2 s value (1.5) is more ...

热门标签