0

I've created a function who check if a number is irrational or not:

function Verifie_infini($value) {
    if(strlen(substr(strrchr($value, "."), 1))>= 10) {
        return 1;
    } else {
        return 0;
    }
}

But it doesn't work when there are big numbers like: sqrt(1194739201) it return 0.
Have you an idea or do exist a better function?

Louis D.
  • 454
  • 6
  • 19
  • 5
    PHP already has an [is_infinite()](http://www.php.net/manual/en/function.is-infinite.php) function that actually works. `if (is_infinite($value)) ....` – Mark Baker Oct 20 '14 at 08:53
  • By "infinite" you probably mean numbers whose decimal representations are infinite, i.e. irrational numbers. I don't think this is doable, because floating point arithmetics works with rational approximations and have no notion of irrationals. – georg Oct 20 '14 at 09:18
  • Yes I would say irrational numbers (sorry for my english). I thought count decimals to suggest if it's an irrational or not number. – Louis D. Oct 20 '14 at 09:28
  • @louis67: yes, and that's not possible (think: how can you _count_ an _infinite_ number?) – georg Oct 20 '14 at 09:35
  • @georg Yes I would check if the number has more that 10 decimals for example and then say that's irrationnal. My function work for small and medium numbers but after it doesn't work. – Louis D. Oct 20 '14 at 09:39
  • @louis67: ok, can you explain what you're trying to do? What is the ultimate goal? – georg Oct 20 '14 at 10:07
  • @georg : I've created a PHP page to calculate a trinomial equation **ax²+bx+c**. And I would to show the result **x1** and **x2** with the perfect result (not a rounding value). So if the result is `(sqrt(2))/4` the program will stop and don't show the rounded value as the perfect result because he know that `(sqrt(2))/4` is not a finite number (irrational) – Louis D. Oct 20 '14 at 12:48
  • @louis67: I see. Answered below. – georg Oct 20 '14 at 13:29

2 Answers2

4

is_infinite()

Returns TRUE if val is infinite (positive or negative), like the result of log(0) or any value too big to fit into a float on this platform.

if(is_infinite($number))
{
  #your code
}
Community
  • 1
  • 1
Manwal
  • 23,450
  • 12
  • 63
  • 93
1

From your comments above, you're looking for a way to calculate mathematically exact square roots. This problem belongs to the domain of symbolic computation and cannot be solved with floating-point manipulations. Here's a (deliberately simplified) example of how to take a square root symbolically:

function factorize($n) {
    $factors = array();
    $p = 2;
    while($n > 1) {
        if($n % $p == 0) {
            $factors[$p]++;
            $n = intval($n / $p);
        } else $p++;
    }
    return $factors;
}

function symbolic_root($n) {
    $rat = $irr = 1;
    foreach(factorize($n) as $prime => $power) {
        $rat *= pow($prime, intval($power / 2));
        $irr *= pow($prime, intval($power % 2));
    }
    if($irr == 1) return $rat;
    if($rat == 1) return "sqrt $irr";    
    return "$rat * sqrt($irr)";
}

echo symbolic_root(1522756), "\n"; # prints "1234"
echo symbolic_root(5549544), "\n"; # prints "462 * sqrt(26)"

Explanation for those curious:

First, we factor the number into the prime powers:

5549544 = 23×32×72×112×131

then, divide each power by two, which gives us the rational part of the root:

462 = 21×31×71×111×130

and the rests (1s and 0s) form the irrational part

26 = 21×30×70×110×131

georg
  • 211,518
  • 52
  • 313
  • 390
  • I've not all understood but it's exactly what I'm looking for ! Now I will try to find a function to determinate if a fraction can be decimal or not (ex : 1/3 is not decimal) – Louis D. Oct 21 '14 at 10:05
  • @louis67: see if this helps: http://stackoverflow.com/questions/8946310/how-to-know-the-repeating-decimal-in-a-fraction. Good luck! – georg Oct 21 '14 at 10:13