Home > Backend Development > PHP Tutorial > PHP sqrt() function

PHP sqrt() function

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-08-20 19:42:02
forward
1823 people have browsed it

PHP sqrt() 函数

Definition and usage

sqrt()The function returns the square root of a positive floating point number. Since the square root of a negative number is undefined, it returns NAN. This is one of the most commonly used functions.

This function always returns a floating point number.

Syntax

sqrt ( float $arg ) : float
Copy after login

Parameters

Serial numberParameters and description
1arg

The number whose square root is required

Return value

PHP sqrt() function returns the square root of the given arg number. For negative numbers, the function returns NAN.

PHP Version

This function is available in PHP 4.x, PHP 5.x and PHP 7.x versions.

Example

Online Demonstration

The following example calculates the square root of 100−

<?php
   $arg = 100;
   echo "Square root of " . $arg . "=" . sqrt($arg) . "</p><p>";
?>
Copy after login

Output

This will produce the following result−

Square root of 100=10
Copy after login

Example

Demonstration

For sqrt(2), 1/sqrt(2) and sqrt(3), PHP has special predefined constants M_SQRT2, M_SQRT1_2 and M_SQRT3 respectively. −

<?php
   echo "sqrt(2) = " . sqrt(2) . "</p><p>";
   echo "M_SQRT2 = " . M_SQRT2. "</p><p>";
   echo "sqrt(3) = " . sqrt(3) . "</p><p>";
   echo "M_SQRT3 = " . M_SQRT3 . "</p><p>";
   echo "1/sqrt(2)) = " . 1/sqrt(2) . "</p><p>";
   echo "M_SQRT1_2 = " . M_SQRT1_2 . "</p><p>";
?>
Copy after login

Output

This will produce the following results −

sqrt(2) = 1.4142135623731
M_SQRT2 = 1.4142135623731
sqrt(3) = 1.7320508075689
M_SQRT3 = 1.7320508075689
1/sqrt(2)) = 0.70710678118655
M_SQRT1_2 = 0.70710678118655
Copy after login

Example

Example

The mathematical constants M_SQRTPI and M_2_SQRTPI represent sqrt respectively (Π) and 2/sqrt(Π) −

<?php
   echo "sqrt(pi) = " . sqrt(M_PI) . "</p><p>";
   echo "M_SQRTPI = " . M_SQRTPI. "</p><p>";
   echo "2/sqrt(pi) = " . 2/sqrt(M_PI) . "</p><p>";
   echo "M_2_SQRTPI = " . M_2_SQRTPI . "</p><p>";
?>
Copy after login

Output

This will produce the following −

sqrt(pi) = 1.7724538509055
M_SQRTPI = 1.7724538509055
2/sqrt(pi) = 1.1283791670955
M_2_SQRTPI = 1.1283791670955
Copy after login

Example

Demonstration

sqrt(-1) is undefined and therefore returns NAN

<?php
   echo "sqrt(-1) = " . sqrt(-1) . "</p><p>";
?>
Copy after login

output

This will produce the following result−

sqrt(-1) = NAN
Copy after login

The above is the detailed content of PHP sqrt() function. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template