{"id":63,"date":"2019-08-30T19:40:00","date_gmt":"2019-08-30T19:40:00","guid":{"rendered":"https:\/\/tutorialio.com\/?p=63"},"modified":"2019-08-30T19:40:00","modified_gmt":"2019-08-30T19:40:00","slug":"miscellaneous-maths-functions-in-php","status":"publish","type":"post","link":"https:\/\/hellonitish.com\/blog\/miscellaneous-maths-functions-in-php\/","title":{"rendered":"Miscellaneous Maths Functions in PHP"},"content":{"rendered":"<table class=\"quick-links\">\n<caption>Miscellaneous Maths Functions in PHP<\/caption>\n<\/caption>\n<tbody>\n<tr>\n<th>Function Name<\/th>\n<th>Function Description<\/th>\n<\/tr>\n<tr>\n<td><a href=\"#deg2rad\">deg2rad()<\/a><\/td>\n<td>Converts the number in degrees to the radian equivalent<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#hypot\">hypot()<\/a><\/td>\n<td>Calculate the length of the hypotenuse of a right-angle triangle<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#intdiv\">intdiv()<\/a><\/td>\n<td>Integer division<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#lcgvalue\">lcg_value()<\/a><\/td>\n<td>Combined linear congruential generator<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#pi\">pi()<\/a><\/td>\n<td>Get value of pi<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#rad2deg\">rad2deg()<\/a><\/td>\n<td>Converts the radian number to the equivalent number in degrees<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"deg2rad\">PHP <code>deg2rad()<\/code> Function<\/h2>\n<h3>PHP <code>deg2rad()<\/code> Usage<\/h3>\n<p>The PHP <code><code>deg2rad()<\/code><\/code> function will converts the number in degrees to the radian equivalent .<\/p>\n<h3>PHP <code>deg2rad()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> deg2rad ( float $number ) : float<\/code><\/pre>\n<h3>PHP <code>deg2rad()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>number<\/code> \u2014 Angular value in degrees <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>deg2rad()<\/code> Return Value<\/h3>\n<p>The PHP <code>deg2rad()<\/code> function returns the radian equivalent of <code class=\"parameter\">number<\/code><\/p>\n<h3>PHP <code>deg2rad()<\/code> Working Examples<\/h3>\n<h4>1. deg2rad() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho deg2rad(45); \/\/ 0.785398163397\nvar_dump(deg2rad(45) === M_PI_4); \/\/ bool(true)\n?&gt;<\/code><\/pre>\n<h2 id=\"hypot\">PHP <code>hypot()<\/code> Function<\/h2>\n<h3>PHP <code>hypot()<\/code> Usage<\/h3>\n<p>The PHP <code><code>hypot()<\/code><\/code> function will calculate the length of the hypotenuse of a right-angle triangle .<\/p>\n<h3>PHP <code>hypot()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> hypot ( float $x , float $y ) : float<\/code><\/pre>\n<h3>PHP <code>hypot()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>x<\/code> \u2014 Length of first side <\/p>\n<\/li>\n<li>\n<p><code>y<\/code> \u2014 Length of second side <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>hypot()<\/code> Return Value<\/h3>\n<p>The PHP <code>hypot()<\/code> function returns calculated length of the hypotenuse<\/p>\n<h2 id=\"intdiv\">PHP <code>intdiv()<\/code> Function<\/h2>\n<h3>PHP <code>intdiv()<\/code> Usage<\/h3>\n<p>The PHP <code><code>intdiv()<\/code><\/code> function will give you the integer quotient of the division of <code class=\"parameter\">dividend<\/code> by <code class=\"parameter\">divisor<\/code>.<\/p>\n<h3>PHP <code>intdiv()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> intdiv ( int $dividend , int $divisor ) : int<\/code><\/pre>\n<h3>PHP <code>intdiv()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>dividend<\/code> \u2014 Number to be divided. <\/p>\n<\/li>\n<li>\n<p><code>divisor<\/code> \u2014 Number which divides the dividend. <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>intdiv()<\/code> Return Value<\/h3>\n<p>The PHP <code>intdiv()<\/code> function returns the integer quotient of the division of <code class=\"parameter\">dividend<\/code> by <code class=\"parameter\">divisor<\/code>.<\/p>\n<h3>PHP <code>intdiv()<\/code> Working Examples<\/h3>\n<h4>1. intdiv() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\nvar_dump(intdiv(3, 2));\nvar_dump(intdiv(-3, 2));\nvar_dump(intdiv(3, -2));\nvar_dump(intdiv(-3, -2));\nvar_dump(intdiv(PHP_INT_MAX, PHP_INT_MAX));\nvar_dump(intdiv(PHP_INT_MIN, PHP_INT_MIN));\nvar_dump(intdiv(PHP_INT_MIN, -1));\nvar_dump(intdiv(1, 0));\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">int(1)\nint(-1)\nint(-1)\nint(1)\nint(1)\nint(1)\nFatal error: Uncaught ArithmeticError: Division of PHP_INT_MIN by -1 is not an integer in %s on line 8\nFatal error: Uncaught DivisionByZeroError: Division by zero in %s on line 9<\/code><\/pre>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: AmeenRoss<\/p>\n<pre><code>This does indeed seem to be equal to intdiv:\n<br>&lt;?php\nfunction intdiv_1($a, $b){\n    return ($a - $a % $b) \/ $b;\n}\n?&gt;<br>\nHowever, this isn't:\n<br>&lt;?php\nfunction intdiv_2($a, $b){\n    return floor($a \/ $b);\n}\n?&gt;<br>\nConsider an example where either of the parameters is negative:\n<br>&lt;?php\n$param1 = -10;\n$param2 = 3;\nprint_r([\n    'modulus' =&gt; intdiv_1($param1, $param2),\n    'floor' =&gt; intdiv_2($param1, $param2),\n]);\n\/**\n * Array\n * (\n *     [modulus] =&gt; -3\n *     [floor] =&gt; -4\n * )\n *\/\n?&gt;<br><\/pre>\n<p><\/code><\/p>\n<h2 id=\"lcg_value\">PHP <code>lcg_value()<\/code> Function<\/h2>\n<h3>PHP <code>lcg_value()<\/code> Usage<\/h3>\n<p>The PHP <code><code>lcg_value()<\/code><\/code> function will give you a pseudo random number in the range of (0, 1). The function combines two CGs with periods of 2^31 - 85 and 2^31 - 249. The period of this function is equal to the product of both primes.<\/p>\n<h3>PHP <code>lcg_value()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> lcg_value ( void ) : float<\/code><\/pre>\n<h3>PHP <code>lcg_value()<\/code> Parameters<\/h3>\n<ol><\/ol>\n<p>This function does not accept any parameters.<\/p>\n<h3>PHP <code>lcg_value()<\/code> Return Value<\/h3>\n<p>The PHP <code>lcg_value()<\/code> function returns a pseudo random float value between 0.0 and 1.0, inclusive.<\/p>\n<h3>Important Points about PHP <code>lcg_value()<\/code> Function<\/h3>\n<ol>\n<li>\n<p>This function does not generate cryptographically secure values, and should not be used for cryptographic purposes. If you need a cryptographically secure value, consider using <code>random_int()<\/code>, <code>random_bytes()<\/code>, or <code>openssl_random_pseudo_bytes()<\/code> instead.<\/p>\n<\/li>\n<\/ol>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: rok kralj gmail com<\/p>\n<pre><code>An elegant way to return random float between two numbers:\n<br>&lt;?php\nfunction random_float ($min,$max) {\n   return ($min+lcg_value()*(abs($max-$min)));\n}\n?&gt;<br><\/pre>\n<p><\/code><\/p>\n<p>Contributed By: daniel dot baulig<\/p>\n<pre><code>Choose your weapon:\n<br>&lt;?php\nfunction mt_randf($min, $max)\n{\n    return $min + abs($max - $min) * mt_rand(0, mt_getrandmax())\/mt_getrandmax(); \n}\nfunction lcg_randf($min, $max)\n{\n    return $min + lcg_value() * abs($max - $min);\n}\nfunction randf($min, $max)\n{\n    return $min + rand(0,getrandmax()) \/ getrandmax() * abs($max - $min);\n}?&gt;<br><\/pre>\n<p><\/code><\/p>\n<h2 id=\"pi\">PHP <code>pi()<\/code> Function<\/h2>\n<h3>PHP <code>pi()<\/code> Usage<\/h3>\n<p>The PHP <code><code>pi()<\/code><\/code> function will give you an approximation of pi. Also, you can use the <code>M_PI<\/code> constant which yields identical results to <code>pi()<\/code>.<\/p>\n<h3>PHP <code>pi()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> pi ( void ) : float<\/code><\/pre>\n<h3>PHP <code>pi()<\/code> Parameters<\/h3>\n<ol><\/ol>\n<p>This function does not accept any parameters.<\/p>\n<h3>PHP <code>pi()<\/code> Return Value<\/h3>\n<p>The PHP <code>pi()<\/code> function returns the value of pi as float.<\/p>\n<h3>PHP <code>pi()<\/code> Working Examples<\/h3>\n<h4>1. pi() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho pi(); \/\/ 3.1415926535898\necho M_PI; \/\/ 3.1415926535898\n?&gt;<\/code><\/pre>\n<h2 id=\"rad2deg\">PHP <code>rad2deg()<\/code> Function<\/h2>\n<h3>PHP <code>rad2deg()<\/code> Usage<\/h3>\n<p>The PHP <code><code>rad2deg()<\/code><\/code> function will converts the radian number to the equivalent number in degrees .<\/p>\n<h3>PHP <code>rad2deg()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> rad2deg ( float $number ) : float<\/code><\/pre>\n<h3>PHP <code>rad2deg()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>number<\/code> \u2014 A radian value <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>rad2deg()<\/code> Return Value<\/h3>\n<p>The PHP <code>rad2deg()<\/code> function returns the equivalent of <code class=\"parameter\">number<\/code> in degrees<\/p>\n<h3>PHP <code>rad2deg()<\/code> Working Examples<\/h3>\n<h4>1. rad2deg() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho rad2deg(M_PI_4); \/\/ 45\n?&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Miscellaneous Maths Functions in PHP Function Name Function Description deg2rad() Converts the number in degrees to the radian equivalent hypot() Calculate the length of the hypotenuse of a right-angle triangle intdiv() Integer division lcg_value() Combined linear congruential generator pi() Get value of pi rad2deg() Converts the radian number to the equivalent number in degrees PHP [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[8,5],"class_list":["post-63","post","type-post","status-publish","format-standard","hentry","category-reference","tag-math","tag-php"],"_links":{"self":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/63","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/comments?post=63"}],"version-history":[{"count":0,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/63\/revisions"}],"wp:attachment":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/media?parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/categories?post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/tags?post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}