[NumPy-Tickets] [NumPy] #1975: [Mixing integers and floats] : wrong results in algebraic equation
NumPy Trac
numpy-tickets@scipy....
Wed Nov 9 08:44:22 CST 2011
#1975: [Mixing integers and floats] : wrong results in algebraic equation
--------------------+-------------------------------------------------------
Reporter: bde | Owner: somebody
Type: defect | Status: new
Priority: normal | Milestone: Unscheduled
Component: Other | Version: 1.5.1
Keywords: |
--------------------+-------------------------------------------------------
I'm experiencing a strange behavior of numpy when using a ndarray filled
with integers in an algebraic equation with floats. See code snippet below
:
{{{
>>>import numpy
>>> def A(ar): return ((12200**2)*(ar**4)) / ( (ar**2+20.6**2) *
(ar**2+12200**2) * (ar**2+107.7**2)**0.5 * (ar**2+737.9**2)**0.5 )
...
>>> A(numpy.array([1,2,3])) //array with integers
array([ 2.95808751e-08, -3.77586449e-07, -1.61632406e-07]) // negative
numbers are wrong (A is never negative)
>>> A(numpy.array([1,2.0,3.0])) //array of int and floats
array([ 2.95808751e-08, 4.69917244e-07, 2.35099089e-06]) // correct!
>>> A(numpy.array([1.,2,3.0]))
array([ 2.95808751e-08, 4.69917244e-07, 2.35099089e-06]) // correct!
>>> A(1)
2.9580875078524171e-08
>>> A(1.)
2.9580875078524171e-08
>>> A(2)
4.6991724410274925e-07
>>> A(2.0)
4.6991724410274925e-07
}}}
Is it a problem related to the way numpy makes the cast from int to float
? This is strange because there is no need to have ALL the numbers
of "ar" to be float in order to make it work, one can remain int as you
can see below...
--
Ticket URL: <http://projects.scipy.org/numpy/ticket/1975>
NumPy <http://projects.scipy.org/numpy>
My example project
More information about the NumPy-Tickets
mailing list