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

  • Thread starter Thread starter sid_galt
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around a C++ implementation of a mathematical equation related to the calculation of a variable E, defined as E=(B-A^2)^{1/2}. Participants are addressing discrepancies between the mathematical formulation and the results produced by their C++ code, exploring potential reasons for these differences.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents the mathematical formulation of E and its C++ implementation, noting discrepancies between the two.
  • Another participant suggests checking for value truncation and emphasizes the importance of type-casting in C++.
  • A different participant expresses skepticism about truncation issues, citing that all quantities are in double precision and that the differences observed are significant.
  • One participant recommends breaking down the equation to test individual components for expected values.
  • Another participant reiterates the mathematical expression for E, introducing absolute values and questioning the equality under specific conditions (theta_j=0).
  • A later reply reflects on the equality of E and F, suggesting that the cosine term may have been omitted in earlier discussions.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the source of the discrepancies between the mathematical and C++ results. Multiple competing views regarding potential issues and interpretations of the equations remain present.

Contextual Notes

Participants express uncertainty about the behavior of the equations under specific conditions, such as when theta_j equals zero, and the implications of using absolute values in the calculations.

sid_galt
Messages
503
Reaction score
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
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
[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]
 
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?
 
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:

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
1
Views
3K
  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
6
Views
2K