mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-22 00:33:59 +00:00
Math: Added solveQuadratic()
will be used in future for XP. This is extracted from the experience work branch.
This commit is contained in:
parent
5c37d298a6
commit
debfbf0d93
@ -38,4 +38,30 @@ abstract class Math{
|
||||
$i = (int) ($n + 1);
|
||||
return $n >= $i ? $i : $i - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Solves a quadratic equation with the given coefficients and returns an array of up to two solutions.
|
||||
*
|
||||
* @param float $a
|
||||
* @param float $b
|
||||
* @param float $c
|
||||
*
|
||||
* @return float[]
|
||||
*/
|
||||
public static function solveQuadratic(float $a, float $b, float $c) : array{
|
||||
$discriminant = $b ** 2 - 4 * $a * $c;
|
||||
if($discriminant > 0){ //2 real roots
|
||||
$sqrtDiscriminant = sqrt($discriminant);
|
||||
return [
|
||||
(-$b + $sqrtDiscriminant) / (2 * $a),
|
||||
(-$b - $sqrtDiscriminant) / (2 * $a)
|
||||
];
|
||||
}elseif($discriminant == 0){ //1 real root
|
||||
return [
|
||||
-$b / (2 * $a)
|
||||
];
|
||||
}else{ //No real roots
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user