Skip to content

Repository files navigation

RootFinder.js

NPM Package MIT license

RootFinder.js is published as @rawify/rootfinder. It solves quadratic equations and cubic equations, returning either Complex.js values or plain real numbers.

Use it when all roots of a degree-two or degree-three polynomial are needed, including complex conjugate roots. Use a general polynomial solver for degree four or higher, symbolic algebra for exact radical expressions, or a numerical root finder for arbitrary functions.

Features

  • Solve quadratic equations of the form ax² + bx + c = 0.
  • Solve cubic equations of the form ax³ + bx² + cx + d = 0 using Cardano's method.
  • Support for complex roots using Complex.js.
  • Return only real roots as plain numbers when realOnly is enabled.
  • Use explicit discriminant branches for one, two, or three distinct cubic roots.

Installation

You can install RootFinder.js via npm:

npm install @rawify/rootfinder

Or with yarn:

yarn add @rawify/rootfinder

Alternatively, download or clone the repository:

git clone https://github.com/rawify/RootFinder.js

Usage

CommonJS

const RootFinder = require('@rawify/rootfinder');
const roots = RootFinder.quadratic(1, -3, 2);

The direct class export is also available through .default and .RootFinder.

ES modules

import RootFinder, { RootFinder as NamedRootFinder } from '@rawify/rootfinder';
const roots = RootFinder.quadratic(1, -3, 2);

Standalone browser script

<script src="https://cdn.jsdelivr.net/npm/@rawify/rootfinder@0.1.0/dist/rootfinder.min.js"></script>
<script>
  const roots = RootFinder.quadratic(1, -3, 2);
</script>

Native browser module

<script type="module">
  import RootFinder from 'https://cdn.jsdelivr.net/npm/@rawify/rootfinder@0.1.0/dist/rootfinder.mjs';
  const roots = RootFinder.quadratic(1, -3, 2);
</script>

The package supports Node.js 20 or newer. Complex.js is a runtime dependency of the CommonJS build; the ESM and standalone browser builds are self-contained.

Recipes

Solve a quadratic and return real numbers

Pass true as the fourth argument when complex wrapper objects are unnecessary.

import RootFinder from '@rawify/rootfinder';

const roots = RootFinder.quadratic(1, -5, 6, true);
console.log(roots); // [3, 2]

Coefficient order is descending: a*x^2 + b*x + c. If a is near zero, the implementation treats the equation as linear; an equation with no determined root returns an empty array.

Keep complex quadratic roots

With the default realOnly = false, every root is returned as a Complex.js value.

import RootFinder from '@rawify/rootfinder';

const roots = RootFinder.quadratic(1, 0, 1);
console.log(roots.map(String)); // ['i', '-i']

Setting realOnly to true omits non-real roots rather than returning their real parts.

Solve a cubic and verify approximate roots

Cubic solutions use floating-point arithmetic, so callers should compare with a tolerance.

import RootFinder from '@rawify/rootfinder';

const roots = RootFinder.cubic(1, -6, 11, -6, true)
  .sort((left, right) => left - right);

console.log(roots); // [1, 1.9999999999999998, 3]
console.log(roots.every((x) => Math.abs(x ** 3 - 6 * x ** 2 + 11 * x - 6) < 1e-12)); // true

Repeated roots appear once or twice according to the algebraic branch, not necessarily once per multiplicity. Inputs and outputs are JavaScript numbers, so this is not an exact-arithmetic solver.

Cubic algorithm

The cubic is normalized and transformed into the depressed form

t^3 + p*t + q = 0

The implementation keeps Cardano's method conditional on

D = (q / 2)^2 + (p / 3)^3
  • D > 0: one real root and one complex-conjugate pair.
  • D = 0: one triple root, or one single and one double root.
  • D < 0: three distinct real roots, computed trigonometrically to avoid complex branch ambiguity.

Values with Math.abs(D) < 1e-14 use the D = 0 branch to absorb ordinary floating-point noise.

Solving Quadratic Equations

To find the roots of a quadratic equation ax² + bx + c = 0:

const roots = RootFinder.quadratic(1, -3, 2);
console.log(roots); // Output: [ Complex { re: 2 }, Complex { re: 1 } ]

If the equation has complex roots:

const complexRoots = RootFinder.quadratic(1, 0, 1);
console.log(complexRoots); // Output: [ Complex { re: 0, im: 1 }, Complex { re: 0, im: -1 } ]

Solving Cubic Equations

To find the roots of a cubic equation ax³ + bx² + cx + d = 0:

const roots = RootFinder.cubic(1, -6, 11, -6);
console.log(roots); // Output: [ Complex { re: 1 }, Complex { re: 2 }, Complex { re: 3 } ]

For cubic equations with complex roots:

const complexRoots = RootFinder.cubic(1, 0, 0, -1);
console.log(complexRoots); // Output: [ Complex { re: 1, im: 0 }, Complex { re: -0.5, im: 0.866 }, Complex { re: -0.5, im: -0.866 } ]

API

quadratic(a, b, c[, realOnly=false])

Solves the quadratic equation ax² + bx + c = 0.

  • Parameters:

    • a (Number): Coefficient of
    • b (Number): Coefficient of x
    • c (Number): Constant term
    • realOnly (optional Boolean): Return real roots as numbers and omit non-real roots
  • Returns: An array of roots, which can contain real or complex numbers.

cubic(a, b, c, d[, realOnly=false])

Solves the cubic equation ax³ + bx² + cx + d = 0 using Cardano's method.

  • Parameters:

    • a (Number): Coefficient of
    • b (Number): Coefficient of
    • c (Number): Coefficient of x
    • d (Number): Constant term
    • realOnly (optional Boolean): Return real roots as numbers and omit non-real roots
  • Returns: An array of roots, which can contain real or complex numbers.

Building the library

The source is strict TypeScript. The build emits CommonJS, ESM, a standalone browser bundle, source maps, and format-specific declarations.

After cloning the Git repository, run:

npm install
npm run build

Run a test

Testing the source against the shipped test suite is as easy as

npm run test

Copyright and Licensing

Copyright (c) 2026, Robert Eisele Licensed under the MIT license.

About

The RAW root finder library for quadratic and cubic polynomials

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages