1.0.0 • Published 9 years ago

quadratic-roots v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
9 years ago

quadratic-roots

Build Status npm version

Compute the real roots of a quadratic equation in a numerically stable manner

Introduction

In floating point arithmetic, the naive method for determining the roots of a quadratic equation ("x equals negative b, plus or minus etc.") may lead to severe cancellation and an inaccurate result.

Sample usage:

var roots = require('quadratic-roots')

roots(0,0,0)   // --> []
roots(0,0,6)   // --> []
roots(0,2,6)   // --> [-3]
roots(1,1,-12) // --> [-4,3]
roots(1,-4,4)  // --> [2,2]
roots(1,0,1)   // --> []

Install

$ npm install quadratic-roots

API

require('quadratic-roots')( a, b, c )

Compute the roots of the polynomial ax^2 + bx + c = 0.

Returns

  • If no roots, returns an empty array []
  • If a linear equation, returns one root
  • If a parabola with no real roots, returns an empty array
  • If a parabola with one root multiplicity 2, returns an array with the same root twice
  • Otherwise returns an array with two real roots

Credits

(c) 2015 Ricky Reusser. MIT License