#include "mymath.h"

long double
qlog1p(long double x)
{
    /* (log1p) */
    /* Return qlog(1 + x), taking care to avoid subtraction loss. */
    /* (17-Jun-2002) */

    /* Initialized data */

    static long double CUTLO = -0.5L;	/* CUTLO = -1/beta, for arbitrary base beta */
    static long double CUTHI = 0.5L;	/* CUTHI = 1/beta, for arbitrary base beta */
    static long double ONE = 1.0L;
    static long double ZERO = 0.0L;

    /* System generated locals */
    long double ret_val;

    /* Local variables */
    long double sum,
	term,
	xn,
	xtons;

    /*
     * We handle the computation in three regions:
     *
     * x in [-Infinity, CUTLO):  log(1+x)
     * x in [CUTLO, CUTHI]:      Taylor series
     * x in (CUTHI, Infinity]:   log(1+x)
     *
     * The central region suffers loss of ONE or more bits if the
     * simple formula is used.
     *
     * We also handle the cases of log1p(NaN) and log1p(0) specially,
     * so as to preserve NaNs, and the sign of ZERO.
     */

    if (x != x)
	ret_val = x;
    else if (x == ZERO)
	ret_val = x;
    else if (x < CUTLO)
	ret_val = LOG(ONE + x);
    else if (x <= CUTHI)
    {
	term = x;
	sum = ZERO;
	xn = ONE;
	xtons = x;
	while ((sum + term) != sum)
	{
	    sum += term;
	    xn += ONE;
	    xtons = -xtons * x;
	    term = xtons / xn;
	}
	ret_val = sum;
    }
    else
	ret_val = LOG(ONE + x);

    return (ret_val);
}


long double
ql1p10(long double x)
{
    /* (log1p10) */
    /* Return qlog10(1 + x), taking care to avoid subtraction loss. */
    /* (17-Jun-2002) */

    static long double LOG10E = 0.43429448190325182765112891891660508229439700580366L;

    return (qlog1p(x) * LOG10E);
}
