OverflowError: math range error ???

Tim Peters tim.one at comcast.net
Fri May 31 14:26:30 EDT 2002


[Shagshag13]
> What does it mean "OverflowError: math range error" ???

It means math.log is unhappy.  When facing an error like this, break
complicated expression into multiple lines and the cause will soon become
obvious.

> I get this:
>
> Traceback (most recent call last):
>   File "./build_index.py", line 91, in ?
>     ii.updateWeight()
>   File "./invertedindex.py", line 109, in updateWeight
>     idf = ( math.log( (N + 1) / (df + 1) ) / math.log(2) + 1 )
> OverflowError: math range error

My guess is that N and df are both ints, and that N < df.  Then integer
division truncates (N + 1) / (df + 1) to 0, and math.log(0) isn't defined
(it "overflows" to minus infinity).  Try rewriting as

idf = int(math.log((N + 1.0) / (df + 1.0)) * INVLOG2 + 1)

where

INVLOG2 = 1.0 / math.log(2)

is at module scope.

However, it's probably a logic error somewhere else in your code if N *is*
less than df at the time you compute idf (you can't very well have a term
appearing in more documents than the total number of documents ...).






More information about the Python-list mailing list