Bill Bill - 4 months ago 21
PHP Question

php sqrt function that returns an integer?

I'm creating a simple PHP program that does an action if (and only if) three random numbers compute to an integer. The random numbers are all integers, created with the rand() function. Without going into the specific details of the computation, the important thing (in terms of my problem) is that it includes the taking of a square root. Not being a very experienced PHP coder, the only square root function I know is sqrt(). The problem is, sqrt() returns a float, even when the input is an integer and output is exact. I briefly thought about converting the output to an integer (using something like intval(), but that won't work because that will convert all outputs to integers, making the test useless! Any ideas on how to do this?
Thanks,

Answer

If you just want to determine if it is a perfect square, just determine if the

intval(result) * intval(result) == originalValue

I don't know the php version of those functions, but perhaps you do? :)