note 33908 deleted from control-structures.while by didou

From: Date: Fri, 11 Jul 2003 08:05:54 +0000
Subject: note 33908 deleted from control-structures.while by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-51885@lists.php.net to get a copy of this message
Note Submitter: Merve ---- If you want to calculate powers, but you're having trouble with negative numbers and zero, use this code. It's still not compatible with a decimal number as the power (I'm working on that, but I doubt I'll ever be able to). All you have to do is include a bunch of if-then statements, along with the while loop. For those of you unfamiliar with special characters &#61; is = and <sup> puts your stuff in superscript. <?php if ($b==0) { print (" $a<sup>$b</sup> &#61; 1"); } elseif ($b<0) { $c = $a; $d = $b; $f = ($b*-1); while ($f>1) { $a = ($a*$c); $f--; } $e = (1/$a); print (" $c<sup>$d</sup> &#61; $e"); } else { $c = $a; $d = $b; while ($b>1) { $a = ($a*$c); $b--; } print (" $c<sup>$d</sup> &#61; $a"); } } ?> So, making your calculator will be a cinch.

« previous php.notes (#51885) next »