Solve C++ Math Problem: E=(B-A^2)^{1/2}

  • Thread starter Thread starter sid_galt
  • Start date Start date
AI Thread Summary
The discussion revolves around a mathematical equation involving variables A, B, E, and their implementations in C++. The equations are intended to calculate distances based on coordinates and angles, with A representing a transformed distance, B as the squared distance, and E as the resultant distance derived from B and A. The user reports discrepancies between the mathematical results for E and the computed value F in C++, despite using double precision for all calculations. Suggestions for troubleshooting include checking for value truncation, type-casting issues, and breaking down the equations for testing. There is also a mention of using the Dev C++ 4 compiler, raising concerns about potential issues with it. The equality of E and F is questioned, particularly for specific angle values, leading to further clarification on the mathematical relationships involved.
sid_galt
Messages
502
Reaction score
1
<br /> A=-[(x_i-X_j)cos\theta_j + (y_i-Y_J)sin\theta_j]<br />
<br /> B = (x_i-X_j)^2+(y_i-Y_j)^2<br />
<br /> E=(B-A^2)^{1/2}=(x_i-X_j)sin\theta_j+(y_i-Y_j)\theta_j<br />

I implemented this in C++ as follows

A = -(p_c(0)(i) - p_v(0)(j))*cos(angle(j)) - (p_c(1)(i) - p_v(1)(j))*sin(angle(j))

B = (p_c(0)(i) - p_v(0)(j))*(p_c(0)(i) - p_v(0)(j)) + (p_c(1)(i) - p_v(1)(j))*(p_c(1)(i) - p_v(1)(j))

E=(p_c(0)(i) - p_v(0)(j))*sin(angle(j)) + (p_c(1)(i) - p_c(1)(j))*cos(angle(j))

F= sqrt(B-A*A)

(All parantheses instead of square brackets in arrays to avoid HTML encoding)
where
p_c(0)(i) =x_i
p_c(1)(i) = y_i
p_v(0)(j) = X_j
p_v(1)(j) = Y_j
angle(j) = \theta_j

Well, here's the deal. Mathematically
E=(B-A^2)^{1/2}=(x_i-X_j)sin\theta_j+(y_i-Y_j)\theta_j

However in C++, I am getting different values for E and F in C++ when mathematically they are the same. I am going insane.
Please help
 
Last edited:
Computer science news on Phys.org
Hey guys, I really need help.
 
Check that your not truncating values. Type-casting is very important in these situtations.
 
I don't think so because
1. All the quantities are in double
2. The differences are too high, sometimes as high as -1
 
I would try breaking up the equation and testing to see if your getting expected values.
 
sid_galt said:
Well, here's the deal. Mathematically
E=(B-A^2)^{1/2}=(x_i-X_j)sin\theta_j+(y_i-Y_j)\theta_j
E=(B-A^2)^{1/2}=|(x_i-X_j)\sin\theta_j-(y_i-Y_j)\cos\theta_j|
 
chronon said:
E=(B-A^2)^{1/2}=|(x_i-X_j)\sin\theta_j-(y_i-Y_j)\cos\theta_j|

I tried fabs and fabsf on E. The differences are still there. I am using Dev C++ 4 as the compiler. Could there be something wrong with it?
 
I just tried doing the math in my head but I don't think the equality holds for theta_j=0

E=0
F=abs(y_i-Y_i)

right?

Edit: Wrong. I think the second cosine just fell off when you posted the summary. If the cosine is there, and the absolute values are in place, I think the equality holds.
 
Last edited:
Back
Top