/*
THIS INFRAGISTICS ULTIMATE SOFTWARE LICENSE  AGREEMENT ("AGREEMENT") LOCATED HERE:
https://www.infragistics.com/legal/license/igultimate-la
https://www.infragistics.com/legal/license/igultimate-eula
GOVERNS THE LICENSING, INSTALLATION AND USE OF INFRAGISTICS SOFTWARE. BY DOWNLOADING AND/OR INSTALLING AND USING INFRAGISTICS SOFTWARE:  you are indicating that you have read and understand this Agreement, and agree to be legally bound by it on behalf of the yourself and your company.
*/
import { BuiltInFunctionBase } from "./BuiltInFunctionBase";
import { ExcelCalcValue } from "./ExcelCalcValue";
import { ExcelCalcErrorValue } from "./ExcelCalcErrorValue";
import { UltraCalcFunctionCombin } from "./UltraCalcFunctionCombin";
import { markType } from "igniteui-angular-core";
import { truncate, isNaN_ } from "igniteui-angular-core";
/**
 * @hidden
 */
export class UltraCalcFunctionBinomDist extends BuiltInFunctionBase {
    evaluate(a, b) {
        let c = a.pop();
        if (c.isError) {
            return new ExcelCalcValue(c.toErrorValue());
        }
        let d = c._toBoolean1();
        c = a.pop();
        if (c.isError) {
            return new ExcelCalcValue(c.toErrorValue());
        }
        let e = c._toDouble3();
        c = a.pop();
        if (c.isError) {
            return new ExcelCalcValue(c.toErrorValue());
        }
        let f = truncate(c._toDecimal2());
        c = a.pop();
        if (c.isError) {
            return new ExcelCalcValue(c.toErrorValue());
        }
        let g = truncate(c._toDecimal2());
        return UltraCalcFunctionBinomDist._ak(f, e, d ? 0 : g, g);
    }
    static _ak(a, b, c, d) {
        if (c < 0 || c > a || b < 0 || b > 1 || c > d || d > a) {
            return new ExcelCalcValue(new ExcelCalcErrorValue(4));
        }
        let e = 0;
        if (a >= 1030) {
            let f = Math.pow(10, -12);
            let g = 0;
            let h = 0;
            let i = c;
            let j = d;
            let k = a;
            let l = b;
            let m = truncate((k * l));
            let n = c != d;
            g++;
            if (i <= m && m <= j) {
                h++;
            }
            let o = 1;
            let p = false;
            let q = m + 1;
            while (!p && q <= k) {
                let r = o * (k - q + 1) * l / (q * (1 - l));
                g += r;
                if (i <= q && q <= j) {
                    h += r;
                }
                if (r <= f) {
                    p = true;
                }
                o = r;
                q++;
            }
            o = 1;
            p = false;
            q = m - 1;
            while (!p && q >= 0) {
                let s = o * (q + 1) * (1 - l) / ((k - q) * l);
                g += s;
                if (i <= q && q <= j) {
                    h += s;
                }
                if (s <= f) {
                    p = true;
                }
                o = s;
                q--;
            }
            e = h / g;
        }
        else {
            for (let t = c; t <= d && !isNaN_(e); t++) {
                e += UltraCalcFunctionBinomDist._al(t, a, b);
            }
        }
        if (isNaN_(e)) {
            return new ExcelCalcValue(new ExcelCalcErrorValue(4));
        }
        return new ExcelCalcValue(e);
    }
    static _al(a, b, c) {
        let d = UltraCalcFunctionCombin._am(b, a, false);
        return d * Math.pow(c, a) * Math.pow(1 - c, b - a);
    }
    get_name() {
        return "binomdist";
    }
    get name() {
        return this.get_name();
    }
    get_minArgs() {
        return 4;
    }
    get minArgs() {
        return this.get_minArgs();
    }
    get_maxArgs() {
        return 4;
    }
    get maxArgs() {
        return this.get_maxArgs();
    }
}
UltraCalcFunctionBinomDist.$t = markType(UltraCalcFunctionBinomDist, 'UltraCalcFunctionBinomDist', BuiltInFunctionBase.$);
//# sourceMappingURL=UltraCalcFunctionBinomDist.js.map