Assuming the implementation of the algorithm is correct. You might be hitting IEEE754 float precision limitations.
In which case you should use the BCMath extension, which handles arbitrary decimals. Using BCMath will be easier and faster in PHP 8.4 as it has a new class that supports operator overloading.