When does Python perform type conversion when comparing int and float?

前端 未结 7 1418
太阳男子
太阳男子 2021-01-12 23:22

Why does Python return True when I compare int and float objects which have the same value?

For example:

>&         


        
7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-13 00:09

    From the documentation:

    Python fully supports mixed arithmetic: when a binary arithmetic operator has operands of different numeric types, the operand with the “narrower” type is widened to that of the other, where plain integer is narrower than long integer is narrower than floating point is narrower than complex. Comparisons between numbers of mixed type use the same rule.

    According to this 5*2 is widened to 10.0 and which is equal to 10.0 If you are comparing the mixed data types then the result will be considered on the basics of data type which is having long range, so in your case float range is more then int float max number can be --> 1.7976931348623157e+308 int max number can be --> 9223372036854775807

    Thanks

提交回复
热议问题