Precision of TriangleArea problem

Back to General discussions forum

Deceptibot     2015-08-06 18:38:38

I always get answers like 2.01894e+06. I've seen on this forum that another coder had a similar problem but the admin didn't specify in details what he modified in the code. Why do you generate over the top values for the coordinates? The algorithm is good but why do you expect us to calculate such tremendous values if you don't have the computational capacity to do so? I even tried to split Heron's formula to A=sqrt(s)sqrt(s-a)sqrt(s-b)sqrt(s-c) instead of A= sqrt(s(s-a)(s-b)(s-c)).

Rodion (admin)     2015-08-06 20:24:33
User avatar

Hi! Thanks for your message!

> The algorithm is good but why do you expect us to calculate such tremendous values if you don't have the computational capacity to do so?

Results in this problem are either integer, or halves (e.g. 0.5) so I believe the algorithm should work all right. Probably the trouble is somewhere in different place...

I had a look at your solution. Have you tried to print the result with something other than cout?

Perhaps this may help?

printf("%lf ", area);

Regretfully I'm not well acquainted with C++ to suggest how you can format cout output better.

Quandray     2015-08-06 20:41:24
User avatar

To print an integer or .5 I used

if(fabs(area-int(area))<.001) printf("%.0f ",area);  // integer
else printf("%.1f ",area);                           // .5
Rodion (admin)     2015-08-06 21:19:16
User avatar

Well, this should work even with not exact values, e.g. 14.4999999736 will do, I believe :)

Deceptibot     2015-08-06 21:24:59

Thanks, Quandray and Admin.

At first I didn't even thought that what you wrote is even C++. Still a lot to learn.

Rodion (admin)     2015-08-06 21:50:41
User avatar

You are partially correct - at least printf is old C function and some people regard it as not-C++ :)

Please login and solve 5 problems to be able to post at forum