Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

uint: Why is -(a*b) different from -a*b

I understand all of these apart from the last one:

import numpy as np
a = np.array(5).astype('uint16')

a
Out[24]: array(5, dtype=uint16)

-a
Out[25]: 65531

a*1.
Out[26]: 5.0

-(a*1.)
Out[27]: -5.0

0-a*1.
Out[28]: -5.0

-a*1.
Out[29]: 65531.0

type(a*1.)
Out[30]: numpy.float64

In the last line a uint variable is multiplied with a float resulting in a float, so why is the result of -a*1. not -5.0, and how is this different from -(a*1.) and 0-a*1.?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

-a*1. is (-a)*1., because unary - has a higher precedence than *. For details, look at the excerpts from the Python grammar in the documentation.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading