How to prevent division by zero in PHP?

S

soerenfleischer

Guest
Is there a way to prevent a division by 0 in PHP?

I'm making a graph-drawer, which crashes if a function has pole-points, because those cause divisions by zero...

I'm afraid the functions is_infinite() and is_finite() are not available in the "normal" version of PHP...


Any ideas?
Thanks in advance!
 
S

ShockingAlberto

Guest
What's wrong with a simple if statement?
if($x>0){
$x=$x/$y
}
 
S

soerenfleischer

Guest
The problem is, the graph-drawer should accept user-entered functions. And functions can have more than just one factor under the fraction line and can contain many other factors, so the problem can't be solved like that... :(


BTW, you mean
if($y>0){
$x=$x/$y
}
right?
 
T

Teaser

Guest
Well surely you need to write a function which calculates what values will give you the demoninator equal to zero, i.e equate the denominator to 0 and solve. The values for $x (or whatever the variable is - assuming theres only one) you get from this will be those which make the denomiator zero.

When drawing the graph make sure you aren't using of these values.

Not a 10 minute job, but it would work.
 

Users who are viewing this thread

Similar threads

S
Replies
5
Views
734
L_Plates
L
T
Replies
3
Views
555
TheJkWhoSaysNi
T
S
Replies
2
Views
541
wyrd_fish
W
W
Replies
8
Views
1K
wyrd_fish
W
P
Replies
3
Views
729
wyrd_fish
W
Top Bottom