It has been known since Pythagoras that 2^(1/2) is irrational. It is also obvious that 4^(1/2) is rational. There is also a fun proof that even the power of two irrational numbers can be rational.
Can you, in general, compute whether the power of two rational numbers is rational?
The reason I am asking, besides curiosity, is that the Fraction-type in Python always returns a float on exponentiation. If there is a quick way to tell if it could be accurately expressed as a fraction, the power function could conceivably only return floats when it has to.
EDIT: By popular demand, I changed 0.5 to 1/2 to make it clearer that it is a fraction and not a float.
There is no really easy way to test if the result of
a ** bwithaandbbeing rational numbers is rational. The easiest way is to decomposeainto its prime factorisationwith
p_ibeing prime numbers andk_ibeing (signed) integers. The result ofa ** bis rational if allk_i * bare integers again.