473,378 Members | 1,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

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
2 2017
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: thecrow | last post by:
Alright, what the hell is going on here? In the following code, I expect the printed result to be: DEBUG: frank's last name is burns. Instead, what I get is: DEBUG: frank's last name is...
220
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
92
by: Reed L. O'Brien | last post by:
I see rotor was removed for 2.4 and the docs say use an AES module provided separately... Is there a standard module that works alike or an AES module that works alike but with better encryption?...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.