shunwang / numexpr

Automatically exported from code.google.com/p/numexpr
MIT License
0 stars 0 forks source link

Raising array containing 0s to a negative power causes core dump in numexpr 1.4 #37

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago

What steps will reproduce the problem?
1. import numexpr
2. a = [-1, 0, 1]
3. numexpr.evaluate('a**-1')
Floating point exception (core dumped)

I am using numexpr version 1.4 on python:

Python 2.5.4 (r254:67916, Jun 11 2009, 16:53:32)

On linux.

Original issue reported on code.google.com by tom.denn...@gmail.com on 15 Nov 2010 at 3:18

GoogleCodeExporter commented 9 years ago
In fact even 1/a where a is an array will display the issue:

import numexpr
a = [-1, 0, 1]
numexpr.evaluate('1/a')

Original comment by tom.denn...@gmail.com on 15 Nov 2010 at 3:22

GoogleCodeExporter commented 9 years ago
I had the same problem, so I fixed the issue with this small patch. It 
introduces the same behavior than numpy for those situations, that is: x/0 
gives 0 (when x is an integer). Ideally this should be configurable, but 
getting 0 is already much better than a crash.

Original comment by gdemen...@gmail.com on 16 Nov 2010 at 9:26

Attachments:

GoogleCodeExporter commented 9 years ago
Interesting.  I see the issue only happens with ints.  I didn't realize that 
before.  My version of numpy, however, seems to give different results.  It 
seems to give a large negative number for 1/0 which i guess is some attempt at 
replicating the "1/0 = inf" that you get for floats.

I agree that anything would be better than a segfault though.  I would, 
however, prefer to match numpy if possible.

Thanks so much for you're patch.  I really appreciate the help,
Tom

In [10]: a = numpy.array([1, 0, 1])
In [11]: a**(-1)
Out[11]: array([                   1, -9223372036854775808,                    
1])

Original comment by tom.denn...@gmail.com on 16 Nov 2010 at 3:01

GoogleCodeExporter commented 9 years ago
Fixed in r260.

Thanks!

Original comment by fal...@gmail.com on 16 Nov 2010 at 4:43

GoogleCodeExporter commented 9 years ago
Tom, I don't have python/numpy installed on this machine but both numpy docs 
and my recalling of the tests I did a few weeks ago (using straight divisions) 
go in the same direction...

http://docs.scipy.org/doc/numpy/reference/generated/numpy.divide.html#numpy.divi
de

The difference probably comes from the optimization that numexpr does IIRC: 
a**(-1) -> 1 / a while numpy might use another algorithm. There is an argument 
you can pass to evaluate to turn off "optimization", you might want to try that.

Original comment by gdemen...@gmail.com on 16 Nov 2010 at 7:53

GoogleCodeExporter commented 9 years ago
Not clear to me why my install has behavior that doesn't match the docs but any 
behavior that isn't a segfault is fine with me.  Looks like your patch has 
already been included, so i will try it.

Thanks again,
Tom 

Original comment by tom.denn...@gmail.com on 17 Nov 2010 at 3:13