PHPackages                             gamajo/quadratic - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. gamajo/quadratic

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

gamajo/quadratic
================

Handle solving of quadratic equations.

1.0.0(10y ago)3362MITPHPPHP &gt;=7CI failing

Since May 2Pushed 3y ago1 watchersCompare

[ Source](https://github.com/gamajo/quadratic)[ Packagist](https://packagist.org/packages/gamajo/quadratic)[ RSS](/packages/gamajo-quadratic/feed)WikiDiscussions develop Synced 4w ago

READMEChangelog (1)Dependencies (1)Versions (3)Used By (0)

Gamajo Quadratic Equation Solver
================================

[](#gamajo-quadratic-equation-solver)

PHP classes for working with, and solving, quadratic equations.

What Is A Quadratic Equation?
-----------------------------

[](#what-is-a-quadratic-equation)

A quadratic equation is a univariate polynomial with degree of 2, that is then set to equal zero so that the indeterminate can be determined:

> *ax*2 + *bx* + c = 0

Equations can often be solved with [factoring](https://en.wikipedia.org/wiki/Factorization) or [completing the square](https://en.wikipedia.org/wiki/Completing_the_square), but there is also a [Quadratic Formula](https://en.wikipedia.org/wiki/Quadratic_formula):

[![X equals negative B, plus or minus the square root of B squared minus four A C, all over two A](https://camo.githubusercontent.com/3d97cc97512f6d32df71d2f07d6d4839645b792eedda223495af271f35d2f6cd/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f636f6d6d6f6e732f7468756d622f632f63342f5175616472617469635f666f726d756c612e7376672f36343070782d5175616472617469635f666f726d756c612e7376672e706e673f31343434343330303433393032 "Quadratic Formula")](https://camo.githubusercontent.com/3d97cc97512f6d32df71d2f07d6d4839645b792eedda223495af271f35d2f6cd/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f636f6d6d6f6e732f7468756d622f632f63342f5175616472617469635f666f726d756c612e7376672f36343070782d5175616472617469635f666f726d756c612e7376672e706e673f31343434343330303433393032)

The QuadraticEquation interface constructor accepts values for *a*, *b* and *c* and the Solver can return one or both roots, including imaginary roots.

Installation
------------

[](#installation)

```
composer require gamajo/quadratic
```

Requires PHP 7.

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

Create an equation object, pass that to the solver, solve, and retrieve the roots:

```
use Gamajo\Quadratic;

// Represents x^2 + 5x + 6 = 0.
$equation = new BasicQuadraticEquation(1, 5, 6);

$solver = new Solver($equation);
$solver->solve();

echo $solver->get(); // '2 and 3'
echo $solver->get('root1'); // '2'
echo $solver->get('root2'); // '3'
```

The Solver has no problem with complex roots:

```
use Gamajo\Quadratic;

// Represents 3x^2 + 4x + 5 = 0.
$equation = new BasicQuadraticEquation(3, 4, 5);

$solver = new Solver($equation);
$solver->solve();

echo $solver->get(); // '-0.667 + 1.106i and -0.667 - 1.106i'
```

### `BasicQuadraticEquation` Methods

[](#basicquadraticequation-methods)

The `BasicQuadraticEquation` class implements the `QuadraticEquation` interface, which in turn extends the `Equation` interface. As such, it supports the following methods:

```
use Gamajo\Quadratic;

// Represents x^2 + 5x + 6 = 0.
$equation = new BasicQuadraticEquation(1, 5, 6);

echo $equation->getA(); // 1
echo $equation->getB(); // 5
echo $equation->getC(); // 6
print_r( $equation->getArgsAsArray() ); // [1, 5, 6]
```

There is also a `hasValidArguments()` method, but as this library uses scalar type declarations, the manual check to see if they are all integers is somewhat redundant.

### `Solver` Methods

[](#solver-methods)

The `Solver` class can optionally set the maximum decimal place precision of the roots:

```
use Gamajo\Quadratic;

// Represents 8x^2 + 5x - 2 = 0.
$equation = new BasicQuadraticEquation(8, 5, -2);

$solver = new Solver($equation);
$solve->setPrecision(4); // Default precision is 3

echo $solve->getPrecision(); // 4

$solver->solve();

echo $solver->get(); // '-0.9021 and 0.2771' instead of '-0.902 and 0.277'
```

Change Log
----------

[](#change-log)

See the [change log](CHANGELOG.md).

License
-------

[](#license)

[MIT](LICENSE).

Contributions
-------------

[](#contributions)

Contributions are welcome - fork, fix and send pull requests against the `develop` branch please.

Credits
-------

[](#credits)

Built by [Gary Jones](https://twitter.com/GaryJ).
Original procedural code version, copyright 2004 Gary Jones.
This version copyright 2016 [Gamajo Tech](https://gamajo.com)

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 95.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3710d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ba7751eebec0f74a4963e9a062f49e58222b97872bf193d95f0505f3df321d09?d=identicon)[GaryJones](/maintainers/GaryJones)

---

Top Contributors

[![GaryJones](https://avatars.githubusercontent.com/u/88371?v=4)](https://github.com/GaryJones "GaryJones (23 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/gamajo-quadratic/health.svg)

```
[![Health](https://phpackages.com/badges/gamajo-quadratic/health.svg)](https://phpackages.com/packages/gamajo-quadratic)
```

###  Alternatives

[mattwright/urlresolver

PHP class that attempts to resolve URLs to a final, canonical link.

66169.0k](/packages/mattwright-urlresolver)[stuttter/wp-user-profiles

A sophisticated way to edit users in WordPress

11119.5k1](/packages/stuttter-wp-user-profiles)[viest/php-ext-xlswriter-ide-helper

PHP Extension Xlswriter IDE Helper

16497.1k33](/packages/viest-php-ext-xlswriter-ide-helper)[smile/module-custom-entity-product-link

Smile - Custom Entity Product link Module

3269.9k1](/packages/smile-module-custom-entity-product-link)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
