Square root in PHP
Asked Answered
D

6

8

Why is the output of sqrt not an integer for "16" in PHP?

Example

php > $fig = 16;
php > $sq = sqrt($fig); //should be 4
php > echo $sq;
4
php > echo is_int($sq);            // should give 1, but gives false
php > 

I feel that the problem is in the internal presentation which PHP hides similarly as Python. How can you then know when the given figure is integer after taking a square root?

So how can you differentiate between 4 and 4.12323 in PHP without using a regex?

Dorladorlisa answered 31/10, 2009 at 14:18 Comment(0)
S
17

According to the PHP manual, float sqrt ( float $arg ), sqrt() always returns a float. Using the is_int() function won't solve the problem because it checks the datatype and returns a failure.

To get around this, you can test it by using modulus instead: (must be fmod() for floating point modulus and not the % operator for integer modulus)

if (fmod(sqrt(16), 1) == 0) {
   // is an integer
}

If you are using PHP 5.2.0 or later, I believe this would also work, but I haven't used it in this type of circumstance to be certain:

$result = sqrt(16);
if (filter_var($result, FILTER_VALIDATE_INT)) {
    // is an integer
}
Salmi answered 31/10, 2009 at 14:25 Comment(4)
Your first command is not working for me, but the last one works.Conformable
You're right. The first one works now too. I double checked it before I posted the edit.Salmi
As a side note, testing for an integer by using (float % 1) works in JavaScript and MySQL, just not in PHP which requires fmod() instead of %.Salmi
I didn't know that FILTER_VALIDATE_INT existed, and had written my own function. check_is_integer($n) {$i = (int)$n; return ($n == $i);}Heyerdahl
W
9

No, it's not an integer. It's a float.

Wodge answered 31/10, 2009 at 14:19 Comment(0)
L
5

Says it right in the API, return type is float.

https://www.php.net/sqrt

 float sqrt  ( float $arg  )

Returns the square root of arg .

Letty answered 31/10, 2009 at 14:22 Comment(0)
P
3

You can use the floor function to get the integer part of the value and subtract the original value. If the difference is != 0 then its NOT an integer. e.g.

if (($sq - floor($sq)) == 0){
   YES
}else{
 NO
}
Pawnshop answered 31/10, 2009 at 14:29 Comment(1)
This is also a good method. Thank you for pointing out Knuth's function!Conformable
S
2

Because it always returns a float:

float sqrt(float $arg)

You can cast it into a integer if you want:

intval(sqrt(16));

EDIT, Ok then:

$sqrt = sqrt(16);

if (strpos($sqrt, '.') !== false)
{
    $sqrt = intval($sqrt);
}

else
{
    $sqrt = floatval($sqrt);
}

var_dump($sqrt);
Simplistic answered 31/10, 2009 at 14:20 Comment(1)
The problem with intval is that it rounds off. I need to know exactly which is an integer which is not after taking a square root.Conformable
H
0

Another way to check for integers would be casting to a string and checking that with ctype_digit()

$result = sqrt(16);
if (ctype_digit((string)$result)) {
   // is an integer
}

Of course this is a somewhat weird way compared to using modulo when you are doing calculations anyway. But it is handy when testing values posted from a form, as they will be strings to begin with. A caveat is that it would return false for a negative integer.

Hollingshead answered 31/10, 2009 at 14:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.