While I was messing around with Python,
>>> [attr for attr in dir(1) if not attr.startswith('_')]
['bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']
>>> [attr for attr in dir(1.1) if not attr.startswith('_')]
['as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real']
Although I understand that ‘conjugate’, ‘imag’, and ‘real’ are there for the sake of compatibility with complex type, I can’t understand why ‘numerator’ and ‘denominator’ exists for int only, and doesn’t for a float.
Any explanation for that ?
This is most likely because floats are somewhat lossy – they can not perfectly represent every value. Consider this example:
If you wanted the access the denominator of
1.0/5.0python would have to return18014398509481984(20000000000000001/100000000000000000 == 3602879701896397/18014398509481984). The loss of precision will cause python to have no choice but to return crazy values, so the designers chose not to implement the function.