dleal - 1 year ago 105

Python Question

The question is simple:

I create the following random array and I multiply it times a constant but get an unexpected result (zeros instead of real answer):

`In [4]: A = np.random.rand(1,11)`

In [5]: A

Out[5]:

array([[ 0.15138551, 0.41573765, 0.0212214 , 0.44955909, 0.27013062,

0.37835199, 0.89712845, 0.95333785, 0.09920397, 0.2303608 ,

0.11246899]])

In [6]: A*(1/100)

Out[6]: array([[ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])

In [7]: A/100

Out[7]:

array([[ 0.00151386, 0.00415738, 0.00021221, 0.00449559, 0.00270131,

0.00378352, 0.00897128, 0.00953338, 0.00099204, 0.00230361,

0.00112469]])

What is the reason for this? seems extremely counterintuitive,

Thank you and I appreciate your answer,

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You are using python2 and `1/100`

is integer division, resulting in `0`

.

Use floating points:

```
A * (1.0/100)
```

or use a future import:

```
from __future__ import division
```

to enable floating point division for integers, too.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**