|
Subject:
|
Division by zero error
|
|
Posted By:
|
ss2003
|
Post Date:
|
12/30/2003 11:28:43 AM
|
My function is giving me a Division by zero error on the last line number and I can't figure out where the problem is. I realize my code is sloppy as I am very new, if you have any tips to clean it up please let me know. Here's the function:
function altAzim($T, $R, $t, $g, $D) { //Local Hour Angle $T5=$T-$R+$g; $S1=sin($t)*sin($D); $S1=$S1+cos($t)*cos($D)*cos($T5); $C1=1-$S1*$S1; if ($C1 > 0) { $C1=sqrt($C1); $H=atan($S1/$C1); } elseif ($C1 <= 0) { $S1 = ($S1 < 0)? -1 : $S1; $S1 = ($S1 > 0)? 1 : $S1; $S1 = ($S1 = 0)? 0 : $S1; $H=$S1*pi()/2; } $C2=cos($t)*sin($D); $C2=$C2-sin($t)*cos($D)*cos($T5); $S2=-cos($D)*sin($T5); if ($C2 = 0) { $S2 = ($S2 < 0)? -1 : $S2; $S2 = ($S2 > 0)? 1 : $S2; $S2 = ($S2 = 0)? 0 : $S2; $A=$S2*pi()/2; } else { $A=atan($S2/$C2); if ($C2 < 0) { $A=$A+pi(); } } if ($A < 0) { $A=$A+2*pi(); } return; }
All of my divisions are inside of if statements that require the divisor to be non-zero. Is this someting to do with using the atan function?
Thanks for your help.
|
|
Reply By:
|
nikolai
|
Reply Date:
|
12/30/2003 1:01:59 PM
|
No, it's because you're making an assignment instead of a comparison in this line:
if ($C2 = 0)
That should be:
if ($C2 == 0)
What your version does is assigns zero to $C2, then evaluates the conditional expression.
That simplifies down to: if (0). Zero evaluates as false, so the else block executes. Your first line is where you divide by $C2, which you just set to zero above.
Make sense?
Take care,
Nik http://www.bigaction.org/
|
|
Reply By:
|
nikolai
|
Reply Date:
|
12/30/2003 1:03:13 PM
|
You said you were new, so I'm including this link in case it's useful: http://www.php.net/operators.comparison
Take care,
Nik http://www.bigaction.org/
|
|
Reply By:
|
ss2003
|
Reply Date:
|
12/30/2003 5:07:38 PM
|
That's it, thanks! Thanks for the link too. I had looked through the manual trying to find that section but I didn't know it was called Comparison Operators.
Shouldn't I also be writing $S2 = ($S2 == 0)? 0 : $S2; and $S1 = ($S1 == 0)? 0 : $S1; these didn;t give me the error either way I used them.
|
|
Reply By:
|
nikolai
|
Reply Date:
|
12/30/2003 5:22:29 PM
|
Yes, that's also correct. A single equals sign is always assignment, a double (or triple) equals sign is always comparison.
Of course, I don't understand why you've included those two lines of code -- they don't DO anything.
In english, they say "If $S2 is zero, then assign zero to $S2. Otherwise, assign the current value of $S2 back into $S2."
In either case, you're just reassigning the value to a variable that it already contains.
Take care,
Nik http://www.bigaction.org/
|
|
Reply By:
|
ss2003
|
Reply Date:
|
1/1/2004 10:51:35 AM
|
Yes of course you are right! Guess I just got a little carried awy there. Thanks!
|