
[Stefan Pochmann <smpochmann@gmail.com>]
It crashes because it tries to convert 10**400 to a float and that fails:
10**400 / 1e200 Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> 10**400 / 1e200 OverflowError: int too large to convert to float
But with two ints it succeeds:
10**400 / 10**200 1e+200
Simlalary for 1e200 / 10**400. That is, it doesn't matter to this whether int.__truediv__ or float.__truediv__ is invoked. If the types are mixed, the operands are coerced to float first.
Note that 1e200 is an integer:
1e200.is_integer() True
So that could losslessly be converted to int, and then the division would succeed:
10**400 / int(1e200) 1e+200
So could/should 10**400 / 1e200 be implemented to do that instead of raising the error?
It could. But I don't think it "should".
Or is it a too rare use case and not worth the effort, or does something else speak against it?
Too rare, and expensive. int.__truediv__ makes semi-heroic efforts to get 10**400 / int(1e200) "right" (well, as right as can be), and that's expensive, and probably more surprising than not.for most users. For example,
int(1e200) 99999999999999996973312221251036165947450327545502362648241750950346848435554075534196338404706251868027512415973882408182135734368278484639385041047239877871023591066789981811181813306167128854888448
is, all on its own, probably more surprising than not for most users. It's not to me (or presumably to you), but I have no use for Python magically converting a float to int. It doesn't in other contexts either: [1, 2, 3][2.0] Traceback (most recent call last): ... TypeError: list indices must be integers or slices, not float In purely computational contexts, "when an inf and a float are mixed, the int is converted to a float first" is a simple, predictable,and reliable rule:
pow(10**400, 0.5) Traceback (most recent call last): ,,, OverflowError: int too large to convert to float
In practice, over decades I've seen that kind of exception only a handful of times, and it always pointed to a logical error in my code.