The recommendation from my numerical analysis course was to use a "delta" value when comparing, depending on "how close" the values needed to be for you to decide they were equal.
Something like
while ($i - $j < $delta) {
...
}
The trick is selecting a delta that works. For instance if
$i was
2E40 and
$j was
1E40, selecting a delta of
1E-40 probably wouldn't work - it's going to depend on how many significant figures your platform's floating point numbers can represent (the example above would require 80 significant figures, if I'm remembering all this right: 40 in front of the decimal point and 40 after it). You should carefully check out your platform's floating point representation and precision before embarking on adventures in floating point computations.
This is why many financial calculations in which it was necessary to be precise about exact cents used to be (and probably are still) done in cents rather than in fractional dollars - you can precisely add and subtract integers as long as you have enough digits to represent the amounts you care about.