jrjohansson / scientific-python-lectures

Lectures on scientific computing with python, as IPython notebooks.
3.51k stars 1.8k forks source link

float to bool conversion example #44

Closed soulne4ny closed 8 years ago

soulne4ny commented 8 years ago

Hi,

I was quite surprised with y = bool(z.real) and y = bool(z.imag) at https://github.com/jrjohansson/scientific-python-lectures/blame/master/Lecture-1-Introduction-to-Python-Programming.ipynb#L994

While it would be ok for integers and it works as shown, it is useless as exact zero is quite uncommon as a result of a computation.

As well, complex to boolean conversion works quite well as it well fits into a scheme x != 0.

>>> bool(1j)
True
>>> bool(1+0j)
True
>>> bool(0j)
False
soulne4ny commented 8 years ago

I've found bool(x) useful with x of type float when x is was previously converted from bool to float to keep all data unified and these values are not results of numerical computations.