lotrfan Posted October 8, 2007 Share Posted October 8, 2007 Hi All, <?php $side_a = $_REQUEST[side_a]; $side_b = $_REQUEST[side_b]; $side_c = $_REQUEST[side_c]; $angle_a = $_REQUEST[angle_a]; $angle_b = $_REQUEST[angle_b]; $angle_c = $_REQUEST[angle_c]; $side[0]=$side_a; $side[1]=$side_b; $side[2]=$side_c; $angle[0]=$angle_a; $angle[1]=$angle_b; $angle[2]=$angle_c; $angle[0] = deg2rad($angle[0]); $angle[1] = deg2rad($angle[1]); $angle[2] = deg2rad($angle[2]); $angle[0] = asin(($side[0]*sin($angle[2])/$side[2])); echo "<BR>" ,$angle[0], "<BR>"; echo "" ,$angle[2], "<BR>"; $angle[1] = $pi - $angle[0] - $angle[2]; echo $angle[1]; ?> In this case, value inputs are: Angle C = 45 Side A = 12 Side B = 15 Yet, it will never work. I will always get a incorrect radian value. Doing the same problem on my calculator yields no problems at all. (By the way, if any one can tell me the difference between how PHP operates and how a calculator does would be greatly appreciated). The problem starts with the line "$angle[1] = $pi - $angle[0] - $angle[2];". This value will always return a NEGATIVE value inside my code, yet alone (as it is posted here) it has no trouble whatsoever. That alone seems ridiculous to me. I got around by doing this: I replaced this $angle[1] = $pi - $angle[0] - $angle[2]; With this $angle[0] = rad2deg($angle[0]); $angle[1] = rad2deg($angle[1]); $angle[2] = rad2deg($angle[2]); $angle[1] = 180 - $angle[0] - $angle[2]; $angle[0] = deg2rad($angle[0]); $angle[1] = deg2rad($angle[1]); $angle[2] = deg2rad($angle[2]); But it is so messy! (Not to mention seemingly unnecessary) All the first code is doing is finding out the remaining radians in the triangle out of the total 'pi' radians avalible -- the exact same thing as finding the degrees remaining out of 180 degrees for a triangle! This is very perplexing to me. Maybe I missed something very obvious... Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/72230-solved-equivical-codes-one-works-the-other-doesnt/ Share on other sites More sharing options...
lotrfan Posted October 8, 2007 Author Share Posted October 8, 2007 *bump* Guess I stumped everybody, huh? here's html... <form method="post" action="hypotenuse.php"> <p>Side a</p><input name="side_a" type="text" /><br /> <p>Side b</p><input name="side_b" type="text" /><br /> <p>Side c</p><input name="side_c" type="text" /><br /> <p>Angle A</p><input name="angle_a" type="text" /><br /> <p>Angle B</p><input name="angle_b" type="text" /><br /> <p>Angle C</p><input name="angle_c" type="text" /><br /> <input type="submit" value="GO!" /> <form/> Quote Link to comment https://forums.phpfreaks.com/topic/72230-solved-equivical-codes-one-works-the-other-doesnt/#findComment-364977 Share on other sites More sharing options...
Rithiur Posted October 8, 2007 Share Posted October 8, 2007 Well.. I don't see $pi defined anywhere. Perhaps you should have been using pi() or M_PI instead. Quote Link to comment https://forums.phpfreaks.com/topic/72230-solved-equivical-codes-one-works-the-other-doesnt/#findComment-364978 Share on other sites More sharing options...
lotrfan Posted October 8, 2007 Author Share Posted October 8, 2007 My bad...didn't want to paste whole 683 line script...but it's there... $pi = 3.14159265358979323846; Quote Link to comment https://forums.phpfreaks.com/topic/72230-solved-equivical-codes-one-works-the-other-doesnt/#findComment-364982 Share on other sites More sharing options...
lessthanthree Posted October 8, 2007 Share Posted October 8, 2007 Should $angle[1] = $pi - $angle[0] - $angle[2]; not be: $angle[1] = $pi - ($angle[0] - $angle[2]); ? Quote Link to comment https://forums.phpfreaks.com/topic/72230-solved-equivical-codes-one-works-the-other-doesnt/#findComment-364999 Share on other sites More sharing options...
roopurt18 Posted October 8, 2007 Share Posted October 8, 2007 What is the value of $side[2] before this line: $angle[0] = asin(($side[0]*sin($angle[2])/$side[2])); And is that value correct? What exactly are you trying to compute anyways? My trig is a little rusty! Quote Link to comment https://forums.phpfreaks.com/topic/72230-solved-equivical-codes-one-works-the-other-doesnt/#findComment-365011 Share on other sites More sharing options...
lotrfan Posted October 9, 2007 Author Share Posted October 9, 2007 Well, the value will be whatever the user inputs. It's arbitrary since this code will be used for any triangle situation -- there's a lot more to this code that works -- but this is the one line that REFUSES to work for reasons I can't understand. What I am solving for -- here's an except from my code: elseif (empty($side[2])) { $side[2] = sqrt((($side[0]*$side[0])+($side[1]*$side[1]))-(2*$side[0]*$side[1]*cos($angle[2]))); if ($side[0] < $side[1]) { $angle[0] = asin(($side[0]*sin($angle[2])/$side[2])); $angle[1] = $pi - $angle[0] - $angle[2]; } basically, if the user doesn't input side two, the php has already determined this is an SAS triangle (again, this not really important, just more code that works, so isn't included). Now it solves here: $side[2] = sqrt((($side[0]*$side[0])+($side[1]*$side[1]))-(2*$side[0]*$side[1]*cos($angle[2]))); if ($side[0] < $side[1]) for side[2] by law of cosines. next, by law of sines: $angle[0] = asin(($side[0]*sin($angle[2])/$side[2])); and then, to get the last angle (which, yes could be used by law of sines, but unfortunatly, other parts of my code need the next line of code to work -- uses this line: $angle[1] = $pi - $angle[0] - $angle[2]; Here's the concept: pi radians = 180 degrees -->180 - 2 degree angles = last angle which should be the same as: -->pi - 2 radian angles = last angle But it won't work! Please help me -- I'm lost! Quote Link to comment https://forums.phpfreaks.com/topic/72230-solved-equivical-codes-one-works-the-other-doesnt/#findComment-365057 Share on other sites More sharing options...
roopurt18 Posted October 9, 2007 Share Posted October 9, 2007 echo $pi, $angle[0], and $angle[2] before executing the statement $angle[1] = $pi - $angle[0] - $angle[2]; and figure out which is incorrect. Then figure out what in your code is causing it to be incorrect. Perhaps if you attached your entire script to a post someone could help you more. Quote Link to comment https://forums.phpfreaks.com/topic/72230-solved-equivical-codes-one-works-the-other-doesnt/#findComment-365159 Share on other sites More sharing options...
lotrfan Posted October 9, 2007 Author Share Posted October 9, 2007 Well, I made a newbie error... I did as ruupert said (echoing vars before the operations) and found nothing wrong. I kept this up, trying to find where the error was, and it was always in this line: $angle[1] = $pi - $angle[0] - $angle[2]; About three hours of senseless work later, it hit me like a bus. I defined $pi, but failed to pass it correctly to the function that it was in! Wow, do I feel like an idiot... >_< Quote Link to comment https://forums.phpfreaks.com/topic/72230-solved-equivical-codes-one-works-the-other-doesnt/#findComment-365666 Share on other sites More sharing options...
roopurt18 Posted October 9, 2007 Share Posted October 9, 2007 Lesson learned: Never take anything for granted in a program. Even while typing that response, I almost left off $pi, but then I thought better of it and said, "Hell, it's just a little more typing, might as well make sure it's what we think it is." About three hours of senseless work later, it hit me like a bus. I defined $pi, but failed to pass it correctly to the function that it was in! We've all done this at one point or other. But! If you'd started with the line that was failing (which is what I recommended), you'd have saved that time. Trust me, most of us feel your pain. Quote Link to comment https://forums.phpfreaks.com/topic/72230-solved-equivical-codes-one-works-the-other-doesnt/#findComment-365670 Share on other sites More sharing options...
lotrfan Posted October 9, 2007 Author Share Posted October 9, 2007 Thanks for indulging me...really sorry if I wasted your time. Quote Link to comment https://forums.phpfreaks.com/topic/72230-solved-equivical-codes-one-works-the-other-doesnt/#findComment-365684 Share on other sites More sharing options...
roopurt18 Posted October 9, 2007 Share Posted October 9, 2007 Teaching is only a waste of time if people refuse to learn. Quote Link to comment https://forums.phpfreaks.com/topic/72230-solved-equivical-codes-one-works-the-other-doesnt/#findComment-365693 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.