Saturday, October 1, 2011

logarithm of large number - it is not about computation!

Computer programmers who have not studied Mathematics beyond elementary level, often trip over this one.

Perhaps you asked yourself one of these questions:
Why does Gnu Gmp library not have a logarithm function?

What is the natural log of a large number such as 6775471000000000?

Rules of logs - product and powers:

For calculating logarithms of gigantic numbers - the standard ANSI C library is probably all that you need.

That computational tool, needs to be supplemented, with two pieces of Mathematical knowledge as illustrated here:

Images courtesy of Wikipedia (Creative Commons Licensed)

The number I mentioned above 6775471000000000 is too large for entry into a school standard calculator. Ten digits or so is the most you can enter.

Rewrite the number as 6775471 times 'a thousand million' and go that way.

Using the product of logs rule (graphic above) and doing the two calculations we see that:

log(6775471) = 15.728819  to 6 decimal places


log(1,000,000,000) = 20.723266 to 6 decimal places

Add the two answers together gives 36.452085 to 6 decimal places

Alternatively for the second piece of the addition you could have used 9log(10) by utilising the second rule - power of logs.

But my number is huge and does not contain a long stream of zeros?

This is all about precision. Do you really need more than say 6 or 8 decimal places?

Avoid creating artificial conditions. Unless you are working with numerical methods and/or in Engineering, then you will probably answer NO to what I just asked.

So your number is 6775471 followed by another 75 digits (some zero some not)

If you only need 6 digits of precision in your answer, then it matters not what those 75 digits contain, simply pretend they are zero, and either adapt my method shown or...

...use the 2nd log rule from the image above.
( hint: You might want to use 75log(10) as part of your workings. )

Answer Guess: Around 187.nnnnnn sounds about right.

Prime number searching should not be limited to just Mathematicians


However looking up some rules of logarithms by reading this page or looking on Wikipedia is not hard.

So quit moaning that 'such and such a library' does not have a function for logarithms of huge integers, and take five minutes to do a little addition and subtraction.

Often Ansi C coders first go searching for log() of huge integer functions, when they are writing prime number search programs, which is why I mention it.

Notes and Further Reading:

Laws of Logarithms is part of the "Core 2 (C2)" Curriculum for post compulsory education in the UK (A level) .

If you studied Engineering at University, then you will have encountered Laws of Logarithms twice - once as part of 'A Level' Mathematics, and once as part of your Engineering course.

All the Logarithms in this article are 'Natural Log'. Your calculator might show that as 'ln' depending on the brand.

The Laws of Logarithms apply similarly to base 10 logs - but you would obtain different decimal answers, than those tabulated above.

The Index page for that C2 book shows pages 43 and 44 cover 'multiplication law' & 'power law' for Logarithms.

No comments:

Post a Comment