I having trouble with the rational class
here is my assignment:
Add a square root function to the rational class. Have your program print the square root of any rational number.
I want to find the square root for numerator and denominator separatetly. divide the answer to get decimals and convert the decimal to fractions.
i got till geting the decimal but i want to convert it back to a simplified fraction
Not sure you can achieve that. First an observation:
1 2 3 4
float root(int a,int b)
{
return sqrt(1.0*a/b);
}
Since you are talking about fractions, I assume that a and b are integers (not floats). Also, no reason why you should use sqrt twice.
Now back to your problem: One very simple fraction is 2/1. You know that sqrt(2.0) is not a rational number. Please explain what you want to do in this case.
Also note that sqrt and float functions introduce rounding errors. Take for example 1/9. The square root is 1/3. But, since I use float, 1/3 is about 0.3333332, or some other number, slightly different than 1/3. Even if it's 0.333333, you will write this as 333333/1000000, which does not simplify to 1/3