0

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,

Bill
  • 561
  • 6
  • 15

5 Answers5

4

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? :)

corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • +1 Good idea. Note that this will likely fail if `original value` is a float (unclear from the question), as you cannot reliably compare floats with `==` (in any language). – sleske Mar 01 '11 at 23:57
  • Yes, I like this one. It'll work because the original value is an integer. Thanks! – Bill Mar 02 '11 at 00:21
3

That is a common problem when working with floating point. Just check that the float you get is very close to an integer; commonly this is done by checking that the fractional part is very small:

if (abs(round(f)-f))<delta)
  # do stuff

Here delta is a small constant, such as 0.0001. How small it must be depends on how close you expect your result to be to an integer. That will depend on your calculations.

sleske
  • 81,358
  • 34
  • 189
  • 227
  • This is a good idea, too (very "Calculusesque"!). A good trick to know. I have to think about how much precision would be necessary... – Bill Mar 02 '11 at 00:23
  • @Bill: Thanks. Actually, it's more numerical analysis than calculus. And yes, figuring out the precision is the tricky part... that is what people doing numerical analysis sometimes spend years figuring out... – sleske Mar 02 '11 at 00:29
0

$result % 1e9 will return an integer part of $result (for negatives it will find the ceil of result). But it is a hack, so you might use it only for fun.

https://repl.it/Chqm/1

Andrey Bessonov
  • 338
  • 1
  • 7
0

This knowledge is from programming in another language but have you tried to multiply the expression by 1. for example if you needed to get the integer of something like

a = 3.45 * 8;

do this

a = 3.45 * 8 * 1;
Pang
  • 9,564
  • 146
  • 81
  • 122
0

Something along the lines of if($result % 1.0 == 0) might work, but I don't have the capability to test it right now.

TheJubilex
  • 409
  • 3
  • 9
  • This seems like it should work, though I'm not sure about floating point issues that Sleske pointed out. – Bill Mar 02 '11 at 00:24
  • -1 This does not work. PHP's `%` operator only works with integers. Thus `$result % 1.0` will return 0 for any input. You want function `fmod` (floatin point modulus). But even then you should not compare the result using `==`, as that is almost never safe to do for fp numbers. See e.g. http://stackoverflow.com/questions/4682889/is-floating-point-ever-ok – sleske Mar 03 '11 at 11:15