Jump to content

I have a contest coming up and I am doing problems for it.

And i have a problem where you enter x, y coordinates for 3 points.

Than calculate the area.

area = a * b * sinw / 2
cosw = (a * a + b * b - c * c) / 2 * a * b
a, b, c are the 3 sides connected using the 3 points, w is the degree
This is my code: (c++)
#include <iostream>#include <cmath>int main(){    long double area;    long double a, b, c;    long double cosw, sinw;    long double x[3], y[3];    while (std::cin >> x[0] >> y[0] >> x[1] >> y[1] >> x[2] >> y[2])    {          if (x[0] == 0 && y[0] == 0 && x[1] == 0 && y[1] == 0 && x[2] == 0 && y[2] == 0) break;                a = sqrt(abs((x[0] - x[1]) * (x[0] - x[1]) + (y[0] - y[1]) * (y[0] - y[1])));          b = sqrt(abs((x[1] - x[2]) * (x[1] - x[2]) + (y[1] - y[2]) * (y[1] - y[2])));          c = sqrt(abs((x[2] - x[0]) * (x[2] - x[0]) + (y[2] - y[0]) * (y[2] - y[0])));          cosw = (a * a + b * b - c * c) / (2 * a * b);          sinw = sqrt(1 - cosw * cosw);          area = (a * b * sinw) / 2;          std::cout << area << std::endl;    }    return 0;}

It works for some of the test values.

The diff between answer has to be within 1.

Input:

3.1415 2.7777 -3.9123 0.2133 0.4324 -11.111 
-8.675309 1.41421 9.999 0.0001 9.999 1.41421 
0.7071 7.732 2.718 -1.005 -6.931 0.866025 
0.6125 0.03125 99.999 0.9125 99.999 -0.56875 
Output:
45.5104 
13.2038 
40.2704 
73.6081 
My code is within difference except for the second one and the last one.
Is this because of precision?
 

 

Link to comment
https://linustechtips.com/topic/138425-precision-problem/
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×