Jump to content

[SOLVED] Equivical Codes: One Works, the Other Doesn't...


lotrfan

Recommended Posts

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.

Link to comment
Share on other sites

*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/>

Link to comment
Share on other sites

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!

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

>_<

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.