pocketmine\math\Math::solveQuadratic PHP Method

solveQuadratic() public static method

public static solveQuadratic ( $a, $b, $c ) : array
return array
    public static function solveQuadratic($a, $b, $c) : array
    {
        $x[0] = (-$b + sqrt($b ** 2 - 4 * $a * $c)) / (2 * $a);
        $x[1] = (-$b - sqrt($b ** 2 - 4 * $a * $c)) / (2 * $a);
        if ($x[0] == $x[1]) {
            return [$x[0]];
        }
        return $x;
    }

Usage Example

Esempio n. 1
0
 /**
  * Converts a quantity of exp into a level and a progress percentage
  *
  * @param int $xp
  *
  * @return int[]
  */
 public static function getLevelFromXp(int $xp) : array
 {
     $xp &= 0x7fffffff;
     /** These values are correct up to and including level 16 */
     $a = 1;
     $b = 6;
     $c = -$xp;
     if ($xp > self::getTotalXpRequirement(16)) {
         /** Modify the coefficients to fit the relevant equation */
         if ($xp <= self::getTotalXpRequirement(31)) {
             /** Levels 16-31 */
             $a = 2.5;
             $b = -40.5;
             $c += 360;
         } else {
             /** Level 32+ */
             $a = 4.5;
             $b = -162.5;
             $c += 2220;
         }
     }
     $answer = max(Math::solveQuadratic($a, $b, $c));
     //Use largest result value
     $level = floor($answer);
     $progress = $answer - $level;
     return [$level, $progress];
 }