@polkassembly/util
Version:
Set of utility functions for Polkassembly and more.
26 lines (25 loc) • 1.1 kB
JavaScript
;
// Copyright 2019-2020 @paritytech/polkassembly authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var util_1 = require("@polkadot/util");
var bn_js_1 = __importDefault(require("bn.js"));
/**
* @name solveQuadraticEquation
* @summary Returns the root of a polynomial function of degree 2, a*x^2 + b*x + c where a, b and c are BN from bn.js.
* @param a
* @param b
* @param c
**/
function solveQuadraticEquation(a, b, c) {
var TWO = new bn_js_1.default(2);
var FOUR = new bn_js_1.default(4);
var result = b.neg().add(util_1.bnSqrt(b.pow(TWO).sub(FOUR.mul(a).mul(c)))).div(TWO.mul(a));
var result2 = b.neg().sub(util_1.bnSqrt(b.pow(TWO).sub(FOUR.mul(a).mul(c)))).div(TWO.mul(a));
return [result, result2];
}
exports.solveQuadraticEquation = solveQuadraticEquation;