I'm doing a mixed number calculator for my c++ class. We are to store everything in a custom mixed number class with whole number, numerator and denominator. Because of this, irrational answers have to be approximated before converting to mixed numbers. My problem is if I square the output of 2^(1/2), it would return a mixed number that is really close to 2 but not 2 exactly. I want to know is there a way to make (a^(1/n))^n return a for all positive a's. TIA.
I assume you understand that the square root of two is not a rational number. Hence there exist no two integers whose quotient squared is exactly two. You have two basic options:
You can round all results that are really, really close to an integer to that integer exactly. In some cases, this will give the wrong answer. But you should already have accepted that you can't always provide an exact answer to every possible problem because you're representing as rational some numbers that cannot be represented exactly that way.
You can add some additional information in addition to the two integers to the representation. For example, if something is a result of raising an integer to a power, you can flag it as such. If it is later exponentiated, you can employ rounding as above or you can just restore the original integer.
Basically, decide what you want and code it.