Hi.

I'm working on a little script that will work out if a number is prime or not. However, i'm being hampered by the fact that PHP doesn't seem to be setting the correct variable types to any numbers it generates.

For example, i set 3 (which is a prime) and php recognises it as a integer. Then i use sqrt(3) and store the result as $root. $root is a double, which is fine since you can't divide three evenly.

All this is fine and good, but when i use 4, it understands that 4 is an integer, but when i get the square root as $root it thinks $root is a double.

I've used number_format and added about 70 decimal places to check if the 2 returned by sqrt was a float of some sort, but no, it's a whole number, so why is it being returned as a double?

I'm using PHP 4.2.3 on WinXP.

    I suppose i should have already mentioned that there's no actual point to using the settype function, seeing as i'm dealing with two possible types of number here, not one.

      You should use a language like JSP for a script like that!

      All u do is:

      int varName = something
      float varName = something
      short varName = something
      long varName = something
      double varName = something

      Very simple 🙂

        if(floor($x)==$x) $x is an integer. Besides, sqrt() always returns a double.

          if you want to ensure you use float numbers in all your script then when you set variables write them like this:
          $i = 2.0;
          or
          $i = (float) 2;

          instead of
          $i = 2;

            Write a Reply...