Wednesday, July 3, 2024
HomeLanguagesPhpPHP | gmp_sqrt() Function

PHP | gmp_sqrt() Function

The gmp_sqrt() is a built-in function in PHP which is used to calculate the square root of a GMP number (GNU Multiple Precision : For large numbers). This function returns only the integral part of the square root of the GMP number.

Syntax:

gmp_sqrt ( $num )

Parameters: This function accepts a GMP number $num as a mandatory parameter as shown in the above syntax. This parameter can be a GMP object in PHP version 5.6 and later, or we are also allowed to pass a numeric string provided that it is possible to convert that string to a number.

Return Value: This function returns a GMP number which is the square root of a GMP number passed to it as parameter. This function only returns the integral part of the square root of the GMP number passed to it as argument.

Examples:

Input : "9"
Output : 3

Input : "24"
Output : 4

Below programs illustrate the gmp_sqrt() function in PHP :

Program 1: Program to calculate the square root of a GMP number when numeric strings as GMP numbers are passed as arguments.




<?php
// PHP program to calculate the square root 
// of a GMP number passed as arguments 
  
// strings as GMP numbers 
$num1 = "9";
$num2 = "24";
  
// calculates the square root of a GMP number
$squareRoot = gmp_sqrt($num1);
echo $squareRoot."\n";
  
// calculates the square root of a GMP number
$squareRoot = gmp_sqrt($num2);
echo $squareRoot."\n";
  
?>


Output:

3
4

Program 2: Program to calculate the square root of a GMP number when GMP numbers are passed as arguments.




<?php
// PHP program to calculate the square root 
// of a GMP number
  
// creating GMP numbers using gmp_init()
$num1 = gmp_init(9, 10);
$num2 = gmp_init(24, 10);
  
// calculates the square root of a GMP number
$squareRoot = gmp_sqrt($num1);
echo $squareRoot."\n";
  
// calculates the square root of a GMP number
$squareRoot = gmp_sqrt($num2);
echo $squareRoot."\n";
  
?>


Output:

3
4

Reference:
http://php.net/manual/en/function.gmp-sqrt.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Last Updated :
14 Apr, 2018
Like Article
Save Article

Similar Reads
Related Tutorials
Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments