Mailing List Archive

Bug report
I found the following bug (python 3.9.1) when multiplying an array by
several variables without parentheses; look at the following example:

import numpy as np

NR = 0.25
N = 60461826

initialINCIDENCE = np.array([1, 50, 100, 150, 200, 250, 300])
initialINCIDENCE = initialINCIDENCE*N/(100000*7*NR)
print('First result ' +str(initialINCIDENCE))

initialINCIDENCE = np.array([1, 50, 100, 150, 200, 250, 300])
initialINCIDENCE = initialINCIDENCE*(N/(100000*7*NR))
print('Second result ' +str(initialINCIDENCE))

The result given is:

First result [ 345.49614857 -7267.86283429 10006.94459429 2739.08176
-4528.78107429 -11796.64390857 5478.16352 ]
Second result [ 345.49614857 17274.80742857 34549.61485714
51824.42228571 69099.22971429 86374.03714286 103648.84457143]

Clearly both are different, and in particular the first one has no sense to
me.
Thank you,

Carla.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Bug report [ In reply to ]
On 24/02/2021 20:36, Carla Molina wrote:
> I found the following bug (python 3.9.1) when multiplying an array by
> several variables without parentheses; look at the following example:
>
> import numpy as np
>
> NR = 0.25
> N = 60461826
>
> initialINCIDENCE = np.array([1, 50, 100, 150, 200, 250, 300])
> initialINCIDENCE = initialINCIDENCE*N/(100000*7*NR)
> print('First result ' +str(initialINCIDENCE))
>
> initialINCIDENCE = np.array([1, 50, 100, 150, 200, 250, 300])
> initialINCIDENCE = initialINCIDENCE*(N/(100000*7*NR))
> print('Second result ' +str(initialINCIDENCE))
>
> The result given is:
>
> First result [ 345.49614857 -7267.86283429 10006.94459429 2739.08176
> -4528.78107429 -11796.64390857 5478.16352 ]
> Second result [ 345.49614857 17274.80742857 34549.61485714
> 51824.42228571 69099.22971429 86374.03714286 103648.84457143]
>
> Clearly both are different, and in particular the first one has no sense to
> me.

This is not a bug. Have a look at the array's dtype:

>>> n = 60461826
>>> a = np.array([1, 50, 100, 150, 200, 250, 300])
>>> a.dtype
dtype('int32')

A 32-bit integer cannot hold the result of, e. g. 50 * n, the result is
unhelpfully clipped.

One possible fix is to specify the array type:

>>> b = np.array([1, 50, 100, 150, 200, 250, 300], dtype=float)
>>> b * n
array([6.04618260e+07, 3.02309130e+09, 6.04618260e+09, 9.06927390e+09,
1.20923652e+10, 1.51154565e+10, 1.81385478e+10])
--
https://mail.python.org/mailman/listinfo/python-list
Re: Bug report [ In reply to ]
On 24/02/2021 20:36, Carla Molina wrote:
> I found the following bug (python 3.9.1) when multiplying an array by
> several variables without parentheses; look at the following example:
>
> import numpy as np
>
> NR = 0.25
> N = 60461826
>
> initialINCIDENCE = np.array([1, 50, 100, 150, 200, 250, 300])
> initialINCIDENCE = initialINCIDENCE*N/(100000*7*NR)
> print('First result ' +str(initialINCIDENCE))
>
> initialINCIDENCE = np.array([1, 50, 100, 150, 200, 250, 300])
> initialINCIDENCE = initialINCIDENCE*(N/(100000*7*NR))
> print('Second result ' +str(initialINCIDENCE))
>
> The result given is:
>
> First result [ 345.49614857 -7267.86283429 10006.94459429 2739.08176
> -4528.78107429 -11796.64390857 5478.16352 ]
> Second result [ 345.49614857 17274.80742857 34549.61485714
> 51824.42228571 69099.22971429 86374.03714286 103648.84457143]
>
> Clearly both are different, and in particular the first one has no sense to
> me.

This is not a bug. Have a look at the array's dtype:

>>> n = 60461826
>>> a = np.array([1, 50, 100, 150, 200, 250, 300])
>>> a.dtype
dtype('int32')

A 32-bit integer cannot hold the result of, e. g. 50 * n, the result is
unhelpfully clipped.

One possible fix is to specify the array type:

>>> b = np.array([1, 50, 100, 150, 200, 250, 300], dtype=float)
>>> b * n
array([6.04618260e+07, 3.02309130e+09, 6.04618260e+09, 9.06927390e+09,
1.20923652e+10, 1.51154565e+10, 1.81385478e+10])

