"G Patel said:
It is a school assignment and I have the program working except that
our programs are testing automatically by some program.
He lets us know 8 of the cases, the rest are hidden. 2 of the cases
seem impossible to implement in computers (and yes, we have to sqrt in
a distance function, and then resquare it in the actualy "figuring out"
function).
One of the cases has a triangle with a side that is of length sqrt(41),
which is irrational. So when the other function gets it and resquares
it, accuracy is lost. Yet his testing program wants an exactly
solution (no approximate one allowed for this case).
The other case has a triangle with 2 really large sides and one very
small sides. He wants us to get it within some tolerance, but i can't
seem to "keep" the information from the small side to "stick" within
the sqrt function.
I am going to email him and tell him that his cases are ridiculous (in
a nice way).
No, the test cases are not a problem. Putting a completely unnecessary
complication into the problem _is_ a problem.
There is a way around this. Your program started with integer values.
Each side of the triangle is the square root of an integer. If you
square it, the result is again an integer, except for rounding errors,
which means it is only close to an integer. Now you have to check whether
a*a + b*b - c*c == 0.0
or not. The squares should be integers; therefore the sum on the left
side should be an integer. It won't be an integer because of rounding
errors. Lets say you got a result of 0.01: You know you don't have the
exact result, because the exact result must be an integer. So you can
assume that the correct result is the nearest integer. To check whether
the correct result is zero, check whether the absolute value is less
than 1/2.