Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 30th, 2003, 12:28 PM
Authorized User
 
Join Date: Dec 2003
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default Division by zero error

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.

 
Old December 30th, 2003, 02:01 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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/
 
Old December 30th, 2003, 02:03 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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/
 
Old December 30th, 2003, 06:07 PM
Authorized User
 
Join Date: Dec 2003
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

 
Old December 30th, 2003, 06:22 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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/
 
Old January 1st, 2004, 11:51 AM
Authorized User
 
Join Date: Dec 2003
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes of course you are right! Guess I just got a little carried awy there. Thanks!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Division by zero Corey BOOK: Beginning Access 2003 VBA 2 April 13th, 2007 02:56 PM
Division by zero error Corey Access 4 October 20th, 2005 12:51 PM
IIf Statement "division by zero" error Corey BOOK: Beginning Access 2003 VBA 5 September 30th, 2005 05:48 AM
division by zero rickjg JSP Basics 1 November 9th, 2004 08:03 AM
Performing Division in XSLT... Please help.. kiril XSLT 3 May 17th, 2004 05:27 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.