--
https://mail.python.org/mailman/listinfo/python-list
Re: Bug report [ In reply to ]
I'm getting:
/usr/local/cpython-2.7/bin/python (2.7.16) bad
('numpy version:', '1.16.6')
Traceback (most recent call last):
File "./nii", line 31, in <module>
assert left == right, "{} != {}".format(left, right)
AssertionError: 86374.0371429 != 86374.0371429
/usr/local/cpython-3.0/bin/python (3.0.1) bad No numpy found
/usr/local/cpython-3.1/bin/python (3.1.5) bad No numpy found
/usr/local/cpython-3.2/bin/python (3.2.5) bad No numpy found
/usr/local/cpython-3.3/bin/python (3.3.7) bad No numpy found
/usr/local/cpython-3.4/bin/python (3.4.8) bad No numpy found
/usr/local/cpython-3.5/bin/python (3.5.5) bad
numpy version: 1.18.5
Traceback (most recent call last):
File "./nii", line 31, in <module>
assert left == right, "{} != {}".format(left, right)
AssertionError: 86374.03714285714 != 86374.03714285715
/usr/local/cpython-3.6/bin/python (3.6.0) bad
numpy version: 1.18.1
Traceback (most recent call last):
File "./nii", line 31, in <module>
assert left == right, "{} != {}".format(left, right)
AssertionError: 86374.03714285714 != 86374.03714285715
/usr/local/cpython-3.7/bin/python (3.7.0) bad
numpy version: 1.19.0
Traceback (most recent call last):
File "./nii", line 31, in <module>
assert left == right, "{} != {}".format(left, right)
AssertionError: 86374.03714285714 != 86374.03714285715
/usr/local/cpython-3.8/bin/python (3.8.0) bad
numpy version: 1.19.0
Traceback (most recent call last):
File "./nii", line 31, in <module>
assert left == right, "{} != {}".format(left, right)
AssertionError: 86374.03714285714 != 86374.03714285715
/usr/local/cpython-3.9/bin/python (3.9.0) bad
numpy version: 1.19.4
Traceback (most recent call last):
File
"/home/dstromberg/src/stack-overflow,python-list/numpy-initial-incidence/./nii",
line 31, in <module>
assert left == right, "{} != {}".format(left, right)
AssertionError: 86374.03714285714 != 86374.03714285715

So the difference is tiny for me, and can be imputed to rounding error.
Multiplying the big terms first before dividing naturally gives less
rounding error.

As far as why your install is having problems, I don't really know. What
version of numpy are you using?


On Wed, Feb 24, 2021 at 12:12 PM Carla Molina <carlamolinagrane@gmail.com>
wrote:

> I found the following bug (python 3.9.1) when multiplying an array by
> several variables without parentheses; look at the following example:
>
> import numpy as np
>
> NR = 0.25
> N = 60461826
>
> initialINCIDENCE = np.array([1, 50, 100, 150, 200, 250, 300])
> initialINCIDENCE = initialINCIDENCE*N/(100000*7*NR)
> print('First result ' +str(initialINCIDENCE))
>
> initialINCIDENCE = np.array([1, 50, 100, 150, 200, 250, 300])
> initialINCIDENCE = initialINCIDENCE*(N/(100000*7*NR))
> print('Second result ' +str(initialINCIDENCE))
>
> The result given is:
>
> First result [ 345.49614857 -7267.86283429 10006.94459429 2739.08176
> -4528.78107429 -11796.64390857 5478.16352 ]
> Second result [ 345.49614857 17274.80742857 34549.61485714
> 51824.42228571 69099.22971429 86374.03714286 103648.84457143]
>
> Clearly both are different, and in particular the first one has no sense to
> me.
> Thank you,
>
> Carla.
> --
> https://mail.python.org/mailman/listinfo/python-list
>
--
https://mail.python.org/mailman/listinfo/python-list
Re: Bug report [ In reply to ]
On Wed, Feb 24, 2021 at 12:58 PM Peter Otten <__peter__@web.de> wrote:

> On 24/02/2021 20:36, Carla Molina wrote:
> This is not a bug. Have a look at the array's dtype:
>
> >>> n = 60461826
> >>> a = np.array([1, 50, 100, 150, 200, 250, 300])
> >>> a.dtype
> dtype('int32')
>
I'm getting dtypes of float64.
--
https://mail.python.org/mailman/listinfo/python-list
Re: Bug report [ In reply to ]
On 24/02/2021 22:03, Dan Stromberg wrote:
> On Wed, Feb 24, 2021 at 12:58 PM Peter Otten <__peter__@web.de> wrote:
>
>> On 24/02/2021 20:36, Carla Molina wrote:
>> This is not a bug. Have a look at the array's dtype:
>>
>> >>> n = 60461826
>> >>> a = np.array([1, 50, 100, 150, 200, 250, 300])
>> >>> a.dtype
>> dtype('int32')
>>
> I'm getting dtypes of float64.

When you run the snippet above or

> import numpy as np
>
> NR = 0.25
> N = 60461826
>
> initialINCIDENCE = np.array([1, 50, 100, 150, 200, 250, 300])
> initialINCIDENCE = initialINCIDENCE*N/(100000*7*NR)

here, i. e. after the division?

initialINCIDENCE*N

should be an int32 array, but dividing by

(100000*7*NR)

returns an dtype=float64 array. Switching back to my modified example:

>>> a/42
array([0.02380952, 1.19047619, 2.38095238, 3.57142857, 4.76190476,
5.95238095, 7.14285714])
>>> _.dtype
dtype('float64')
--
https://mail.python.org/mailman/listinfo/python-list
Re: Bug report [ In reply to ]
On 24/02/2021 22:03, Dan Stromberg wrote:
> On Wed, Feb 24, 2021 at 12:58 PM Peter Otten <__peter__@web.de> wrote:
>
>> On 24/02/2021 20:36, Carla Molina wrote:
>> This is not a bug. Have a look at the array's dtype:
>>
>> >>> n = 60461826
>> >>> a = np.array([1, 50, 100, 150, 200, 250, 300])
>> >>> a.dtype
>> dtype('int32')
>>
> I'm getting dtypes of float64.

When you run the snippet above or

> import numpy as np
>
> NR = 0.25
> N = 60461826
>
> initialINCIDENCE = np.array([1, 50, 100, 150, 200, 250, 300])
> initialINCIDENCE = initialINCIDENCE*N/(100000*7*NR)

here, i. e. after the division?

initialINCIDENCE*N

should be an int32 array, but dividing by

(100000*7*NR)

returns an dtype=float64 array. Switching back to my modified example:

>>> a/42
array([0.02380952, 1.19047619, 2.38095238, 3.57142857, 4.76190476,
5.95238095, 7.14285714])
>>> _.dtype
dtype('float64')

--
https://mail.python.org/mailman/listinfo/python-list