I wrote a user function to return the distance between two points on an x,y coordinate system. The input params were 0,0,10,10.
Here is the original code:
public static function dist2d($x1,$y1,$x2,$y2) {
return sqrt((($x2 - $x1) * ($x2 - $x1)) + (($y2 - $y1) * ($y2 - $y1)));
}
This returns 110.
Here is the code that works:
public static function dist2d($x1,$y1,$x2,$y2) {
$result = (($x2 - $x1) * ($x2 - $x1)) + (($y2 - $y1) * ($y2 - $y1));
return sqrt($result);
}
This returns 14.1.
I am new to PHP, what is going on here?
Both of these functions return 14.142135623731.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With