note 116510 added to book.bc
| From: | volekatadamvdotcz | Date: | Thu, 15 Jan 2015 13:44:51 +0000 |
| Subject: | note 116510 added to book.bc | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-201431@lists.php.net to get a copy of this message | ||
Note that when you use implementation of factorial that ClaudiuS made, you get results even if you
try to calculate factorial of number that you normally can't, e.g. 2.5, -2, etc. Here is safer
implementation:
<?php
/**
* Calculates a factorial of given number.
* @param string|int $num
* @throws InvalidArgumentException
* @return string
*/
function bcfact($num)
{
if (!filter_var($num, FILTER_VALIDATE_INT) || $num <= 0) {
throw new InvalidArgumentException(sprintf('Argument must be natural number,
"%s" given.', $num));
}
for ($result = '1'; $num > 0; $num--) {
$result = bcmul($result, $num);
}
return $result;
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 213.192.60.53
----
Manual Page -- http://php.net/manual/en/book.bc.php
Edit -- https://master.php.net/note/edit/116510
Del: integrated -- https://master.php.net/note/delete/116510/integrated
Del: useless -- https://master.php.net/note/delete/116510/useless
Del: bad code -- https://master.php.net/note/delete/116510/bad+code
Del: spam -- https://master.php.net/note/delete/116510/spam
Del: non-english -- https://master.php.net/note/delete/116510/non-english
Del: in docs -- https://master.php.net/note/delete/116510/in+docs
Del: other reasons-- https://master.php.net/note/delete/116510
Reject -- https://master.php.net/note/reject/116510
Search -- https://master.php.net/manage/user-notes.php