Jellby
Jellby

Reputation: 2694

numpy sign and fraction

What is the pitfall I'm falling into (apart from blindly applying a numpy method to some object it was probably not designed for)?

Python 3.4.3 (default, Nov 28 2017, 16:41:13) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy, fractions
>>> numpy.__version__
'1.8.2'
>>> numpy.sign(1)
1
>>> fractions.Fraction(1)
Fraction(1, 1)
>>> numpy.sign(fractions.Fraction(1))
-1
>>>

Upvotes: 0

Views: 193

Answers (1)

Anshul Goyal
Anshul Goyal

Reputation: 77023

This seems to be a bug with the particular version of numpy you are using. It works for me. Though in general, numpy may not support fractions, going through the documentation of numpy.sign, no special case about fractions is mentioned per se, only that it accepts numbers and outputs the sign.

In [14]: import numpy, fractions

In [15]: numpy.sign(1)
    ...: 
Out[15]: 1

In [16]: fractions.Fraction(1)
    ...: 
Out[16]: Fraction(1, 1)

In [17]: numpy.sign(fractions.Fraction(1))
Out[17]: 1L

Upvotes: 1

Related Questions