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

  • Thread starter sid_galt
  • Start date
In summary, the conversation discusses a mathematical equation and its implementation in C++. The equation involves variables A, B, E, and F, which are calculated using different quantities in the code. However, the output values for E and F are different in C++ compared to the expected values from the mathematical equation. The conversation suggests checking for truncation or type-casting errors in the code. A suggestion is made to break up the equation and test for expected values, but the differences persist. The conversation ends with a realization that the equality may not hold for certain values of theta_j.
  • #1
sid_galt
502
1
[tex]
A=-[(x_i-X_j)cos\theta_j + (y_i-Y_J)sin\theta_j]
[/tex]
[tex]
B = (x_i-X_j)^2+(y_i-Y_j)^2
[/tex]
[tex]
E=(B-A^2)^{1/2}=(x_i-X_j)sin\theta_j+(y_i-Y_j)\theta_j
[/tex]

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) =[tex]x_i[/tex]
p_c(1)(i) = [tex]y_i[/tex]
p_v(0)(j) = [tex]X_j[/tex]
p_v(1)(j) = [tex]Y_j[/tex]
angle(j) = [tex]\theta_j[/tex]

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

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
  • #2
Hey guys, I really need help.
 
  • #3
Check that your not truncating values. Type-casting is very important in these situtations.
 
  • #4
I don't think so because
1. All the quantities are in double
2. The differences are too high, sometimes as high as -1
 
  • #5
I would try breaking up the equation and testing to see if your getting expected values.
 
  • #6
sid_galt said:
Well, here's the deal. Mathematically
[tex]E=(B-A^2)^{1/2}=(x_i-X_j)sin\theta_j+(y_i-Y_j)\theta_j[/tex]
[tex]E=(B-A^2)^{1/2}=|(x_i-X_j)\sin\theta_j-(y_i-Y_j)\cos\theta_j|[/tex]
 
  • #7
chronon said:
[tex]E=(B-A^2)^{1/2}=|(x_i-X_j)\sin\theta_j-(y_i-Y_j)\cos\theta_j|[/tex]

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?
 
  • #8
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:

What is the formula for solving the C++ math problem E=(B-A^2)^{1/2}?

The formula for solving this C++ math problem is E = (B - A^2)^(1/2), where E represents the result, B is the first number, and A is the second number.

How do I input the values of B and A in the C++ program?

In order to input the values of B and A in the C++ program, you can use the cin function to prompt the user for input.

What is the purpose of the "^" symbol in the math problem?

The "^" symbol is the exponent operator in C++, which raises the number before it to the power of the number after it.

What happens if the value of (B-A^2) is negative?

If the value of (B-A^2) is negative, the program will result in an error because the square root of a negative number is not a real number.

Can this C++ math problem be solved using any other programming language?

Yes, this C++ math problem can be solved in other programming languages such as Java or Python. However, the syntax and logic may differ slightly.

Similar threads

Replies
5
Views
271
  • Set Theory, Logic, Probability, Statistics
Replies
9
Views
999
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
12
Views
1K
  • Advanced Physics Homework Help
Replies
2
Views
1K
  • Topology and Analysis
Replies
4
Views
2K
Replies
3
Views
621
Replies
3
Views
1K
Back
Top