note 33908 added to control-structures.while
| From: | Merve at rack1 dot php dot net | Date: | Thu, 10 Jul 2003 18:48:05 +0000 |
| Subject: | note 33908 added to control-structures.while | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-51861@lists.php.net to get a copy of this message | ||
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 =
is = and <sup> puts your stuff in superscript.
<?php
if ($b==0) {
print (" $a<sup>$b</sup> = 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> = $e");
} else {
$c = $a;
$d = $b;
while ($b>1)
{
$a = ($a*$c);
$b--;
}
print (" $c<sup>$d</sup> = $a");
}
}
?>
So, making your calculator will be a cinch.
----
Manual Page -- http://www.php.net/manual/en/control-structures.while.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33908
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33908&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33908&report=yes