Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

I'm going crazy

  1. Jul 17, 2005 #1
    A=-[(x_i-X_j)cos\theta_j + (y_i-Y_J)sin\theta_j]
    B = (x_i-X_j)^2+(y_i-Y_j)^2

    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)
    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

    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: Jul 17, 2005
  2. jcsd
  3. Jul 18, 2005 #2
    Hey guys, I really need help.
  4. Jul 18, 2005 #3


    User Avatar
    Staff Emeritus

    Check that your not truncating values. Type-casting is very important in these situtations.
  5. Jul 18, 2005 #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
  6. Jul 18, 2005 #5


    User Avatar
    Staff Emeritus

    I would try breaking up the equation and testing to see if your getting expected values.
  7. Jul 19, 2005 #6
  8. Jul 19, 2005 #7
    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?
  9. Jul 22, 2005 #8


    User Avatar

    I just tried doing the math in my head but I don't think the equality holds for theta_j=0



    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: Jul 22, 2005
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook