Jump to content

precision problem

RexLee

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
Share on other sites

Link to post
Share on other sites

Answer is 2 1/2

Link to comment
Share on other sites

Link to post
Share on other sites

what is the output supposed to be?

 

doubles shouldn't give you errors in that order of magnitude, i really don't think that's the case

Link to comment
Share on other sites

Link to post
Share on other sites

The output is the area of the triangle.

Link to comment
Share on other sites

Link to post
Share on other sites

and what's your problem? your program gives the exact results for the second and fourth cases too

Link to comment
Share on other sites

Link to post
Share on other sites

Really?

What compiler are you using.

I am getting 9.xxx for the second one, and 70.xxx for the last one.

Link to comment
Share on other sites

Link to post
Share on other sites

Really?

What compiler are you using.

I am getting 9.xxx for the second one, and 70.xxx for the last one.

are you sure you're writing in the right numbers?

i'm using GCC, but this really shouldn't be about it

Link to comment
Share on other sites

Link to post
Share on other sites

alright i see, i had to remove the abs() part because the compiler couldn't find it

abs returns an int, not a floating point

also, abs() is not required because

a^2 + b^2 >= 0

for every a, b

 

so yeah, just take away those abs() and you're good

Link to comment
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

×