By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

what's the reasonale of loghelper() in mathmodule.c

P: n/a
Hi all,

I downloaded Python-2.5.2.tar.bz2 and want to lean some math function
implementation in it. I found that mathmodule.c implements 'math'
module of python. In this file there's a function loghelper()(in
Python-2.5.2/Modules/mathmodule.c), it seems with this function's help
any base logarithm can be computed, its comments state as:

/* A decent logarithm is easy to compute even for huge longs, but libm
can't
do that by itself -- loghelper can. func is log or log10, and name
is
"log" or "log10". Note that overflow isn't possible: a long can
contain
no more than INT_MAX * SHIFT bits, so has value certainly less than
2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is
small enough to fit in an IEEE single. log and log10 are even
smaller.
*/

static PyObject*
loghelper(PyObject* args, double (*func)(double), char *format,
PyObject *arg)
{
.......
}
I cannot understand it well, can anyone explain me why and how
loghelper() can compute any base logarithm? Or could anyone give me
some reference(such as, books or papers)?

Thanks in advance,

-Rockins Chen
Apr 9 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Apr 9, 4:38*am, rockins <ybc2...@gmail.comwrote:
I cannot understand it well, can anyone explain me why and how
loghelper() can compute any base logarithm? Or could anyone give me
some reference(such as, books or papers)?
loghelper is there so that log(n) can be computed for any positive
integer n---it's nothing to do with computing logs to an arbitrary
base.

All of the other math functions convert an integer argument to a float
first. That conversion fails if the integer is larger than the
largest
representable float (around 1.7e308 on most systems). For example:
>>from math import sqrt, log
sqrt(10**600)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to float
>>log(10**600)
1381.5510557964274

The sqrt call first tries to convert 10**600 to a float, giving an
OverflowError (even though the actual square root *is* representable
as a float). The log call goes through loghelper instead, which
doesn't try to convert 10**600 to a float, but instead computes
the log based on the top few bits of 10**600 (in its internal
binary representation) and on the number of bits required to
represent 10**600.

You're not going to learn much about math function implementations
from mathmodule.c: all it does it wrap the platform libm functions.

Mark
Apr 9 '08 #2

P: n/a
On Apr 9, 9:11 pm, Mark Dickinson <dicki...@gmail.comwrote:
On Apr 9, 4:38 am,rockins<ybc2...@gmail.comwrote:
I cannot understand it well, can anyone explain me why and how
loghelper() can compute any base logarithm? Or could anyone give me
some reference(such as, books or papers)?

loghelper is there so that log(n) can be computed for any positive
integer n---it's nothing to do with computing logs to an arbitrary
base.

All of the other math functions convert an integer argument to a float
first. That conversion fails if the integer is larger than the
largest
representable float (around 1.7e308 on most systems). For example:
>from math import sqrt, log
sqrt(10**600)

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: long int too large to convert to float>>log(10**600)

1381.5510557964274

The sqrt call first tries to convert 10**600 to a float, giving an
OverflowError (even though the actual square root *is* representable
as a float). The log call goes through loghelper instead, which
doesn't try to convert 10**600 to a float, but instead computes
the log based on the top few bits of 10**600 (in its internal
binary representation) and on the number of bits required to
represent 10**600.

You're not going to learn much about math function implementations
from mathmodule.c: all it does it wrap the platform libm functions.

Mark
Thanks Marks, your explanation is very clear. Yes, I do not need to
learn much about the math functions' implementation of libm, I just
need to know why there exists loghelper() in mathmodule.c; I have
checked Python's longobject.c, I think I have understood why
loghelper() is needed now. Since Python can represent arbitrary
precision long integer object, so for logarithm it indeed need to deal
with very huge number. Doesn't it? If there's any misunderstanding of
mine, please point out. Thank you very much.

-rockins
Apr 10 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.