0

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?

PythonNerd
  • 293
  • 1
  • 11
GMG
  • 1,498
  • 14
  • 20

1 Answers1

1

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.

Augunrik
  • 1,866
  • 1
  • 21
  • 28
  • 1
    "the result will be converted to the larger type (which is a float) " um, what do you mean here? – juanpa.arrivillaga Nov 19 '19 at 18:07
  • int * float results in a float return value. You assume it's int, but that's wrong. – Augunrik Nov 19 '19 at 18:08
  • I don't assume that at all. In fact, I don't even think the OP thinks that. In any case, that doesn't answer my question regarding what you mean by that quoted sentence. Specifically, what do you mean by "the larger type" in this context? – juanpa.arrivillaga Nov 19 '19 at 18:09
  • "I don't assume that at all. In fact, I don't even think the OP thinks that." It's clearly stated in the question: "x and y as I known are integers". I clarified my answer at that point. – Augunrik Nov 19 '19 at 18:11