best way to compare float numbers in this math formula [closed]

I have a code and I know it has problem with float numbers.

Please somebody write the whole code without float problems…

if ($x2 - $x1 !== 0.0) 
{
    $pq = ($y1 - $y2) / ($x1 - $y1);
}

Answer

It’s not the most correct way to compare floating point values.

You can use a smallest acceptable difference:

if (abs(($x2-$x1)/$x1) < 0.00001) {
  //Do something 
}

Leave a Reply

Your email address will not be published. Required fields are marked *