简体   繁体   中英

Python int unexpected result

Can someone explain me why x and y give completely different results?

>>> x=int(5) * 1e50
>>> x
5e+50

>>> y=int(5e50)
>>> y
499999999999999996610474337180813988230854220972032

The python manual says that integer can be of arbitrary length regardless the available memory, x and y as I known are integers but the result is completely different but are equivalent expressions, is 5e50 interpreted as float before int conversion? If so, why?

int(5) is an int, 1e50 is a float. If you multiply them, the int(5) will be converted to the larger type (which is a float) and then multiplied with 1e50. The result is of type float, not int.

int(5e50) converts the float to an int and is therefore printed as an integer of, as you say, arbitrary length.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM