
/** @internal */
const f = 134217729;  // 2**27 + 1;


// Taken from https://github.com/munrocket/double.js/blob/master/src/double.ts
// Unfortunately no error bound given
/**
 * Returns the square root of a double-double as a double-double.
 * * no error bound is returned
 * 
 * @param x a double-double precision floating point number
 */
// TODO - calculate an error bound and add to function description
function ddSqrt(x: number[]): number[] {
    const xl = x[0];
    const xh = x[1];
    
    if (xh === 0) { return [0,0]; }
    const s = Math.sqrt(xh);

    //const [tl,th] = twoSquare(s);
    const th = s*s;
    const c = f * s; const ah = c - (c - s); const al = s - ah;
    const tl = (al*al) - ((th - (ah*ah)) - 2*(ah*al));

    const e = (xh - th - tl + xl) * 0.5 / s;

    return [e - ((s + e) - s), s + e];
}


export { ddSqrt }
