# Why am I getting WA? (Right Triangle)

I was solving the Right Triangle problem (https://www.codechef.com/problems/RIGHTTRI)
and I coded the following solution using a bit of trigonometry:-

``````#include <bits/stdc++.h>
using namespace std;

int main()
{
long long t;
cin >> t;

while(t--)
{
long double h, s;
cin >> h >> s;
long double val = 4*s/pow(h,2);
if(!(val > 0 && val < 1))
{
cout << -1;
}
else
{
long double a,b,c;
long double theta = (asin(val)/2);
a = h*cos(theta);
b = h*sin(theta);
c = h;
cout << fixed << setprecision(6) << min(a,b) << " " << max(a,b) << " " << c;
}
cout << "\n";
}
return 0;
}
``````

I am getting exactly the same output as given in the problem:-

``````3.000000 4.000000 5.000000
-1
-1
285168.817674 546189.769984 616153.000000
``````

@everule1 It would be great if you could help me with this one.

Inaccuracy in floating point calculation
Replace this

``````if(!(val > 0 && val < 1))
{
cout << -1;
}
``````

by

``````if(4*s>h*h)
{
cout << -1;
}
``````

standard c++ trigonometric functions use floats, which are inaccurate.

2 Likes

@everule1 Thank you so much ! I have a doubt, is there any way by which we can increase accuracy of the trigonometric functions?

Define your own trigonometric function, and use at least 7-8 terms of the taylor series. I don’t know about any accurate inbuilt trigonometric functions and couldn’t find it on the internet.

2 Likes

Thanks for the fast response ! I can’t really leave my house so…

1 Like