diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c
new file mode 100644
index 1bfa29e..79812a8
--- a/src/backend/utils/adt/numeric.c
+++ b/src/backend/utils/adt/numeric.c
@@ -361,20 +361,6 @@ static NumericVar const_zero_point_nine
 {1, -1, NUMERIC_POS, 1, NULL, const_zero_point_nine_data};
 
 #if DEC_DIGITS == 4
-static NumericDigit const_zero_point_01_data[1] = {100};
-static NumericVar const_zero_point_01 =
-{1, -1, NUMERIC_POS, 2, NULL, const_zero_point_01_data};
-#elif DEC_DIGITS == 2
-static NumericDigit const_zero_point_01_data[1] = {1};
-static NumericVar const_zero_point_01 =
-{1, -1, NUMERIC_POS, 2, NULL, const_zero_point_01_data};
-#elif DEC_DIGITS == 1
-static NumericDigit const_zero_point_01_data[1] = {1};
-static NumericVar const_zero_point_01 =
-{1, -2, NUMERIC_POS, 2, NULL, const_zero_point_01_data};
-#endif
-
-#if DEC_DIGITS == 4
 static NumericDigit const_one_point_one_data[2] = {1, 1000};
 #elif DEC_DIGITS == 2
 static NumericDigit const_one_point_one_data[2] = {1, 10};
@@ -471,13 +457,13 @@ static void div_var(NumericVar *var1, Nu
 static void div_var_fast(NumericVar *var1, NumericVar *var2, NumericVar *result,
 			 int rscale, bool round);
 static int	select_div_scale(NumericVar *var1, NumericVar *var2);
+static int	estimate_ln_weight(NumericVar *var);
 static void mod_var(NumericVar *var1, NumericVar *var2, NumericVar *result);
 static void ceil_var(NumericVar *var, NumericVar *result);
 static void floor_var(NumericVar *var, NumericVar *result);
 
 static void sqrt_var(NumericVar *arg, NumericVar *result, int rscale);
 static void exp_var(NumericVar *arg, NumericVar *result, int rscale);
-static void exp_var_internal(NumericVar *arg, NumericVar *result, int rscale);
 static void ln_var(NumericVar *arg, NumericVar *result, int rscale);
 static void log_var(NumericVar *base, NumericVar *num, NumericVar *result);
 static void power_var(NumericVar *base, NumericVar *exp, NumericVar *result);
@@ -2697,7 +2683,7 @@ numeric_ln(PG_FUNCTION_ARGS)
 	Numeric		res;
 	NumericVar	arg;
 	NumericVar	result;
-	int			dec_digits;
+	int			ln_weight;
 	int			rscale;
 
 	/*
@@ -2709,16 +2695,10 @@ numeric_ln(PG_FUNCTION_ARGS)
 	init_var_from_num(num, &arg);
 	init_var(&result);
 
-	/* Approx decimal digits before decimal point */
-	dec_digits = (arg.weight + 1) * DEC_DIGITS;
-
-	if (dec_digits > 1)
-		rscale = NUMERIC_MIN_SIG_DIGITS - (int) log10(dec_digits - 1);
-	else if (dec_digits < 1)
-		rscale = NUMERIC_MIN_SIG_DIGITS - (int) log10(1 - dec_digits);
-	else
-		rscale = NUMERIC_MIN_SIG_DIGITS;
+	/* Estimated weight of logarithm */
+	ln_weight = estimate_ln_weight(&arg);
 
+	rscale = NUMERIC_MIN_SIG_DIGITS - ln_weight;
 	rscale = Max(rscale, arg.dscale);
 	rscale = Max(rscale, NUMERIC_MIN_DISPLAY_SCALE);
 	rscale = Min(rscale, NUMERIC_MAX_DISPLAY_SCALE);
@@ -5715,7 +5695,6 @@ mul_var(NumericVar *var1, NumericVar *va
 	int			res_ndigits;
 	int			res_sign;
 	int			res_weight;
-	int			maxdigits;
 	int		   *dig;
 	int			carry;
 	int			maxdig;
@@ -5747,41 +5726,8 @@ mul_var(NumericVar *var1, NumericVar *va
 		res_sign = NUMERIC_NEG;
 	res_weight = var1->weight + var2->weight + 2;
 
-	/*
-	 * Determine number of result digits to compute.  If the exact result
-	 * would have more than rscale fractional digits, truncate the computation
-	 * with MUL_GUARD_DIGITS guard digits.  We do that by pretending that one
-	 * or both inputs have fewer digits than they really do.
-	 */
+	/* Determine number of result digits to compute */
 	res_ndigits = var1ndigits + var2ndigits + 1;
-	maxdigits = res_weight + 1 + (rscale * DEC_DIGITS) + MUL_GUARD_DIGITS;
-	if (res_ndigits > maxdigits)
-	{
-		if (maxdigits < 3)
-		{
-			/* no useful precision at all in the result... */
-			zero_var(result);
-			result->dscale = rscale;
-			return;
-		}
-		/* force maxdigits odd so that input ndigits can be equal */
-		if ((maxdigits & 1) == 0)
-			maxdigits++;
-		if (var1ndigits > var2ndigits)
-		{
-			var1ndigits -= res_ndigits - maxdigits;
-			if (var1ndigits < var2ndigits)
-				var1ndigits = var2ndigits = (var1ndigits + var2ndigits) / 2;
-		}
-		else
-		{
-			var2ndigits -= res_ndigits - maxdigits;
-			if (var2ndigits < var1ndigits)
-				var1ndigits = var2ndigits = (var1ndigits + var2ndigits) / 2;
-		}
-		res_ndigits = maxdigits;
-		Assert(res_ndigits == var1ndigits + var2ndigits + 1);
-	}
 
 	/*
 	 * We do the arithmetic in an array "dig[]" of signed int's.  Since
@@ -6478,6 +6424,81 @@ select_div_scale(NumericVar *var1, Numer
 
 
 /*
+ * Estimate the weight of the most significant digit of the natural logarithm
+ * of a number.
+ *
+ * Returns an approximate value for log10(Abs(ln(var))), used to determine the
+ * appropriate scale when computing natural logarithms.
+ */
+static int
+estimate_ln_weight(NumericVar *var)
+{
+	int			ln_weight = 0;
+
+	if (cmp_var(var, &const_zero_point_nine) >= 0 &&
+		cmp_var(var, &const_one_point_one) <= 0)
+	{
+		/*
+		 * 0.9 <= var <= 1.1
+		 *
+		 * Its logarithm has a (possibly very large) negative weight. Estimate
+		 * it using ln(var) = ln(1+x) = x + O(x^2) ~= x.
+		 */
+		if (cmp_var(var, &const_one) != 0)
+		{
+			NumericVar	x;
+
+			init_var(&x);
+			sub_var(var, &const_one, &x);
+
+			/* Weight of most significant digit of x */
+			ln_weight = x.weight * DEC_DIGITS + (int) log10(x.digits[0]);
+
+			free_var(&x);
+		}
+		else
+			ln_weight = 0; /* ln(var) = 0 */
+	}
+	else
+	{
+		/*
+		 * Estimate the logarithm using the first couple of digits from the
+		 * input number.
+		 */
+		if (var->ndigits > 0)
+		{
+			double		digits;
+			int			weight;
+			double		ln_var;
+
+			digits = var->digits[0];
+			weight = var->weight * DEC_DIGITS;
+
+			if (var->ndigits > 1)
+			{
+				digits = digits * NBASE + var->digits[1];
+				weight -= DEC_DIGITS;
+			}
+
+			/*
+			 * var ~= digits * 10^weight
+			 * ln(var) ~= ln(digits) + weight*ln(10)
+			 */
+			ln_var = log(digits) + weight * 2.302585092994046;
+			ln_weight = (int) log10(Abs(ln_var));
+		}
+		else
+			/* If var has no digits, then it must be zero */
+			ereport(ERROR,
+					(errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
+					 errmsg("cannot take logarithm of zero")));
+	}
+
+	return ln_weight;
+}
+
+
+/*
  * mod_var() -
  *
  *	Calculate the modulo of two numerics at variable level
@@ -6635,134 +6656,93 @@ static void
 exp_var(NumericVar *arg, NumericVar *result, int rscale)
 {
 	NumericVar	x;
-	int			xintval;
-	bool		xneg = FALSE;
+	double		val;
+	int			weight;
+	int			ndiv2;
+	int			sig_digits;
 	int			local_rscale;
+	NumericVar	elem;
+	NumericVar	ni;
 
-	/*----------
-	 * We separate the integral and fraction parts of x, then compute
-	 *		e^x = e^xint * e^xfrac
-	 * where e = exp(1) and e^xfrac = exp(xfrac) are computed by
-	 * exp_var_internal; the limited range of inputs allows that routine
-	 * to do a good job with a simple Taylor series.  Raising e^xint is
-	 * done by repeated multiplications in power_var_int.
-	 *----------
+	/*
+	 * Estimate the weight of the result using floating point arithmetic, so
+	 * that we can choose an appropriate local rscale for the calculation.
 	 */
 	init_var(&x);
+	init_var(&elem);
+	init_var(&ni);
 
 	set_var_from_var(arg, &x);
 
-	if (x.sign == NUMERIC_NEG)
-	{
-		xneg = TRUE;
-		x.sign = NUMERIC_POS;
-	}
-
-	/* Extract the integer part, remove it from x */
-	xintval = 0;
-	while (x.weight >= 0)
-	{
-		xintval *= NBASE;
-		if (x.ndigits > 0)
-		{
-			xintval += x.digits[0];
-			x.digits++;
-			x.ndigits--;
-		}
-		x.weight--;
-		/* Guard against overflow */
-		if (xintval >= NUMERIC_MAX_RESULT_SCALE * 3)
-			ereport(ERROR,
-					(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
-					 errmsg("argument for function \"exp\" too big")));
-	}
+	val = numericvar_to_double_no_overflow(&x);
 
-	/* Select an appropriate scale for internal calculation */
-	local_rscale = rscale + MUL_GUARD_DIGITS * 2;
+	/* Guard against overflow */
+	if (Abs(val) >= NUMERIC_MAX_RESULT_SCALE * 3)
+		ereport(ERROR,
+				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
+				 errmsg("argument for function \"exp\" too big")));
 
-	/* Compute e^xfrac */
-	exp_var_internal(&x, result, local_rscale);
+	/* weight = log10(e^x) = x * log10(e) */
+	weight = (int) (val * 0.434294481903252);
 
-	/* If there's an integer part, multiply by e^xint */
-	if (xintval > 0)
+	/*
+	 * Reduce x to the range 0 <= x <= 0.01 by dividing by 2^n, to improve the
+	 * convergence rate of the Taylor series.
+	 */
+	if (Abs(val) > 0.01)
 	{
-		NumericVar	e;
-
-		init_var(&e);
-		exp_var_internal(&const_one, &e, local_rscale);
-		power_var_int(&e, xintval, &e, local_rscale);
-		mul_var(&e, result, result, local_rscale);
-		free_var(&e);
-	}
-
-	/* Compensate for input sign, and round to requested rscale */
-	if (xneg)
-		div_var_fast(&const_one, result, result, rscale, true);
-	else
-		round_var(result, rscale);
-
-	free_var(&x);
-}
+		NumericVar	tmp;
 
+		init_var(&tmp);
+		set_var_from_var(&const_two, &tmp);
 
-/*
- * exp_var_internal() -
- *
- *	Raise e to the power of x, where 0 <= x <= 1
- *
- * NB: the result should be good to at least rscale digits, but it has
- * *not* been rounded off; the caller must do that if wanted.
- */
-static void
-exp_var_internal(NumericVar *arg, NumericVar *result, int rscale)
-{
-	NumericVar	x;
-	NumericVar	xpow;
-	NumericVar	ifac;
-	NumericVar	elem;
-	NumericVar	ni;
-	int			ndiv2 = 0;
-	int			local_rscale;
+		ndiv2 = 1;
+		val /= 2;
 
-	init_var(&x);
-	init_var(&xpow);
-	init_var(&ifac);
-	init_var(&elem);
-	init_var(&ni);
+		while (Abs(val) > 0.01)
+		{
+			ndiv2++;
+			val /= 2;
+			add_var(&tmp, &tmp, &tmp);
+		}
 
-	set_var_from_var(arg, &x);
+		local_rscale = x.dscale + ndiv2;
+		div_var_fast(&x, &tmp, &x, local_rscale, true);
 
-	Assert(x.sign == NUMERIC_POS);
+		free_var(&tmp);
+	}
+	else
+		ndiv2 = 0;
 
-	local_rscale = rscale + 8;
+	/*
+	 * Set the scale for the Taylor series expansion.  The final result has
+	 * (weight + rscale + 1) significant digits.  In addition, we have to
+	 * raise the Taylor series result to the power 2^ndiv2, which introduces
+	 * an error of up to around log10(2^ndiv2) digits, so work with this
+	 * many extra digits of precision (plus a few more for good measure).
+	 */
+	sig_digits = 1 + weight + rscale + (int) (ndiv2 * 0.301029995663981);
+	sig_digits = Max(sig_digits, 0) + 8;
 
-	/* Reduce input into range 0 <= x <= 0.01 */
-	while (cmp_var(&x, &const_zero_point_01) > 0)
-	{
-		ndiv2++;
-		local_rscale++;
-		mul_var(&x, &const_zero_point_five, &x, x.dscale + 1);
-	}
+	local_rscale = sig_digits - 1;
 
 	/*
 	 * Use the Taylor series
 	 *
 	 * exp(x) = 1 + x + x^2/2! + x^3/3! + ...
 	 *
-	 * Given the limited range of x, this should converge reasonably quickly.
-	 * We run the series until the terms fall below the local_rscale limit.
+	 * Given the limited range of x, the series should converge reasonably
+	 * quickly.  We run it until the terms fall below the local_rscale limit.
 	 */
 	add_var(&const_one, &x, result);
-	set_var_from_var(&x, &xpow);
-	set_var_from_var(&const_one, &ifac);
+	set_var_from_var(&x, &elem);
 	set_var_from_var(&const_one, &ni);
 
 	for (;;)
 	{
 		add_var(&ni, &const_one, &ni);
-		mul_var(&xpow, &x, &xpow, local_rscale);
-		mul_var(&ifac, &ni, &ifac, 0);
-		div_var_fast(&xpow, &ifac, &elem, local_rscale, true);
+		mul_var(&elem, &x, &elem, local_rscale);
+		div_var_fast(&elem, &ni, &elem, local_rscale, true);
 
 		if (elem.ndigits == 0)
 			break;
@@ -6770,13 +6750,21 @@ exp_var_internal(NumericVar *arg, Numeri
 		add_var(result, &elem, result);
 	}
 
-	/* Compensate for argument range reduction */
+	/*
+	 * Compensate for the argument range reduction.  Since the weight of the
+	 * result doubles with each multiplication, we can reduce the local rscale
+	 * as we proceed.
+	 */
 	while (ndiv2-- > 0)
+	{
+		local_rscale = sig_digits - result->weight * 2 * DEC_DIGITS;
 		mul_var(result, result, result, local_rscale);
+	}
+
+	/* Round to requested rscale */
+	round_var(result, rscale);
 
 	free_var(&x);
-	free_var(&xpow);
-	free_var(&ifac);
 	free_var(&elem);
 	free_var(&ni);
 }
@@ -6795,8 +6783,10 @@ ln_var(NumericVar *arg, NumericVar *resu
 	NumericVar	ni;
 	NumericVar	elem;
 	NumericVar	fact;
+	NumericVar	ln_10;
 	int			local_rscale;
 	int			cmp;
+	int			pow_10;
 
 	cmp = cmp_var(arg, &const_zero);
 	if (cmp == 0)
@@ -6815,10 +6805,24 @@ ln_var(NumericVar *arg, NumericVar *resu
 	init_var(&ni);
 	init_var(&elem);
 	init_var(&fact);
+	init_var(&ln_10);
 
 	set_var_from_var(arg, &x);
 	set_var_from_var(&const_two, &fact);
 
+	/*
+	 * Reduce the input weight if it is too large.  This helps prevent loss of
+	 * precision from the repeated sqrt()s in the input range reduction below.
+	 */
+	if (Abs((x.weight + 1) * DEC_DIGITS) > 10)
+	{
+		ln_var(&const_ten, &ln_10, local_rscale);
+		pow_10 = x.weight * DEC_DIGITS;
+		x.weight = 0;
+	}
+	else
+		pow_10 = 0;
+
 	/* Reduce input into range 0.9 < x < 1.1 */
 	while (cmp_var(&x, &const_zero_point_nine) <= 0)
 	{
@@ -6868,13 +6872,25 @@ ln_var(NumericVar *arg, NumericVar *resu
 	}
 
 	/* Compensate for argument range reduction, round to requested rscale */
-	mul_var(result, &fact, result, rscale);
+	if (pow_10 != 0)
+	{
+		mul_var(result, &fact, result, local_rscale);
+
+		int64_to_numericvar((int64) pow_10, &ni);
+		mul_var(&ln_10, &ni, &xx, local_rscale);
+		add_var(result, &xx, result);
+
+		round_var(result, rscale);
+	}
+	else
+		mul_var(result, &fact, result, rscale);
 
 	free_var(&x);
 	free_var(&xx);
 	free_var(&ni);
 	free_var(&elem);
 	free_var(&fact);
+	free_var(&ln_10);
 }
 
 
@@ -6890,42 +6906,47 @@ log_var(NumericVar *base, NumericVar *nu
 {
 	NumericVar	ln_base;
 	NumericVar	ln_num;
-	int			dec_digits;
+	int			ln_base_weight;
+	int			ln_num_weight;
+	int			result_weight;
 	int			rscale;
-	int			local_rscale;
+	int			ln_base_rscale;
+	int			ln_num_rscale;
 
 	init_var(&ln_base);
 	init_var(&ln_num);
 
-	/* Set scale for ln() calculations --- compare numeric_ln() */
-
-	/* Approx decimal digits before decimal point */
-	dec_digits = (num->weight + 1) * DEC_DIGITS;
-
-	if (dec_digits > 1)
-		rscale = NUMERIC_MIN_SIG_DIGITS - (int) log10(dec_digits - 1);
-	else if (dec_digits < 1)
-		rscale = NUMERIC_MIN_SIG_DIGITS - (int) log10(1 - dec_digits);
-	else
-		rscale = NUMERIC_MIN_SIG_DIGITS;
+	/* Estimated weights of ln(base), ln(num) and the final result */
+	ln_base_weight = estimate_ln_weight(base);
+	ln_num_weight = estimate_ln_weight(num);
+	result_weight = ln_num_weight - ln_base_weight;
 
+	/*
+	 * Select the scale of the result so that it will have at least
+	 * NUMERIC_MIN_SIG_DIGITS significant digits and is not less than either
+	 * input's display scale.
+	 */
+	rscale = NUMERIC_MIN_SIG_DIGITS - result_weight;
 	rscale = Max(rscale, base->dscale);
 	rscale = Max(rscale, num->dscale);
 	rscale = Max(rscale, NUMERIC_MIN_DISPLAY_SCALE);
 	rscale = Min(rscale, NUMERIC_MAX_DISPLAY_SCALE);
 
-	local_rscale = rscale + 8;
-
-	/* Form natural logarithms */
-	ln_var(base, &ln_base, local_rscale);
-	ln_var(num, &ln_num, local_rscale);
+	/*
+	 * Set the scales for ln(base) and ln(num) so that they each have more
+	 * significant digits than the final result.
+	 */
+	ln_base_rscale = rscale + result_weight - ln_base_weight + 8;
+	ln_base_rscale = Max(ln_base_rscale, NUMERIC_MIN_DISPLAY_SCALE);
 
-	ln_base.dscale = rscale;
-	ln_num.dscale = rscale;
+	ln_num_rscale = rscale + result_weight - ln_num_weight + 8;
+	ln_num_rscale = Max(ln_num_rscale, NUMERIC_MIN_DISPLAY_SCALE);
 
-	/* Select scale for division result */
-	rscale = select_div_scale(&ln_num, &ln_base);
+	/* Form natural logarithms */
+	ln_var(base, &ln_base, ln_base_rscale);
+	ln_var(num, &ln_num, ln_num_rscale);
 
+	/* Divide and round to the required scale */
 	div_var_fast(&ln_num, &ln_base, result, rscale, true);
 
 	free_var(&ln_num);
@@ -6945,7 +6966,7 @@ power_var(NumericVar *base, NumericVar *
 {
 	NumericVar	ln_base;
 	NumericVar	ln_num;
-	int			dec_digits;
+	int			ln_weight;
 	int			rscale;
 	int			local_rscale;
 	double		val;
@@ -6989,43 +7010,51 @@ power_var(NumericVar *base, NumericVar *
 	init_var(&ln_base);
 	init_var(&ln_num);
 
-	/* Set scale for ln() calculation --- need extra accuracy here */
-
-	/* Approx decimal digits before decimal point */
-	dec_digits = (base->weight + 1) * DEC_DIGITS;
-
-	if (dec_digits > 1)
-		rscale = NUMERIC_MIN_SIG_DIGITS * 2 - (int) log10(dec_digits - 1);
-	else if (dec_digits < 1)
-		rscale = NUMERIC_MIN_SIG_DIGITS * 2 - (int) log10(1 - dec_digits);
-	else
-		rscale = NUMERIC_MIN_SIG_DIGITS * 2;
-
-	rscale = Max(rscale, base->dscale * 2);
-	rscale = Max(rscale, exp->dscale * 2);
-	rscale = Max(rscale, NUMERIC_MIN_DISPLAY_SCALE * 2);
-	rscale = Min(rscale, NUMERIC_MAX_DISPLAY_SCALE * 2);
+	/*
+	 * Decide on the scale for the ln() calculation.  For this we need an
+	 * estimate of the weight of the result, which we obtain by computing a
+	 * few digits of the logarithm.
+	 *
+	 * result = e^(exp * ln(base))
+	 * result weight = log10(result) = exp * ln(base) * log10(e)
+	 */
+	ln_weight = estimate_ln_weight(base);
 
-	local_rscale = rscale + 8;
+	local_rscale = 8 - ln_weight;
+	local_rscale = Max(local_rscale, NUMERIC_MIN_DISPLAY_SCALE);
+	local_rscale = Min(local_rscale, NUMERIC_MAX_DISPLAY_SCALE);
 
 	ln_var(base, &ln_base, local_rscale);
 
 	mul_var(&ln_base, exp, &ln_num, local_rscale);
 
-	/* Set scale for exp() -- compare numeric_exp() */
-
-	/* convert input to float8, ignoring overflow */
 	val = numericvar_to_double_no_overflow(&ln_num);
 
-	/*
-	 * log10(result) = num * log10(e), so this is approximately the weight:
-	 */
-	val *= 0.434294481903252;
+	val *= 0.434294481903252; /* approximate result weight */
 
 	/* limit to something that won't cause integer overflow */
 	val = Max(val, -NUMERIC_MAX_RESULT_SCALE);
 	val = Min(val, NUMERIC_MAX_RESULT_SCALE);
 
+	/* Estimated result scale */
+
+	rscale = NUMERIC_MIN_SIG_DIGITS - (int) val;
+	rscale = Max(rscale, base->dscale);
+	rscale = Max(rscale, exp->dscale);
+	rscale = Max(rscale, NUMERIC_MIN_DISPLAY_SCALE);
+	rscale = Min(rscale, NUMERIC_MAX_DISPLAY_SCALE);
+
+	/* Set the scale for the real ln() calculation */
+
+	local_rscale = rscale + (int) val - ln_weight + 8;
+	local_rscale = Max(local_rscale, NUMERIC_MIN_DISPLAY_SCALE);
+
+	ln_var(base, &ln_base, local_rscale);
+
+	mul_var(&ln_base, exp, &ln_num, local_rscale);
+
+	/* Set the scale for the final exp() */
+
 	rscale = NUMERIC_MIN_SIG_DIGITS - (int) val;
 	rscale = Max(rscale, base->dscale);
 	rscale = Max(rscale, exp->dscale);
@@ -7046,6 +7075,10 @@ power_var(NumericVar *base, NumericVar *
 static void
 power_var_int(NumericVar *base, int exp, NumericVar *result, int rscale)
 {
+	double		f;
+	int			p;
+	int			i;
+	int			sig_digits;
 	unsigned int mask;
 	bool		neg;
 	NumericVar	base_prod;
@@ -7080,15 +7113,71 @@ power_var_int(NumericVar *base, int exp,
 			break;
 	}
 
+	/* Handle the special case where the base is zero */
+	if (base->ndigits == 0 || base->digits[0] == 0)
+	{
+		if (exp < 0)
+			ereport(ERROR,
+					(errcode(ERRCODE_DIVISION_BY_ZERO),
+					 errmsg("division by zero")));
+		zero_var(result);
+		result->dscale = rscale;
+		return;
+	}
+
 	/*
 	 * The general case repeatedly multiplies base according to the bit
-	 * pattern of exp.  We do the multiplications with some extra precision.
+	 * pattern of exp.
+	 *
+	 * First we need to estimate the weight of the result so that we know how
+	 * many significant digits are needed.
+	 */
+	f = base->digits[0];
+	p = base->weight * DEC_DIGITS;
+
+	for (i = 1; i < base->ndigits && i * DEC_DIGITS < 16; i++)
+	{
+		f = f * NBASE + base->digits[i];
+		p -= DEC_DIGITS;
+	}
+
+	/*
+	 * base ~= f * 10^p
+	 * log10(result) = log10(base^exp) ~= exp * (p + log10(f))
+	 */
+	f = exp * (p + log10(f));
+
+	/* Crude overflow/underflow tests */
+
+	if (f > 3 * SHRT_MAX * DEC_DIGITS)
+		ereport(ERROR,
+				(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
+				 errmsg("value overflows numeric format")));
+
+	if (f < 3 * SHRT_MIN * DEC_DIGITS)
+	{
+		zero_var(result);
+		result->dscale = rscale;
+		return;
+	}
+
+	/*
+	 * Significant digits in the result.  Could perhaps do another underflow
+	 * test here.
 	 */
+	sig_digits = 1 + rscale + (int) Abs(f);
+	sig_digits = Max(sig_digits, 1);
+
+	/*
+	 * The multiplications to produce the result may introduce an error of up
+	 * to around log10(Abs(exp)) digits, so work with this many extra digits
+	 * of precision (plus a few more for good measure).
+	 */
+	sig_digits += (int) log(Abs(exp)) + 8;
+
 	neg = (exp < 0);
 	mask = Abs(exp);
 
-	local_rscale = rscale + MUL_GUARD_DIGITS * 2;
-
 	init_var(&base_prod);
 	set_var_from_var(base, &base_prod);
 
@@ -7099,9 +7188,26 @@ power_var_int(NumericVar *base, int exp,
 
 	while ((mask >>= 1) > 0)
 	{
+		/*
+		 * Do the multiplications using rscales large enough to hold the
+		 * results to the required number of significant digits, but don't
+		 * waste time by exceeding the scales of the numbers themselves.
+		 */
+		local_rscale = sig_digits - 2 * base_prod.weight * DEC_DIGITS;
+		local_rscale = Max(local_rscale, 2 * base_prod.dscale);
+
 		mul_var(&base_prod, &base_prod, &base_prod, local_rscale);
+
 		if (mask & 1)
+		{
+			local_rscale = sig_digits -
+				(base_prod.weight + result->weight) * DEC_DIGITS;
+
+			local_rscale = Max(local_rscale,
+							   base_prod.dscale + result->dscale);
+
 			mul_var(&base_prod, result, result, local_rscale);
+		}
 
 		/*
 		 * When abs(base) > 1, the number of digits to the left of the decimal
diff --git a/src/test/regress/expected/numeric.out b/src/test/regress/expected/numeric.out
new file mode 100644
index e6ee548..1c3b61c
--- a/src/test/regress/expected/numeric.out
+++ b/src/test/regress/expected/numeric.out
@@ -1433,6 +1433,163 @@ select 10.0 ^ 2147483647 as overflows;
 ERROR:  value overflows numeric format
 select 117743296169.0 ^ 1000000000 as overflows;
 ERROR:  value overflows numeric format
+-- cases that used to return inaccurate results
+select 3.789 ^ 21;
+            ?column?            
+--------------------------------
+ 1409343026052.8716016316022141
+(1 row)
+
+select 3.789 ^ 35;
+                ?column?                
+----------------------------------------
+ 177158169650516670809.3820586142670135
+(1 row)
+
+select 1.2 ^ 345;
+                   ?column?                    
+-----------------------------------------------
+ 2077446682327378559843444695.5827049735727869
+(1 row)
+
+select 0.12 ^ (-20);
+               ?column?               
+--------------------------------------
+ 2608405330458882702.5529619561355838
+(1 row)
+
+-- cases that used to error out
+select 0.12 ^ (-25);
+                 ?column?                  
+-------------------------------------------
+ 104825960103961013959336.4983657883169110
+(1 row)
+
+select 0.5678 ^ (-85);
+                ?column?                
+----------------------------------------
+ 782333637740774446257.7719390061997396
+(1 row)
+
+--
+-- Tests for raising to non-integer powers
+--
+-- special cases
+select 0.0 ^ 0.0;
+      ?column?      
+--------------------
+ 1.0000000000000000
+(1 row)
+
+select (-12.34) ^ 0.0;
+      ?column?      
+--------------------
+ 1.0000000000000000
+(1 row)
+
+select 12.34 ^ 0.0;
+      ?column?      
+--------------------
+ 1.0000000000000000
+(1 row)
+
+select 0.0 ^ 12.34;
+      ?column?      
+--------------------
+ 0.0000000000000000
+(1 row)
+
+-- invalid inputs
+select 0.0 ^ (-12.34);
+ERROR:  zero raised to a negative power is undefined
+select (-12.34) ^ 1.2;
+ERROR:  a negative number raised to a non-integer power yields a complex result
+-- cases that used to generate inaccurate results
+select 32.1 ^ 9.8;
+      ?column?      
+--------------------
+ 580429286790711.10
+(1 row)
+
+select 32.1 ^ (-9.8);
+             ?column?             
+----------------------------------
+ 0.000000000000001722862754788209
+(1 row)
+
+select 12.3 ^ 45.6;
+                       ?column?                       
+------------------------------------------------------
+ 50081010321492803393171165777624533697036806969694.9
+(1 row)
+
+select 12.3 ^ (-45.6);
+                              ?column?                               
+---------------------------------------------------------------------
+ 0.00000000000000000000000000000000000000000000000001996764828785491
+(1 row)
+
+-- big test
+select 1.234 ^ 5678;
+                                                                                                                                                                                                                                                                         ?column?                                                                                                                                                                                                                                                                         
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ 307239295662090741644584872593956173493568238595074141254349565406661439636598896798876823220904084953233015553994854875890890858118656468658643918169805277399402542281777901029346337707622181574346585989613344285010764501017625366742865066948856161360224801370482171458030533346309750557140549621313515752078638620714732831815297168231790779296290266207315344008883935010274044001522606235576584215999260117523114297033944018699691024106823438431754073086813382242140602291215149759520833200152654884259619588924545324.5973362312547382
+(1 row)
+
+--
+-- Tests for EXP()
+--
+-- special cases
+select exp(0.0);
+        exp         
+--------------------
+ 1.0000000000000000
+(1 row)
+
+select exp(1.0);
+        exp         
+--------------------
+ 2.7182818284590452
+(1 row)
+
+select exp(1.0::numeric(71,70));
+                                   exp                                    
+--------------------------------------------------------------------------
+ 2.7182818284590452353602874713526624977572470936999595749669676277240766
+(1 row)
+
+-- cases that used to generate inaccurate results
+select exp(32.999);
+         exp         
+---------------------
+ 214429043492155.053
+(1 row)
+
+select exp(-32.999);
+               exp                
+----------------------------------
+ 0.000000000000004663547361468248
+(1 row)
+
+select exp(123.456);
+                            exp                             
+------------------------------------------------------------
+ 413294435277809344957685441227343146614594393746575438.725
+(1 row)
+
+select exp(-123.456);
+                                   exp                                   
+-------------------------------------------------------------------------
+ 0.000000000000000000000000000000000000000000000000000002419582541264601
+(1 row)
+
+-- big test
+select exp(1234.5678);
+                                                                                                                                                                                                                                                                              exp                                                                                                                                                                                                                                                                               
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ 146549072930959479983482138503979804217622199675223653966270157446954995433819741094410764947112047906012815540251009949604426069672532417736057033099274204598385314594846509975629046864798765888104789074984927709616261452461385220475510438783429612447831614003668421849727379202555580791042606170523016207262965336641214601082882495255771621327088265411334088968112458492660609809762865582162764292604697957813514621259353683899630997077707406305730694385703091201347848855199354307506425820147289848677003277208302716466011827836279231.9667
+(1 row)
+
 --
 -- Tests for generate_series
 --
@@ -1523,3 +1680,965 @@ select * from generate_series(1::numeric
  3 | 4
 (10 rows)
 
+--
+-- Tests for LN()
+--
+-- Invalid inputs
+select ln(-12.34);
+ERROR:  cannot take logarithm of a negative number
+select ln(0.0);
+ERROR:  cannot take logarithm of zero
+-- Some random tests
+select ln(1.2345678e-28);
+                   ln                    
+-----------------------------------------
+ -64.26166165451762991204894255882820859
+(1 row)
+
+select ln(0.0456789);
+         ln          
+---------------------
+ -3.0861187944847439
+(1 row)
+
+select ln(0.349873948359354029493948309745709580730482050975);
+                         ln                          
+-----------------------------------------------------
+ -1.050182336912082775693991697979750253056317885460
+(1 row)
+
+select ln(0.99949452);
+           ln            
+-------------------------
+ -0.00050560779808326467
+(1 row)
+
+select ln(1.00049687395);
+           ln           
+------------------------
+ 0.00049675054901370394
+(1 row)
+
+select ln(1234.567890123456789);
+         ln         
+--------------------
+ 7.1184763012977896
+(1 row)
+
+select ln(5.80397490724e5);
+         ln         
+--------------------
+ 13.271468476626518
+(1 row)
+
+select ln(9.342536355e34);
+         ln         
+--------------------
+ 80.522470935524187
+(1 row)
+
+-- input very small
+--
+-- bc(1) results computed with a scale of 500 and truncated using the script
+-- below, and then rounded by hand to match the precision of LN():
+--
+-- for p in {1..40} ; do l=$(bc -ql <<< "scale=500 ; l(10^-$p)" | head -n 1); echo "('1.0e-$p', $l)," ; done
+WITH t(x, bc_result) AS (VALUES
+('1.0e-1', -2.3025850929940457),
+('1.0e-2', -4.6051701859880914),
+('1.0e-3', -6.9077552789821371),
+('1.0e-4', -9.2103403719761827),
+('1.0e-5', -11.512925464970228),
+('1.0e-6', -13.815510557964274),
+('1.0e-7', -16.118095650958320),
+('1.0e-8', -18.420680743952365),
+('1.0e-9', -20.723265836946411),
+('1.0e-10', -23.025850929940457),
+('1.0e-11', -25.328436022934503),
+('1.0e-12', -27.631021115928548),
+('1.0e-13', -29.933606208922594),
+('1.0e-14', -32.236191301916640),
+('1.0e-15', -34.5387763949106853),
+('1.0e-16', -36.84136148790473094),
+('1.0e-17', -39.143946580898776628),
+('1.0e-18', -41.4465316738928223123),
+('1.0e-19', -43.74911676688686799634),
+('1.0e-20', -46.051701859880913680360),
+('1.0e-21', -48.3542869528749593643778),
+('1.0e-22', -50.65687204586900504839581),
+('1.0e-23', -52.959457138863050732413803),
+('1.0e-24', -55.2620422318570964164317949),
+('1.0e-25', -57.56462732485114210044978637),
+('1.0e-26', -59.867212417845187784467777822),
+('1.0e-27', -62.1697975108392334684857692765),
+('1.0e-28', -64.47238260383327915250376073116),
+('1.0e-29', -66.774967696827324836521752185847),
+('1.0e-30', -69.0775527898213705205397436405309),
+('1.0e-31', -71.38013788281541620455773509521529),
+('1.0e-32', -73.682722975809461888575726549899655),
+('1.0e-33', -75.9853080688035075725937180045840189),
+('1.0e-34', -78.28789316179755325661170945926838306),
+('1.0e-35', -80.590478254791598940629700913952747266),
+('1.0e-36', -82.8930633477856446246476923686371114736),
+('1.0e-37', -85.19564844077969030866568382332147568124),
+('1.0e-38', -87.498233533773735992683675278005839888842),
+('1.0e-39', -89.8008186267677816767016667326902040964430),
+('1.0e-40', -92.10340371976182736071965818737456830404406))
+SELECT x, bc_result, ln(x::numeric), ln(x::numeric)-bc_result AS diff FROM t;
+    x    |                   bc_result                   |                      ln                       |                    diff                     
+---------+-----------------------------------------------+-----------------------------------------------+---------------------------------------------
+ 1.0e-1  |                           -2.3025850929940457 |                           -2.3025850929940457 |                          0.0000000000000000
+ 1.0e-2  |                           -4.6051701859880914 |                           -4.6051701859880914 |                          0.0000000000000000
+ 1.0e-3  |                           -6.9077552789821371 |                           -6.9077552789821371 |                          0.0000000000000000
+ 1.0e-4  |                           -9.2103403719761827 |                           -9.2103403719761827 |                          0.0000000000000000
+ 1.0e-5  |                           -11.512925464970228 |                           -11.512925464970228 |                           0.000000000000000
+ 1.0e-6  |                           -13.815510557964274 |                           -13.815510557964274 |                           0.000000000000000
+ 1.0e-7  |                           -16.118095650958320 |                           -16.118095650958320 |                           0.000000000000000
+ 1.0e-8  |                           -18.420680743952365 |                           -18.420680743952365 |                           0.000000000000000
+ 1.0e-9  |                           -20.723265836946411 |                           -20.723265836946411 |                           0.000000000000000
+ 1.0e-10 |                           -23.025850929940457 |                           -23.025850929940457 |                           0.000000000000000
+ 1.0e-11 |                           -25.328436022934503 |                           -25.328436022934503 |                           0.000000000000000
+ 1.0e-12 |                           -27.631021115928548 |                           -27.631021115928548 |                           0.000000000000000
+ 1.0e-13 |                           -29.933606208922594 |                           -29.933606208922594 |                           0.000000000000000
+ 1.0e-14 |                           -32.236191301916640 |                           -32.236191301916640 |                           0.000000000000000
+ 1.0e-15 |                          -34.5387763949106853 |                          -34.5387763949106853 |                          0.0000000000000000
+ 1.0e-16 |                         -36.84136148790473094 |                         -36.84136148790473094 |                         0.00000000000000000
+ 1.0e-17 |                        -39.143946580898776628 |                        -39.143946580898776628 |                        0.000000000000000000
+ 1.0e-18 |                       -41.4465316738928223123 |                       -41.4465316738928223123 |                       0.0000000000000000000
+ 1.0e-19 |                      -43.74911676688686799634 |                      -43.74911676688686799634 |                      0.00000000000000000000
+ 1.0e-20 |                     -46.051701859880913680360 |                     -46.051701859880913680360 |                     0.000000000000000000000
+ 1.0e-21 |                    -48.3542869528749593643778 |                    -48.3542869528749593643778 |                    0.0000000000000000000000
+ 1.0e-22 |                   -50.65687204586900504839581 |                   -50.65687204586900504839581 |                   0.00000000000000000000000
+ 1.0e-23 |                  -52.959457138863050732413803 |                  -52.959457138863050732413803 |                  0.000000000000000000000000
+ 1.0e-24 |                 -55.2620422318570964164317949 |                 -55.2620422318570964164317949 |                 0.0000000000000000000000000
+ 1.0e-25 |                -57.56462732485114210044978637 |                -57.56462732485114210044978637 |                0.00000000000000000000000000
+ 1.0e-26 |               -59.867212417845187784467777822 |               -59.867212417845187784467777822 |               0.000000000000000000000000000
+ 1.0e-27 |              -62.1697975108392334684857692765 |              -62.1697975108392334684857692765 |              0.0000000000000000000000000000
+ 1.0e-28 |             -64.47238260383327915250376073116 |             -64.47238260383327915250376073116 |             0.00000000000000000000000000000
+ 1.0e-29 |            -66.774967696827324836521752185847 |            -66.774967696827324836521752185847 |            0.000000000000000000000000000000
+ 1.0e-30 |           -69.0775527898213705205397436405309 |           -69.0775527898213705205397436405309 |           0.0000000000000000000000000000000
+ 1.0e-31 |          -71.38013788281541620455773509521529 |          -71.38013788281541620455773509521529 |          0.00000000000000000000000000000000
+ 1.0e-32 |         -73.682722975809461888575726549899655 |         -73.682722975809461888575726549899655 |         0.000000000000000000000000000000000
+ 1.0e-33 |        -75.9853080688035075725937180045840189 |        -75.9853080688035075725937180045840189 |        0.0000000000000000000000000000000000
+ 1.0e-34 |       -78.28789316179755325661170945926838306 |       -78.28789316179755325661170945926838306 |       0.00000000000000000000000000000000000
+ 1.0e-35 |      -80.590478254791598940629700913952747266 |      -80.590478254791598940629700913952747266 |      0.000000000000000000000000000000000000
+ 1.0e-36 |     -82.8930633477856446246476923686371114736 |     -82.8930633477856446246476923686371114736 |     0.0000000000000000000000000000000000000
+ 1.0e-37 |    -85.19564844077969030866568382332147568124 |    -85.19564844077969030866568382332147568124 |    0.00000000000000000000000000000000000000
+ 1.0e-38 |   -87.498233533773735992683675278005839888842 |   -87.498233533773735992683675278005839888842 |   0.000000000000000000000000000000000000000
+ 1.0e-39 |  -89.8008186267677816767016667326902040964430 |  -89.8008186267677816767016667326902040964430 |  0.0000000000000000000000000000000000000000
+ 1.0e-40 | -92.10340371976182736071965818737456830404406 | -92.10340371976182736071965818737456830404406 | 0.00000000000000000000000000000000000000000
+(40 rows)
+
+-- input very close to but smaller than 1
+--
+-- bc(1) results computed with a scale of 500 and truncated using the script
+-- below, and then rounded by hand to match the precision of LN():
+--
+-- for p in {1..40} ; do l=$(bc -ql <<< "scale=500 ; l(1-10^-$p)" | head -n 1); echo "('1.0e-$p', $l)," ; done
+WITH t(x, bc_result) AS (VALUES
+('1.0e-1', -.10536051565782630),
+('1.0e-2', -.010050335853501441),
+('1.0e-3', -.0010005003335835335),
+('1.0e-4', -.00010000500033335834),
+('1.0e-5', -.000010000050000333336),
+('1.0e-6', -.0000010000005000003333),
+('1.0e-7', -.00000010000000500000033),
+('1.0e-8', -.000000010000000050000000),
+('1.0e-9', -.0000000010000000005000000),
+('1.0e-10', -.00000000010000000000500000),
+('1.0e-11', -.000000000010000000000050000),
+('1.0e-12', -.0000000000010000000000005000),
+('1.0e-13', -.00000000000010000000000000500),
+('1.0e-14', -.000000000000010000000000000050),
+('1.0e-15', -.0000000000000010000000000000005),
+('1.0e-16', -.00000000000000010000000000000001),
+('1.0e-17', -.000000000000000010000000000000000),
+('1.0e-18', -.0000000000000000010000000000000000),
+('1.0e-19', -.00000000000000000010000000000000000),
+('1.0e-20', -.000000000000000000010000000000000000),
+('1.0e-21', -.0000000000000000000010000000000000000),
+('1.0e-22', -.00000000000000000000010000000000000000),
+('1.0e-23', -.000000000000000000000010000000000000000),
+('1.0e-24', -.0000000000000000000000010000000000000000),
+('1.0e-25', -.00000000000000000000000010000000000000000),
+('1.0e-26', -.000000000000000000000000010000000000000000),
+('1.0e-27', -.0000000000000000000000000010000000000000000),
+('1.0e-28', -.00000000000000000000000000010000000000000000),
+('1.0e-29', -.000000000000000000000000000010000000000000000),
+('1.0e-30', -.0000000000000000000000000000010000000000000000),
+('1.0e-31', -.00000000000000000000000000000010000000000000000),
+('1.0e-32', -.000000000000000000000000000000010000000000000000),
+('1.0e-33', -.0000000000000000000000000000000010000000000000000),
+('1.0e-34', -.00000000000000000000000000000000010000000000000000),
+('1.0e-35', -.000000000000000000000000000000000010000000000000000),
+('1.0e-36', -.0000000000000000000000000000000000010000000000000000),
+('1.0e-37', -.00000000000000000000000000000000000010000000000000000),
+('1.0e-38', -.000000000000000000000000000000000000010000000000000000),
+('1.0e-39', -.0000000000000000000000000000000000000010000000000000000),
+('1.0e-40', -.00000000000000000000000000000000000000010000000000000000))
+SELECT '1-'||x, bc_result, ln(1.0-x::numeric), ln(1.0-x::numeric)-bc_result AS diff FROM t;
+ ?column?  |                          bc_result                          |                             ln                              |                            diff                            
+-----------+-------------------------------------------------------------+-------------------------------------------------------------+------------------------------------------------------------
+ 1-1.0e-1  |                                        -0.10536051565782630 |                                        -0.10536051565782630 |                                        0.00000000000000000
+ 1-1.0e-2  |                                       -0.010050335853501441 |                                       -0.010050335853501441 |                                       0.000000000000000000
+ 1-1.0e-3  |                                      -0.0010005003335835335 |                                      -0.0010005003335835335 |                                      0.0000000000000000000
+ 1-1.0e-4  |                                     -0.00010000500033335834 |                                     -0.00010000500033335834 |                                     0.00000000000000000000
+ 1-1.0e-5  |                                    -0.000010000050000333336 |                                    -0.000010000050000333336 |                                    0.000000000000000000000
+ 1-1.0e-6  |                                   -0.0000010000005000003333 |                                   -0.0000010000005000003333 |                                   0.0000000000000000000000
+ 1-1.0e-7  |                                  -0.00000010000000500000033 |                                  -0.00000010000000500000033 |                                  0.00000000000000000000000
+ 1-1.0e-8  |                                 -0.000000010000000050000000 |                                 -0.000000010000000050000000 |                                 0.000000000000000000000000
+ 1-1.0e-9  |                                -0.0000000010000000005000000 |                                -0.0000000010000000005000000 |                                0.0000000000000000000000000
+ 1-1.0e-10 |                               -0.00000000010000000000500000 |                               -0.00000000010000000000500000 |                               0.00000000000000000000000000
+ 1-1.0e-11 |                              -0.000000000010000000000050000 |                              -0.000000000010000000000050000 |                              0.000000000000000000000000000
+ 1-1.0e-12 |                             -0.0000000000010000000000005000 |                             -0.0000000000010000000000005000 |                             0.0000000000000000000000000000
+ 1-1.0e-13 |                            -0.00000000000010000000000000500 |                            -0.00000000000010000000000000500 |                            0.00000000000000000000000000000
+ 1-1.0e-14 |                           -0.000000000000010000000000000050 |                           -0.000000000000010000000000000050 |                           0.000000000000000000000000000000
+ 1-1.0e-15 |                          -0.0000000000000010000000000000005 |                          -0.0000000000000010000000000000005 |                          0.0000000000000000000000000000000
+ 1-1.0e-16 |                         -0.00000000000000010000000000000001 |                         -0.00000000000000010000000000000001 |                         0.00000000000000000000000000000000
+ 1-1.0e-17 |                        -0.000000000000000010000000000000000 |                        -0.000000000000000010000000000000000 |                        0.000000000000000000000000000000000
+ 1-1.0e-18 |                       -0.0000000000000000010000000000000000 |                       -0.0000000000000000010000000000000000 |                       0.0000000000000000000000000000000000
+ 1-1.0e-19 |                      -0.00000000000000000010000000000000000 |                      -0.00000000000000000010000000000000000 |                      0.00000000000000000000000000000000000
+ 1-1.0e-20 |                     -0.000000000000000000010000000000000000 |                     -0.000000000000000000010000000000000000 |                     0.000000000000000000000000000000000000
+ 1-1.0e-21 |                    -0.0000000000000000000010000000000000000 |                    -0.0000000000000000000010000000000000000 |                    0.0000000000000000000000000000000000000
+ 1-1.0e-22 |                   -0.00000000000000000000010000000000000000 |                   -0.00000000000000000000010000000000000000 |                   0.00000000000000000000000000000000000000
+ 1-1.0e-23 |                  -0.000000000000000000000010000000000000000 |                  -0.000000000000000000000010000000000000000 |                  0.000000000000000000000000000000000000000
+ 1-1.0e-24 |                 -0.0000000000000000000000010000000000000000 |                 -0.0000000000000000000000010000000000000000 |                 0.0000000000000000000000000000000000000000
+ 1-1.0e-25 |                -0.00000000000000000000000010000000000000000 |                -0.00000000000000000000000010000000000000000 |                0.00000000000000000000000000000000000000000
+ 1-1.0e-26 |               -0.000000000000000000000000010000000000000000 |               -0.000000000000000000000000010000000000000000 |               0.000000000000000000000000000000000000000000
+ 1-1.0e-27 |              -0.0000000000000000000000000010000000000000000 |              -0.0000000000000000000000000010000000000000000 |              0.0000000000000000000000000000000000000000000
+ 1-1.0e-28 |             -0.00000000000000000000000000010000000000000000 |             -0.00000000000000000000000000010000000000000000 |             0.00000000000000000000000000000000000000000000
+ 1-1.0e-29 |            -0.000000000000000000000000000010000000000000000 |            -0.000000000000000000000000000010000000000000000 |            0.000000000000000000000000000000000000000000000
+ 1-1.0e-30 |           -0.0000000000000000000000000000010000000000000000 |           -0.0000000000000000000000000000010000000000000000 |           0.0000000000000000000000000000000000000000000000
+ 1-1.0e-31 |          -0.00000000000000000000000000000010000000000000000 |          -0.00000000000000000000000000000010000000000000000 |          0.00000000000000000000000000000000000000000000000
+ 1-1.0e-32 |         -0.000000000000000000000000000000010000000000000000 |         -0.000000000000000000000000000000010000000000000000 |         0.000000000000000000000000000000000000000000000000
+ 1-1.0e-33 |        -0.0000000000000000000000000000000010000000000000000 |        -0.0000000000000000000000000000000010000000000000000 |        0.0000000000000000000000000000000000000000000000000
+ 1-1.0e-34 |       -0.00000000000000000000000000000000010000000000000000 |       -0.00000000000000000000000000000000010000000000000000 |       0.00000000000000000000000000000000000000000000000000
+ 1-1.0e-35 |      -0.000000000000000000000000000000000010000000000000000 |      -0.000000000000000000000000000000000010000000000000000 |      0.000000000000000000000000000000000000000000000000000
+ 1-1.0e-36 |     -0.0000000000000000000000000000000000010000000000000000 |     -0.0000000000000000000000000000000000010000000000000000 |     0.0000000000000000000000000000000000000000000000000000
+ 1-1.0e-37 |    -0.00000000000000000000000000000000000010000000000000000 |    -0.00000000000000000000000000000000000010000000000000000 |    0.00000000000000000000000000000000000000000000000000000
+ 1-1.0e-38 |   -0.000000000000000000000000000000000000010000000000000000 |   -0.000000000000000000000000000000000000010000000000000000 |   0.000000000000000000000000000000000000000000000000000000
+ 1-1.0e-39 |  -0.0000000000000000000000000000000000000010000000000000000 |  -0.0000000000000000000000000000000000000010000000000000000 |  0.0000000000000000000000000000000000000000000000000000000
+ 1-1.0e-40 | -0.00000000000000000000000000000000000000010000000000000000 | -0.00000000000000000000000000000000000000010000000000000000 | 0.00000000000000000000000000000000000000000000000000000000
+(40 rows)
+
+-- input very close to but larger than 1
+--
+-- bc(1) results computed with a scale of 500 and truncated using the script
+-- below, and then rounded by hand to match the precision of LN():
+--
+-- for p in {1..40} ; do l=$(bc -ql <<< "scale=500 ; l(1+10^-$p)" | head -n 1); echo "('1.0e-$p', $l)," ; done
+WITH t(x, bc_result) AS (VALUES
+('1.0e-1', .09531017980432486),
+('1.0e-2', .009950330853168083),
+('1.0e-3', .0009995003330835332),
+('1.0e-4', .00009999500033330834),
+('1.0e-5', .000009999950000333331),
+('1.0e-6', .0000009999995000003333),
+('1.0e-7', .00000009999999500000033),
+('1.0e-8', .000000009999999950000000),
+('1.0e-9', .0000000009999999995000000),
+('1.0e-10', .00000000009999999999500000),
+('1.0e-11', .000000000009999999999950000),
+('1.0e-12', .0000000000009999999999995000),
+('1.0e-13', .00000000000009999999999999500),
+('1.0e-14', .000000000000009999999999999950),
+('1.0e-15', .0000000000000009999999999999995),
+('1.0e-16', .00000000000000010000000000000000),
+('1.0e-17', .000000000000000010000000000000000),
+('1.0e-18', .0000000000000000010000000000000000),
+('1.0e-19', .00000000000000000010000000000000000),
+('1.0e-20', .000000000000000000010000000000000000),
+('1.0e-21', .0000000000000000000010000000000000000),
+('1.0e-22', .00000000000000000000010000000000000000),
+('1.0e-23', .000000000000000000000010000000000000000),
+('1.0e-24', .0000000000000000000000010000000000000000),
+('1.0e-25', .00000000000000000000000010000000000000000),
+('1.0e-26', .000000000000000000000000010000000000000000),
+('1.0e-27', .0000000000000000000000000010000000000000000),
+('1.0e-28', .00000000000000000000000000010000000000000000),
+('1.0e-29', .000000000000000000000000000010000000000000000),
+('1.0e-30', .0000000000000000000000000000010000000000000000),
+('1.0e-31', .00000000000000000000000000000010000000000000000),
+('1.0e-32', .000000000000000000000000000000010000000000000000),
+('1.0e-33', .0000000000000000000000000000000010000000000000000),
+('1.0e-34', .00000000000000000000000000000000010000000000000000),
+('1.0e-35', .000000000000000000000000000000000010000000000000000),
+('1.0e-36', .0000000000000000000000000000000000010000000000000000),
+('1.0e-37', .00000000000000000000000000000000000010000000000000000),
+('1.0e-38', .000000000000000000000000000000000000010000000000000000),
+('1.0e-39', .0000000000000000000000000000000000000010000000000000000),
+('1.0e-40', .00000000000000000000000000000000000000010000000000000000))
+SELECT '1+'||x, bc_result, ln(1.0+x::numeric), ln(1.0+x::numeric)-bc_result AS diff FROM t;
+ ?column?  |                         bc_result                          |                             ln                             |                            diff                            
+-----------+------------------------------------------------------------+------------------------------------------------------------+------------------------------------------------------------
+ 1+1.0e-1  |                                        0.09531017980432486 |                                        0.09531017980432486 |                                        0.00000000000000000
+ 1+1.0e-2  |                                       0.009950330853168083 |                                       0.009950330853168083 |                                       0.000000000000000000
+ 1+1.0e-3  |                                      0.0009995003330835332 |                                      0.0009995003330835332 |                                      0.0000000000000000000
+ 1+1.0e-4  |                                     0.00009999500033330834 |                                     0.00009999500033330834 |                                     0.00000000000000000000
+ 1+1.0e-5  |                                    0.000009999950000333331 |                                    0.000009999950000333331 |                                    0.000000000000000000000
+ 1+1.0e-6  |                                   0.0000009999995000003333 |                                   0.0000009999995000003333 |                                   0.0000000000000000000000
+ 1+1.0e-7  |                                  0.00000009999999500000033 |                                  0.00000009999999500000033 |                                  0.00000000000000000000000
+ 1+1.0e-8  |                                 0.000000009999999950000000 |                                 0.000000009999999950000000 |                                 0.000000000000000000000000
+ 1+1.0e-9  |                                0.0000000009999999995000000 |                                0.0000000009999999995000000 |                                0.0000000000000000000000000
+ 1+1.0e-10 |                               0.00000000009999999999500000 |                               0.00000000009999999999500000 |                               0.00000000000000000000000000
+ 1+1.0e-11 |                              0.000000000009999999999950000 |                              0.000000000009999999999950000 |                              0.000000000000000000000000000
+ 1+1.0e-12 |                             0.0000000000009999999999995000 |                             0.0000000000009999999999995000 |                             0.0000000000000000000000000000
+ 1+1.0e-13 |                            0.00000000000009999999999999500 |                            0.00000000000009999999999999500 |                            0.00000000000000000000000000000
+ 1+1.0e-14 |                           0.000000000000009999999999999950 |                           0.000000000000009999999999999950 |                           0.000000000000000000000000000000
+ 1+1.0e-15 |                          0.0000000000000009999999999999995 |                          0.0000000000000009999999999999995 |                          0.0000000000000000000000000000000
+ 1+1.0e-16 |                         0.00000000000000010000000000000000 |                         0.00000000000000010000000000000000 |                         0.00000000000000000000000000000000
+ 1+1.0e-17 |                        0.000000000000000010000000000000000 |                        0.000000000000000010000000000000000 |                        0.000000000000000000000000000000000
+ 1+1.0e-18 |                       0.0000000000000000010000000000000000 |                       0.0000000000000000010000000000000000 |                       0.0000000000000000000000000000000000
+ 1+1.0e-19 |                      0.00000000000000000010000000000000000 |                      0.00000000000000000010000000000000000 |                      0.00000000000000000000000000000000000
+ 1+1.0e-20 |                     0.000000000000000000010000000000000000 |                     0.000000000000000000010000000000000000 |                     0.000000000000000000000000000000000000
+ 1+1.0e-21 |                    0.0000000000000000000010000000000000000 |                    0.0000000000000000000010000000000000000 |                    0.0000000000000000000000000000000000000
+ 1+1.0e-22 |                   0.00000000000000000000010000000000000000 |                   0.00000000000000000000010000000000000000 |                   0.00000000000000000000000000000000000000
+ 1+1.0e-23 |                  0.000000000000000000000010000000000000000 |                  0.000000000000000000000010000000000000000 |                  0.000000000000000000000000000000000000000
+ 1+1.0e-24 |                 0.0000000000000000000000010000000000000000 |                 0.0000000000000000000000010000000000000000 |                 0.0000000000000000000000000000000000000000
+ 1+1.0e-25 |                0.00000000000000000000000010000000000000000 |                0.00000000000000000000000010000000000000000 |                0.00000000000000000000000000000000000000000
+ 1+1.0e-26 |               0.000000000000000000000000010000000000000000 |               0.000000000000000000000000010000000000000000 |               0.000000000000000000000000000000000000000000
+ 1+1.0e-27 |              0.0000000000000000000000000010000000000000000 |              0.0000000000000000000000000010000000000000000 |              0.0000000000000000000000000000000000000000000
+ 1+1.0e-28 |             0.00000000000000000000000000010000000000000000 |             0.00000000000000000000000000010000000000000000 |             0.00000000000000000000000000000000000000000000
+ 1+1.0e-29 |            0.000000000000000000000000000010000000000000000 |            0.000000000000000000000000000010000000000000000 |            0.000000000000000000000000000000000000000000000
+ 1+1.0e-30 |           0.0000000000000000000000000000010000000000000000 |           0.0000000000000000000000000000010000000000000000 |           0.0000000000000000000000000000000000000000000000
+ 1+1.0e-31 |          0.00000000000000000000000000000010000000000000000 |          0.00000000000000000000000000000010000000000000000 |          0.00000000000000000000000000000000000000000000000
+ 1+1.0e-32 |         0.000000000000000000000000000000010000000000000000 |         0.000000000000000000000000000000010000000000000000 |         0.000000000000000000000000000000000000000000000000
+ 1+1.0e-33 |        0.0000000000000000000000000000000010000000000000000 |        0.0000000000000000000000000000000010000000000000000 |        0.0000000000000000000000000000000000000000000000000
+ 1+1.0e-34 |       0.00000000000000000000000000000000010000000000000000 |       0.00000000000000000000000000000000010000000000000000 |       0.00000000000000000000000000000000000000000000000000
+ 1+1.0e-35 |      0.000000000000000000000000000000000010000000000000000 |      0.000000000000000000000000000000000010000000000000000 |      0.000000000000000000000000000000000000000000000000000
+ 1+1.0e-36 |     0.0000000000000000000000000000000000010000000000000000 |     0.0000000000000000000000000000000000010000000000000000 |     0.0000000000000000000000000000000000000000000000000000
+ 1+1.0e-37 |    0.00000000000000000000000000000000000010000000000000000 |    0.00000000000000000000000000000000000010000000000000000 |    0.00000000000000000000000000000000000000000000000000000
+ 1+1.0e-38 |   0.000000000000000000000000000000000000010000000000000000 |   0.000000000000000000000000000000000000010000000000000000 |   0.000000000000000000000000000000000000000000000000000000
+ 1+1.0e-39 |  0.0000000000000000000000000000000000000010000000000000000 |  0.0000000000000000000000000000000000000010000000000000000 |  0.0000000000000000000000000000000000000000000000000000000
+ 1+1.0e-40 | 0.00000000000000000000000000000000000000010000000000000000 | 0.00000000000000000000000000000000000000010000000000000000 | 0.00000000000000000000000000000000000000000000000000000000
+(40 rows)
+
+-- input very large
+--
+-- bc(1) results computed with a scale of 500 and truncated using the script
+-- below, and then rounded by hand to match the precision of LN():
+--
+-- for p in {1..40} ; do l=$(bc -ql <<< "scale=500 ; l(10^$p)" | head -n 1); echo "('1.0e$p', $l)," ; done
+WITH t(x, bc_result) AS (VALUES
+('1.0e1', 2.3025850929940457),
+('1.0e2', 4.6051701859880914),
+('1.0e3', 6.9077552789821371),
+('1.0e4', 9.2103403719761827),
+('1.0e5', 11.512925464970228),
+('1.0e6', 13.815510557964274),
+('1.0e7', 16.118095650958320),
+('1.0e8', 18.420680743952365),
+('1.0e9', 20.723265836946411),
+('1.0e10', 23.025850929940457),
+('1.0e11', 25.328436022934503),
+('1.0e12', 27.631021115928548),
+('1.0e13', 29.933606208922594),
+('1.0e14', 32.236191301916640),
+('1.0e15', 34.538776394910685),
+('1.0e16', 36.841361487904731),
+('1.0e17', 39.143946580898777),
+('1.0e18', 41.446531673892822),
+('1.0e19', 43.749116766886868),
+('1.0e20', 46.051701859880914),
+('1.0e21', 48.354286952874959),
+('1.0e22', 50.656872045869005),
+('1.0e23', 52.959457138863051),
+('1.0e24', 55.262042231857096),
+('1.0e25', 57.564627324851142),
+('1.0e26', 59.867212417845188),
+('1.0e27', 62.169797510839233),
+('1.0e28', 64.472382603833279),
+('1.0e29', 66.774967696827325),
+('1.0e30', 69.077552789821371),
+('1.0e31', 71.380137882815416),
+('1.0e32', 73.682722975809462),
+('1.0e33', 75.985308068803508),
+('1.0e34', 78.287893161797553),
+('1.0e35', 80.590478254791599),
+('1.0e36', 82.893063347785645),
+('1.0e37', 85.195648440779690),
+('1.0e38', 87.498233533773736),
+('1.0e39', 89.800818626767782),
+('1.0e40', 92.103403719761827))
+SELECT x, bc_result, ln(x::numeric), ln(x::numeric)-bc_result AS diff FROM t;
+   x    |     bc_result      |         ln         |        diff        
+--------+--------------------+--------------------+--------------------
+ 1.0e1  | 2.3025850929940457 | 2.3025850929940457 | 0.0000000000000000
+ 1.0e2  | 4.6051701859880914 | 4.6051701859880914 | 0.0000000000000000
+ 1.0e3  | 6.9077552789821371 | 6.9077552789821371 | 0.0000000000000000
+ 1.0e4  | 9.2103403719761827 | 9.2103403719761827 | 0.0000000000000000
+ 1.0e5  | 11.512925464970228 | 11.512925464970228 |  0.000000000000000
+ 1.0e6  | 13.815510557964274 | 13.815510557964274 |  0.000000000000000
+ 1.0e7  | 16.118095650958320 | 16.118095650958320 |  0.000000000000000
+ 1.0e8  | 18.420680743952365 | 18.420680743952365 |  0.000000000000000
+ 1.0e9  | 20.723265836946411 | 20.723265836946411 |  0.000000000000000
+ 1.0e10 | 23.025850929940457 | 23.025850929940457 |  0.000000000000000
+ 1.0e11 | 25.328436022934503 | 25.328436022934503 |  0.000000000000000
+ 1.0e12 | 27.631021115928548 | 27.631021115928548 |  0.000000000000000
+ 1.0e13 | 29.933606208922594 | 29.933606208922594 |  0.000000000000000
+ 1.0e14 | 32.236191301916640 | 32.236191301916640 |  0.000000000000000
+ 1.0e15 | 34.538776394910685 | 34.538776394910685 |  0.000000000000000
+ 1.0e16 | 36.841361487904731 | 36.841361487904731 |  0.000000000000000
+ 1.0e17 | 39.143946580898777 | 39.143946580898777 |  0.000000000000000
+ 1.0e18 | 41.446531673892822 | 41.446531673892822 |  0.000000000000000
+ 1.0e19 | 43.749116766886868 | 43.749116766886868 |  0.000000000000000
+ 1.0e20 | 46.051701859880914 | 46.051701859880914 |  0.000000000000000
+ 1.0e21 | 48.354286952874959 | 48.354286952874959 |  0.000000000000000
+ 1.0e22 | 50.656872045869005 | 50.656872045869005 |  0.000000000000000
+ 1.0e23 | 52.959457138863051 | 52.959457138863051 |  0.000000000000000
+ 1.0e24 | 55.262042231857096 | 55.262042231857096 |  0.000000000000000
+ 1.0e25 | 57.564627324851142 | 57.564627324851142 |  0.000000000000000
+ 1.0e26 | 59.867212417845188 | 59.867212417845188 |  0.000000000000000
+ 1.0e27 | 62.169797510839233 | 62.169797510839233 |  0.000000000000000
+ 1.0e28 | 64.472382603833279 | 64.472382603833279 |  0.000000000000000
+ 1.0e29 | 66.774967696827325 | 66.774967696827325 |  0.000000000000000
+ 1.0e30 | 69.077552789821371 | 69.077552789821371 |  0.000000000000000
+ 1.0e31 | 71.380137882815416 | 71.380137882815416 |  0.000000000000000
+ 1.0e32 | 73.682722975809462 | 73.682722975809462 |  0.000000000000000
+ 1.0e33 | 75.985308068803508 | 75.985308068803508 |  0.000000000000000
+ 1.0e34 | 78.287893161797553 | 78.287893161797553 |  0.000000000000000
+ 1.0e35 | 80.590478254791599 | 80.590478254791599 |  0.000000000000000
+ 1.0e36 | 82.893063347785645 | 82.893063347785645 |  0.000000000000000
+ 1.0e37 | 85.195648440779690 | 85.195648440779690 |  0.000000000000000
+ 1.0e38 | 87.498233533773736 | 87.498233533773736 |  0.000000000000000
+ 1.0e39 | 89.800818626767782 | 89.800818626767782 |  0.000000000000000
+ 1.0e40 | 92.103403719761827 | 92.103403719761827 |  0.000000000000000
+(40 rows)
+
+-- input huge
+--
+-- bc(1) results computed with a scale of 1000 and truncated using the script
+-- below, and then rounded by hand to match the precision of LN():
+--
+-- for p in {1..10} ; do l=$(bc -ql <<< "scale=1000 ; l(10^${p}00)" | head -n 1); echo "('1.0e${p}00', $l)," ; done
+WITH t(x, bc_result) AS (VALUES
+('1.0e100', 230.25850929940457),
+('1.0e200', 460.51701859880914),
+('1.0e300', 690.77552789821371),
+('1.0e400', 921.03403719761827),
+('1.0e500', 1151.2925464970228),
+('1.0e600', 1381.5510557964274),
+('1.0e700', 1611.8095650958320),
+('1.0e800', 1842.0680743952365),
+('1.0e900', 2072.3265836946411),
+('1.0e1000', 2302.5850929940457))
+SELECT x, bc_result, ln(x::numeric), ln(x::numeric)-bc_result AS diff FROM t;
+    x     |     bc_result      |         ln         |       diff       
+----------+--------------------+--------------------+------------------
+ 1.0e100  | 230.25850929940457 | 230.25850929940457 | 0.00000000000000
+ 1.0e200  | 460.51701859880914 | 460.51701859880914 | 0.00000000000000
+ 1.0e300  | 690.77552789821371 | 690.77552789821371 | 0.00000000000000
+ 1.0e400  | 921.03403719761827 | 921.03403719761827 | 0.00000000000000
+ 1.0e500  | 1151.2925464970228 | 1151.2925464970228 |  0.0000000000000
+ 1.0e600  | 1381.5510557964274 | 1381.5510557964274 |  0.0000000000000
+ 1.0e700  | 1611.8095650958320 | 1611.8095650958320 |  0.0000000000000
+ 1.0e800  | 1842.0680743952365 | 1842.0680743952365 |  0.0000000000000
+ 1.0e900  | 2072.3265836946411 | 2072.3265836946411 |  0.0000000000000
+ 1.0e1000 | 2302.5850929940457 | 2302.5850929940457 |  0.0000000000000
+(10 rows)
+
+--
+-- Tests for LOG() (base 10)
+--
+-- invalid inputs
+select log(-12.34);
+ERROR:  cannot take logarithm of a negative number
+CONTEXT:  SQL function "log" statement 1
+select log(0);
+ERROR:  cannot take logarithm of zero
+-- some random tests
+select log(1.234567e-89);
+                                                 log                                                 
+-----------------------------------------------------------------------------------------------------
+ -88.90848533591373725637496492944925187293052336306443143312825869985819779294142441287021741054275
+(1 row)
+
+select log(3.4634998359873254962349856073435545);
+                 log                  
+--------------------------------------
+ 0.5395151714070134409152404011959981
+(1 row)
+
+select log(9.999999999999999999);
+         log          
+----------------------
+ 1.000000000000000000
+(1 row)
+
+select log(10.00000000000000000);
+         log         
+---------------------
+ 1.00000000000000000
+(1 row)
+
+select log(10.00000000000000001);
+         log         
+---------------------
+ 1.00000000000000000
+(1 row)
+
+select log(590489.45235237);
+        log        
+-------------------
+ 5.771212144411727
+(1 row)
+
+-- input very small, exact result known
+WITH t(x) AS (SELECT '1e-'||n FROM generate_series(1, 100) g(n))
+SELECT x, log(x::numeric) FROM t;
+   x    |                                                    log                                                    
+--------+-----------------------------------------------------------------------------------------------------------
+ 1e-1   |                                                                                       -1.0000000000000000
+ 1e-2   |                                                                                       -2.0000000000000000
+ 1e-3   |                                                                                       -3.0000000000000000
+ 1e-4   |                                                                                       -4.0000000000000000
+ 1e-5   |                                                                                        -5.000000000000000
+ 1e-6   |                                                                                        -6.000000000000000
+ 1e-7   |                                                                                        -7.000000000000000
+ 1e-8   |                                                                                        -8.000000000000000
+ 1e-9   |                                                                                        -9.000000000000000
+ 1e-10  |                                                                                       -10.000000000000000
+ 1e-11  |                                                                                       -11.000000000000000
+ 1e-12  |                                                                                       -12.000000000000000
+ 1e-13  |                                                                                       -13.000000000000000
+ 1e-14  |                                                                                       -14.000000000000000
+ 1e-15  |                                                                                       -15.000000000000000
+ 1e-16  |                                                                                      -16.0000000000000000
+ 1e-17  |                                                                                     -17.00000000000000000
+ 1e-18  |                                                                                    -18.000000000000000000
+ 1e-19  |                                                                                   -19.0000000000000000000
+ 1e-20  |                                                                                  -20.00000000000000000000
+ 1e-21  |                                                                                 -21.000000000000000000000
+ 1e-22  |                                                                                -22.0000000000000000000000
+ 1e-23  |                                                                               -23.00000000000000000000000
+ 1e-24  |                                                                              -24.000000000000000000000000
+ 1e-25  |                                                                             -25.0000000000000000000000000
+ 1e-26  |                                                                            -26.00000000000000000000000000
+ 1e-27  |                                                                           -27.000000000000000000000000000
+ 1e-28  |                                                                          -28.0000000000000000000000000000
+ 1e-29  |                                                                         -29.00000000000000000000000000000
+ 1e-30  |                                                                        -30.000000000000000000000000000000
+ 1e-31  |                                                                       -31.0000000000000000000000000000000
+ 1e-32  |                                                                      -32.00000000000000000000000000000000
+ 1e-33  |                                                                     -33.000000000000000000000000000000000
+ 1e-34  |                                                                    -34.0000000000000000000000000000000000
+ 1e-35  |                                                                   -35.00000000000000000000000000000000000
+ 1e-36  |                                                                  -36.000000000000000000000000000000000000
+ 1e-37  |                                                                 -37.0000000000000000000000000000000000000
+ 1e-38  |                                                                -38.00000000000000000000000000000000000000
+ 1e-39  |                                                               -39.000000000000000000000000000000000000000
+ 1e-40  |                                                              -40.0000000000000000000000000000000000000000
+ 1e-41  |                                                             -41.00000000000000000000000000000000000000000
+ 1e-42  |                                                            -42.000000000000000000000000000000000000000000
+ 1e-43  |                                                           -43.0000000000000000000000000000000000000000000
+ 1e-44  |                                                          -44.00000000000000000000000000000000000000000000
+ 1e-45  |                                                         -45.000000000000000000000000000000000000000000000
+ 1e-46  |                                                        -46.0000000000000000000000000000000000000000000000
+ 1e-47  |                                                       -47.00000000000000000000000000000000000000000000000
+ 1e-48  |                                                      -48.000000000000000000000000000000000000000000000000
+ 1e-49  |                                                     -49.0000000000000000000000000000000000000000000000000
+ 1e-50  |                                                    -50.00000000000000000000000000000000000000000000000000
+ 1e-51  |                                                   -51.000000000000000000000000000000000000000000000000000
+ 1e-52  |                                                  -52.0000000000000000000000000000000000000000000000000000
+ 1e-53  |                                                 -53.00000000000000000000000000000000000000000000000000000
+ 1e-54  |                                                -54.000000000000000000000000000000000000000000000000000000
+ 1e-55  |                                               -55.0000000000000000000000000000000000000000000000000000000
+ 1e-56  |                                              -56.00000000000000000000000000000000000000000000000000000000
+ 1e-57  |                                             -57.000000000000000000000000000000000000000000000000000000000
+ 1e-58  |                                            -58.0000000000000000000000000000000000000000000000000000000000
+ 1e-59  |                                           -59.00000000000000000000000000000000000000000000000000000000000
+ 1e-60  |                                          -60.000000000000000000000000000000000000000000000000000000000000
+ 1e-61  |                                         -61.0000000000000000000000000000000000000000000000000000000000000
+ 1e-62  |                                        -62.00000000000000000000000000000000000000000000000000000000000000
+ 1e-63  |                                       -63.000000000000000000000000000000000000000000000000000000000000000
+ 1e-64  |                                      -64.0000000000000000000000000000000000000000000000000000000000000000
+ 1e-65  |                                     -65.00000000000000000000000000000000000000000000000000000000000000000
+ 1e-66  |                                    -66.000000000000000000000000000000000000000000000000000000000000000000
+ 1e-67  |                                   -67.0000000000000000000000000000000000000000000000000000000000000000000
+ 1e-68  |                                  -68.00000000000000000000000000000000000000000000000000000000000000000000
+ 1e-69  |                                 -69.000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-70  |                                -70.0000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-71  |                               -71.00000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-72  |                              -72.000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-73  |                             -73.0000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-74  |                            -74.00000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-75  |                           -75.000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-76  |                          -76.0000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-77  |                         -77.00000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-78  |                        -78.000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-79  |                       -79.0000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-80  |                      -80.00000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-81  |                     -81.000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-82  |                    -82.0000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-83  |                   -83.00000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-84  |                  -84.000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-85  |                 -85.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-86  |                -86.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-87  |               -87.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-88  |              -88.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-89  |             -89.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-90  |            -90.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-91  |           -91.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-92  |          -92.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-93  |         -93.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-94  |        -94.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-95  |       -95.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-96  |      -96.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-97  |     -97.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-98  |    -98.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-99  |   -99.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+ 1e-100 | -100.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
+(100 rows)
+
+-- input very small, non-exact results
+--
+-- bc(1) results computed with a scale of 500 and truncated using the script
+-- below, and then rounded by hand to match the precision of LN():
+--
+-- for p in {1..50..7} ; do for d in {9..1..3} ; do l=$(bc -ql <<< "scale=500 ; l($d*10^-$p) / l(10)" | head -n 1); echo "('${d}.0e-$p', $l)," ; done ; done
+WITH t(x, bc_result) AS (VALUES
+('9.0e-1', -.04575749056067513),
+('6.0e-1', -.2218487496163564),
+('3.0e-1', -.5228787452803376),
+('9.0e-8', -7.045757490560675),
+('6.0e-8', -7.221848749616356),
+('3.0e-8', -7.522878745280338),
+('9.0e-15', -14.0457574905606751),
+('6.0e-15', -14.2218487496163564),
+('3.0e-15', -14.5228787452803376),
+('9.0e-22', -21.04575749056067512540994),
+('6.0e-22', -21.22184874961635636749123),
+('3.0e-22', -21.52287874528033756270497),
+('9.0e-29', -28.045757490560675125409944193490),
+('6.0e-29', -28.221848749616356367491233202020),
+('3.0e-29', -28.522878745280337562704972096745),
+('9.0e-36', -35.0457574905606751254099441934897693816),
+('6.0e-36', -35.2218487496163563674912332020203916640),
+('3.0e-36', -35.5228787452803375627049720967448846908),
+('9.0e-43', -42.04575749056067512540994419348976938159974227),
+('6.0e-43', -42.22184874961635636749123320202039166403168125),
+('3.0e-43', -42.52287874528033756270497209674488469079987114),
+('9.0e-50', -49.045757490560675125409944193489769381599742271618608),
+('6.0e-50', -49.221848749616356367491233202020391664031681254347196),
+('3.0e-50', -49.522878745280337562704972096744884690799871135809304))
+SELECT x, bc_result, log(x::numeric), log(x::numeric)-bc_result AS diff FROM t;
+    x    |                        bc_result                        |                           log                           |                         diff                          
+---------+---------------------------------------------------------+---------------------------------------------------------+-------------------------------------------------------
+ 9.0e-1  |                                    -0.04575749056067513 |                                    -0.04575749056067513 |                                   0.00000000000000000
+ 6.0e-1  |                                     -0.2218487496163564 |                                     -0.2218487496163564 |                                    0.0000000000000000
+ 3.0e-1  |                                     -0.5228787452803376 |                                     -0.5228787452803376 |                                    0.0000000000000000
+ 9.0e-8  |                                      -7.045757490560675 |                                      -7.045757490560675 |                                     0.000000000000000
+ 6.0e-8  |                                      -7.221848749616356 |                                      -7.221848749616356 |                                     0.000000000000000
+ 3.0e-8  |                                      -7.522878745280338 |                                      -7.522878745280338 |                                     0.000000000000000
+ 9.0e-15 |                                    -14.0457574905606751 |                                    -14.0457574905606751 |                                    0.0000000000000000
+ 6.0e-15 |                                    -14.2218487496163564 |                                    -14.2218487496163564 |                                    0.0000000000000000
+ 3.0e-15 |                                    -14.5228787452803376 |                                    -14.5228787452803376 |                                    0.0000000000000000
+ 9.0e-22 |                             -21.04575749056067512540994 |                             -21.04575749056067512540994 |                             0.00000000000000000000000
+ 6.0e-22 |                             -21.22184874961635636749123 |                             -21.22184874961635636749123 |                             0.00000000000000000000000
+ 3.0e-22 |                             -21.52287874528033756270497 |                             -21.52287874528033756270497 |                             0.00000000000000000000000
+ 9.0e-29 |                      -28.045757490560675125409944193490 |                      -28.045757490560675125409944193490 |                      0.000000000000000000000000000000
+ 6.0e-29 |                      -28.221848749616356367491233202020 |                      -28.221848749616356367491233202020 |                      0.000000000000000000000000000000
+ 3.0e-29 |                      -28.522878745280337562704972096745 |                      -28.522878745280337562704972096745 |                      0.000000000000000000000000000000
+ 9.0e-36 |               -35.0457574905606751254099441934897693816 |               -35.0457574905606751254099441934897693816 |               0.0000000000000000000000000000000000000
+ 6.0e-36 |               -35.2218487496163563674912332020203916640 |               -35.2218487496163563674912332020203916640 |               0.0000000000000000000000000000000000000
+ 3.0e-36 |               -35.5228787452803375627049720967448846908 |               -35.5228787452803375627049720967448846908 |               0.0000000000000000000000000000000000000
+ 9.0e-43 |        -42.04575749056067512540994419348976938159974227 |        -42.04575749056067512540994419348976938159974227 |        0.00000000000000000000000000000000000000000000
+ 6.0e-43 |        -42.22184874961635636749123320202039166403168125 |        -42.22184874961635636749123320202039166403168125 |        0.00000000000000000000000000000000000000000000
+ 3.0e-43 |        -42.52287874528033756270497209674488469079987114 |        -42.52287874528033756270497209674488469079987114 |        0.00000000000000000000000000000000000000000000
+ 9.0e-50 | -49.045757490560675125409944193489769381599742271618608 | -49.045757490560675125409944193489769381599742271618608 | 0.000000000000000000000000000000000000000000000000000
+ 6.0e-50 | -49.221848749616356367491233202020391664031681254347196 | -49.221848749616356367491233202020391664031681254347196 | 0.000000000000000000000000000000000000000000000000000
+ 3.0e-50 | -49.522878745280337562704972096744884690799871135809304 | -49.522878745280337562704972096744884690799871135809304 | 0.000000000000000000000000000000000000000000000000000
+(24 rows)
+
+-- input very close to but smaller than 1
+--
+-- bc(1) results computed with a scale of 500 and truncated using the script
+-- below, and then rounded by hand to match the precision of LN():
+--
+-- for p in {1..40..7} ; do for d in {9..1..3} ; do l=$(bc -ql <<< "scale=500 ; l(1-$d*10^-$p) / l(10)" | head -n 1); echo "('${d}.0e-$p', $l)," ; done ; done
+WITH t(x, bc_result) AS (VALUES
+('9.0e-1', -1.0000000000000000),
+('6.0e-1', -.3979400086720376),
+('3.0e-1', -.1549019599857432),
+('9.0e-8', -.000000039086505130185422),
+('6.0e-8', -.000000026057669695925208),
+('3.0e-8', -.000000013028834652530076),
+('9.0e-15', -.0000000000000039086503371292840),
+('6.0e-15', -.0000000000000026057668914195188),
+('3.0e-15', -.0000000000000013028834457097574),
+('9.0e-22', -.00000000000000000000039086503371292664),
+('6.0e-22', -.00000000000000000000026057668914195110),
+('3.0e-22', -.00000000000000000000013028834457097555),
+('9.0e-29', -.000000000000000000000000000039086503371292664),
+('6.0e-29', -.000000000000000000000000000026057668914195110),
+('3.0e-29', -.000000000000000000000000000013028834457097555),
+('9.0e-36', -.0000000000000000000000000000000000039086503371292664),
+('6.0e-36', -.0000000000000000000000000000000000026057668914195110),
+('3.0e-36', -.0000000000000000000000000000000000013028834457097555))
+SELECT '1-'||x, bc_result, log(1.0-x::numeric), log(1.0-x::numeric)-bc_result AS diff FROM t;
+ ?column?  |                        bc_result                        |                           log                           |                          diff                          
+-----------+---------------------------------------------------------+---------------------------------------------------------+--------------------------------------------------------
+ 1-9.0e-1  |                                     -1.0000000000000000 |                                     -1.0000000000000000 |                                     0.0000000000000000
+ 1-6.0e-1  |                                     -0.3979400086720376 |                                     -0.3979400086720376 |                                     0.0000000000000000
+ 1-3.0e-1  |                                     -0.1549019599857432 |                                     -0.1549019599857432 |                                     0.0000000000000000
+ 1-9.0e-8  |                             -0.000000039086505130185422 |                             -0.000000039086505130185422 |                             0.000000000000000000000000
+ 1-6.0e-8  |                             -0.000000026057669695925208 |                             -0.000000026057669695925208 |                             0.000000000000000000000000
+ 1-3.0e-8  |                             -0.000000013028834652530076 |                             -0.000000013028834652530076 |                             0.000000000000000000000000
+ 1-9.0e-15 |                      -0.0000000000000039086503371292840 |                      -0.0000000000000039086503371292840 |                      0.0000000000000000000000000000000
+ 1-6.0e-15 |                      -0.0000000000000026057668914195188 |                      -0.0000000000000026057668914195188 |                      0.0000000000000000000000000000000
+ 1-3.0e-15 |                      -0.0000000000000013028834457097574 |                      -0.0000000000000013028834457097574 |                      0.0000000000000000000000000000000
+ 1-9.0e-22 |               -0.00000000000000000000039086503371292664 |               -0.00000000000000000000039086503371292664 |               0.00000000000000000000000000000000000000
+ 1-6.0e-22 |               -0.00000000000000000000026057668914195110 |               -0.00000000000000000000026057668914195110 |               0.00000000000000000000000000000000000000
+ 1-3.0e-22 |               -0.00000000000000000000013028834457097555 |               -0.00000000000000000000013028834457097555 |               0.00000000000000000000000000000000000000
+ 1-9.0e-29 |        -0.000000000000000000000000000039086503371292664 |        -0.000000000000000000000000000039086503371292664 |        0.000000000000000000000000000000000000000000000
+ 1-6.0e-29 |        -0.000000000000000000000000000026057668914195110 |        -0.000000000000000000000000000026057668914195110 |        0.000000000000000000000000000000000000000000000
+ 1-3.0e-29 |        -0.000000000000000000000000000013028834457097555 |        -0.000000000000000000000000000013028834457097555 |        0.000000000000000000000000000000000000000000000
+ 1-9.0e-36 | -0.0000000000000000000000000000000000039086503371292664 | -0.0000000000000000000000000000000000039086503371292664 | 0.0000000000000000000000000000000000000000000000000000
+ 1-6.0e-36 | -0.0000000000000000000000000000000000026057668914195110 | -0.0000000000000000000000000000000000026057668914195110 | 0.0000000000000000000000000000000000000000000000000000
+ 1-3.0e-36 | -0.0000000000000000000000000000000000013028834457097555 | -0.0000000000000000000000000000000000013028834457097555 | 0.0000000000000000000000000000000000000000000000000000
+(18 rows)
+
+-- input very close to but larger than 1
+--
+-- bc(1) results computed with a scale of 500 and truncated using the script
+-- below, and then rounded by hand to match the precision of LN():
+--
+-- for p in {1..40..7} ; do for d in {9..1..3} ; do l=$(bc -ql <<< "scale=500 ; l(1+$d*10^-$p) / l(10)" | head -n 1); echo "('${d}.0e-$p', $l)," ; done ; done
+WITH t(x, bc_result) AS (VALUES
+('9.0e-1', .2787536009528290),
+('6.0e-1', .2041199826559248),
+('3.0e-1', .1139433523068368),
+('9.0e-8', .000000039086501612400118),
+('6.0e-8', .000000026057668132465074),
+('3.0e-8', .000000013028834261665042),
+('9.0e-15', .0000000000000039086503371292489),
+('6.0e-15', .0000000000000026057668914195031),
+('3.0e-15', .0000000000000013028834457097535),
+('9.0e-22', .00000000000000000000039086503371292664),
+('6.0e-22', .00000000000000000000026057668914195110),
+('3.0e-22', .00000000000000000000013028834457097555),
+('9.0e-29', .000000000000000000000000000039086503371292664),
+('6.0e-29', .000000000000000000000000000026057668914195110),
+('3.0e-29', .000000000000000000000000000013028834457097555),
+('9.0e-36', .0000000000000000000000000000000000039086503371292664),
+('6.0e-36', .0000000000000000000000000000000000026057668914195110),
+('3.0e-36', .0000000000000000000000000000000000013028834457097555))
+SELECT '1+'||x, bc_result, log(1.0+x::numeric), log(1.0+x::numeric)-bc_result AS diff FROM t;
+ ?column?  |                       bc_result                        |                          log                           |                          diff                          
+-----------+--------------------------------------------------------+--------------------------------------------------------+--------------------------------------------------------
+ 1+9.0e-1  |                                     0.2787536009528290 |                                     0.2787536009528290 |                                     0.0000000000000000
+ 1+6.0e-1  |                                     0.2041199826559248 |                                     0.2041199826559248 |                                     0.0000000000000000
+ 1+3.0e-1  |                                     0.1139433523068368 |                                     0.1139433523068368 |                                     0.0000000000000000
+ 1+9.0e-8  |                             0.000000039086501612400118 |                             0.000000039086501612400118 |                             0.000000000000000000000000
+ 1+6.0e-8  |                             0.000000026057668132465074 |                             0.000000026057668132465074 |                             0.000000000000000000000000
+ 1+3.0e-8  |                             0.000000013028834261665042 |                             0.000000013028834261665042 |                             0.000000000000000000000000
+ 1+9.0e-15 |                      0.0000000000000039086503371292489 |                      0.0000000000000039086503371292489 |                      0.0000000000000000000000000000000
+ 1+6.0e-15 |                      0.0000000000000026057668914195031 |                      0.0000000000000026057668914195031 |                      0.0000000000000000000000000000000
+ 1+3.0e-15 |                      0.0000000000000013028834457097535 |                      0.0000000000000013028834457097535 |                      0.0000000000000000000000000000000
+ 1+9.0e-22 |               0.00000000000000000000039086503371292664 |               0.00000000000000000000039086503371292664 |               0.00000000000000000000000000000000000000
+ 1+6.0e-22 |               0.00000000000000000000026057668914195110 |               0.00000000000000000000026057668914195110 |               0.00000000000000000000000000000000000000
+ 1+3.0e-22 |               0.00000000000000000000013028834457097555 |               0.00000000000000000000013028834457097555 |               0.00000000000000000000000000000000000000
+ 1+9.0e-29 |        0.000000000000000000000000000039086503371292664 |        0.000000000000000000000000000039086503371292664 |        0.000000000000000000000000000000000000000000000
+ 1+6.0e-29 |        0.000000000000000000000000000026057668914195110 |        0.000000000000000000000000000026057668914195110 |        0.000000000000000000000000000000000000000000000
+ 1+3.0e-29 |        0.000000000000000000000000000013028834457097555 |        0.000000000000000000000000000013028834457097555 |        0.000000000000000000000000000000000000000000000
+ 1+9.0e-36 | 0.0000000000000000000000000000000000039086503371292664 | 0.0000000000000000000000000000000000039086503371292664 | 0.0000000000000000000000000000000000000000000000000000
+ 1+6.0e-36 | 0.0000000000000000000000000000000000026057668914195110 | 0.0000000000000000000000000000000000026057668914195110 | 0.0000000000000000000000000000000000000000000000000000
+ 1+3.0e-36 | 0.0000000000000000000000000000000000013028834457097555 | 0.0000000000000000000000000000000000013028834457097555 | 0.0000000000000000000000000000000000000000000000000000
+(18 rows)
+
+-- input very large, exact result known
+WITH t(x) AS (SELECT '1e'||n FROM generate_series(1, 100) g(n))
+SELECT x, log(x::numeric) FROM t;
+   x   |        log         
+-------+--------------------
+ 1e1   | 1.0000000000000000
+ 1e2   | 2.0000000000000000
+ 1e3   | 3.0000000000000000
+ 1e4   | 4.0000000000000000
+ 1e5   |  5.000000000000000
+ 1e6   |  6.000000000000000
+ 1e7   |  7.000000000000000
+ 1e8   |  8.000000000000000
+ 1e9   |  9.000000000000000
+ 1e10  | 10.000000000000000
+ 1e11  | 11.000000000000000
+ 1e12  | 12.000000000000000
+ 1e13  | 13.000000000000000
+ 1e14  | 14.000000000000000
+ 1e15  | 15.000000000000000
+ 1e16  | 16.000000000000000
+ 1e17  | 17.000000000000000
+ 1e18  | 18.000000000000000
+ 1e19  | 19.000000000000000
+ 1e20  | 20.000000000000000
+ 1e21  | 21.000000000000000
+ 1e22  | 22.000000000000000
+ 1e23  | 23.000000000000000
+ 1e24  | 24.000000000000000
+ 1e25  | 25.000000000000000
+ 1e26  | 26.000000000000000
+ 1e27  | 27.000000000000000
+ 1e28  | 28.000000000000000
+ 1e29  | 29.000000000000000
+ 1e30  | 30.000000000000000
+ 1e31  | 31.000000000000000
+ 1e32  | 32.000000000000000
+ 1e33  | 33.000000000000000
+ 1e34  | 34.000000000000000
+ 1e35  | 35.000000000000000
+ 1e36  | 36.000000000000000
+ 1e37  | 37.000000000000000
+ 1e38  | 38.000000000000000
+ 1e39  | 39.000000000000000
+ 1e40  | 40.000000000000000
+ 1e41  | 41.000000000000000
+ 1e42  | 42.000000000000000
+ 1e43  | 43.000000000000000
+ 1e44  |  44.00000000000000
+ 1e45  |  45.00000000000000
+ 1e46  |  46.00000000000000
+ 1e47  |  47.00000000000000
+ 1e48  |  48.00000000000000
+ 1e49  |  49.00000000000000
+ 1e50  |  50.00000000000000
+ 1e51  |  51.00000000000000
+ 1e52  |  52.00000000000000
+ 1e53  |  53.00000000000000
+ 1e54  |  54.00000000000000
+ 1e55  |  55.00000000000000
+ 1e56  |  56.00000000000000
+ 1e57  |  57.00000000000000
+ 1e58  |  58.00000000000000
+ 1e59  |  59.00000000000000
+ 1e60  |  60.00000000000000
+ 1e61  |  61.00000000000000
+ 1e62  |  62.00000000000000
+ 1e63  |  63.00000000000000
+ 1e64  |  64.00000000000000
+ 1e65  |  65.00000000000000
+ 1e66  |  66.00000000000000
+ 1e67  |  67.00000000000000
+ 1e68  |  68.00000000000000
+ 1e69  |  69.00000000000000
+ 1e70  |  70.00000000000000
+ 1e71  |  71.00000000000000
+ 1e72  |  72.00000000000000
+ 1e73  |  73.00000000000000
+ 1e74  |  74.00000000000000
+ 1e75  |  75.00000000000000
+ 1e76  |  76.00000000000000
+ 1e77  |  77.00000000000000
+ 1e78  |  78.00000000000000
+ 1e79  |  79.00000000000000
+ 1e80  |  80.00000000000000
+ 1e81  |  81.00000000000000
+ 1e82  |  82.00000000000000
+ 1e83  |  83.00000000000000
+ 1e84  |  84.00000000000000
+ 1e85  |  85.00000000000000
+ 1e86  |  86.00000000000000
+ 1e87  |  87.00000000000000
+ 1e88  |  88.00000000000000
+ 1e89  |  89.00000000000000
+ 1e90  |  90.00000000000000
+ 1e91  |  91.00000000000000
+ 1e92  |  92.00000000000000
+ 1e93  |  93.00000000000000
+ 1e94  |  94.00000000000000
+ 1e95  |  95.00000000000000
+ 1e96  |  96.00000000000000
+ 1e97  |  97.00000000000000
+ 1e98  |  98.00000000000000
+ 1e99  |  99.00000000000000
+ 1e100 | 100.00000000000000
+(100 rows)
+
+-- input very large, non-exact results
+--
+-- bc(1) results computed with a scale of 500 and truncated using the script
+-- below, and then rounded by hand to match the precision of LN():
+--
+-- for p in {10..50..7} ; do for d in {2..9..3} ; do l=$(bc -ql <<< "scale=500 ; l($d*10^$p) / l(10)" | head -n 1); echo "('${d}.0e$p', $l)," ; done ; done
+WITH t(x, bc_result) AS (VALUES
+('2.0e10', 10.301029995663981),
+('5.0e10', 10.698970004336019),
+('8.0e10', 10.903089986991944),
+('2.0e17', 17.301029995663981),
+('5.0e17', 17.698970004336019),
+('8.0e17', 17.903089986991944),
+('2.0e24', 24.301029995663981),
+('5.0e24', 24.698970004336019),
+('8.0e24', 24.903089986991944),
+('2.0e31', 31.301029995663981),
+('5.0e31', 31.698970004336019),
+('8.0e31', 31.903089986991944),
+('2.0e38', 38.301029995663981),
+('5.0e38', 38.698970004336019),
+('8.0e38', 38.903089986991944),
+('2.0e45', 45.30102999566398),
+('5.0e45', 45.69897000433602),
+('8.0e45', 45.90308998699194))
+SELECT x, bc_result, log(x::numeric), log(x::numeric)-bc_result AS diff FROM t;
+   x    |     bc_result      |        log         |       diff        
+--------+--------------------+--------------------+-------------------
+ 2.0e10 | 10.301029995663981 | 10.301029995663981 | 0.000000000000000
+ 5.0e10 | 10.698970004336019 | 10.698970004336019 | 0.000000000000000
+ 8.0e10 | 10.903089986991944 | 10.903089986991944 | 0.000000000000000
+ 2.0e17 | 17.301029995663981 | 17.301029995663981 | 0.000000000000000
+ 5.0e17 | 17.698970004336019 | 17.698970004336019 | 0.000000000000000
+ 8.0e17 | 17.903089986991944 | 17.903089986991944 | 0.000000000000000
+ 2.0e24 | 24.301029995663981 | 24.301029995663981 | 0.000000000000000
+ 5.0e24 | 24.698970004336019 | 24.698970004336019 | 0.000000000000000
+ 8.0e24 | 24.903089986991944 | 24.903089986991944 | 0.000000000000000
+ 2.0e31 | 31.301029995663981 | 31.301029995663981 | 0.000000000000000
+ 5.0e31 | 31.698970004336019 | 31.698970004336019 | 0.000000000000000
+ 8.0e31 | 31.903089986991944 | 31.903089986991944 | 0.000000000000000
+ 2.0e38 | 38.301029995663981 | 38.301029995663981 | 0.000000000000000
+ 5.0e38 | 38.698970004336019 | 38.698970004336019 | 0.000000000000000
+ 8.0e38 | 38.903089986991944 | 38.903089986991944 | 0.000000000000000
+ 2.0e45 |  45.30102999566398 |  45.30102999566398 |  0.00000000000000
+ 5.0e45 |  45.69897000433602 |  45.69897000433602 |  0.00000000000000
+ 8.0e45 |  45.90308998699194 |  45.90308998699194 |  0.00000000000000
+(18 rows)
+
+--
+-- Tests for LOG() (arbitrary base)
+--
+-- invalid inputs
+select log(-12.34, 56.78);
+ERROR:  cannot take logarithm of a negative number
+select log(-12.34, -56.78);
+ERROR:  cannot take logarithm of a negative number
+select log(12.34, -56.78);
+ERROR:  cannot take logarithm of a negative number
+select log(0.0, 12.34);
+ERROR:  cannot take logarithm of zero
+select log(12.34, 0.0);
+ERROR:  cannot take logarithm of zero
+select log(1.0, 12.34);
+ERROR:  division by zero
+-- some random tests
+select log(1.23e-89, 6.4689e45);
+                                              log                                               
+------------------------------------------------------------------------------------------------
+ -0.5152489207781856983977054971756484879653568168479201885425588841094788842469115325262329756
+(1 row)
+
+select log(0.99923, 4.58934e34);
+         log         
+---------------------
+ -103611.55579544132
+(1 row)
+
+select log(1.000016, 8.452010e18);
+        log         
+--------------------
+ 2723830.2877097365
+(1 row)
+
+select log(3.1954752e47, 9.4792021e-73);
+                                         log                                         
+-------------------------------------------------------------------------------------
+ -1.51613372350688302142917386143459361608600157692779164475351842333265418126982165
+(1 row)
+
diff --git a/src/test/regress/sql/numeric.sql b/src/test/regress/sql/numeric.sql
new file mode 100644
index 982287c..4c90505
--- a/src/test/regress/sql/numeric.sql
+++ b/src/test/regress/sql/numeric.sql
@@ -848,6 +848,57 @@ select 10.0 ^ -2147483647 as rounds_to_z
 select 10.0 ^ 2147483647 as overflows;
 select 117743296169.0 ^ 1000000000 as overflows;
 
+-- cases that used to return inaccurate results
+select 3.789 ^ 21;
+select 3.789 ^ 35;
+select 1.2 ^ 345;
+select 0.12 ^ (-20);
+
+-- cases that used to error out
+select 0.12 ^ (-25);
+select 0.5678 ^ (-85);
+
+--
+-- Tests for raising to non-integer powers
+--
+
+-- special cases
+select 0.0 ^ 0.0;
+select (-12.34) ^ 0.0;
+select 12.34 ^ 0.0;
+select 0.0 ^ 12.34;
+
+-- invalid inputs
+select 0.0 ^ (-12.34);
+select (-12.34) ^ 1.2;
+
+-- cases that used to generate inaccurate results
+select 32.1 ^ 9.8;
+select 32.1 ^ (-9.8);
+select 12.3 ^ 45.6;
+select 12.3 ^ (-45.6);
+
+-- big test
+select 1.234 ^ 5678;
+
+--
+-- Tests for EXP()
+--
+
+-- special cases
+select exp(0.0);
+select exp(1.0);
+select exp(1.0::numeric(71,70));
+
+-- cases that used to generate inaccurate results
+select exp(32.999);
+select exp(-32.999);
+select exp(123.456);
+select exp(-123.456);
+
+-- big test
+select exp(1234.5678);
+
 --
 -- Tests for generate_series
 --
@@ -868,3 +919,392 @@ select (i / (10::numeric ^ 131071))::num
 select * from generate_series(1::numeric, 3::numeric) i, generate_series(i,3) j;
 select * from generate_series(1::numeric, 3::numeric) i, generate_series(1,i) j;
 select * from generate_series(1::numeric, 3::numeric) i, generate_series(1,5,i) j;
+
+--
+-- Tests for LN()
+--
+
+-- Invalid inputs
+select ln(-12.34);
+select ln(0.0);
+
+-- Some random tests
+select ln(1.2345678e-28);
+select ln(0.0456789);
+select ln(0.349873948359354029493948309745709580730482050975);
+select ln(0.99949452);
+select ln(1.00049687395);
+select ln(1234.567890123456789);
+select ln(5.80397490724e5);
+select ln(9.342536355e34);
+
+-- input very small
+--
+-- bc(1) results computed with a scale of 500 and truncated using the script
+-- below, and then rounded by hand to match the precision of LN():
+--
+-- for p in {1..40} ; do l=$(bc -ql <<< "scale=500 ; l(10^-$p)" | head -n 1); echo "('1.0e-$p', $l)," ; done
+WITH t(x, bc_result) AS (VALUES
+('1.0e-1', -2.3025850929940457),
+('1.0e-2', -4.6051701859880914),
+('1.0e-3', -6.9077552789821371),
+('1.0e-4', -9.2103403719761827),
+('1.0e-5', -11.512925464970228),
+('1.0e-6', -13.815510557964274),
+('1.0e-7', -16.118095650958320),
+('1.0e-8', -18.420680743952365),
+('1.0e-9', -20.723265836946411),
+('1.0e-10', -23.025850929940457),
+('1.0e-11', -25.328436022934503),
+('1.0e-12', -27.631021115928548),
+('1.0e-13', -29.933606208922594),
+('1.0e-14', -32.236191301916640),
+('1.0e-15', -34.5387763949106853),
+('1.0e-16', -36.84136148790473094),
+('1.0e-17', -39.143946580898776628),
+('1.0e-18', -41.4465316738928223123),
+('1.0e-19', -43.74911676688686799634),
+('1.0e-20', -46.051701859880913680360),
+('1.0e-21', -48.3542869528749593643778),
+('1.0e-22', -50.65687204586900504839581),
+('1.0e-23', -52.959457138863050732413803),
+('1.0e-24', -55.2620422318570964164317949),
+('1.0e-25', -57.56462732485114210044978637),
+('1.0e-26', -59.867212417845187784467777822),
+('1.0e-27', -62.1697975108392334684857692765),
+('1.0e-28', -64.47238260383327915250376073116),
+('1.0e-29', -66.774967696827324836521752185847),
+('1.0e-30', -69.0775527898213705205397436405309),
+('1.0e-31', -71.38013788281541620455773509521529),
+('1.0e-32', -73.682722975809461888575726549899655),
+('1.0e-33', -75.9853080688035075725937180045840189),
+('1.0e-34', -78.28789316179755325661170945926838306),
+('1.0e-35', -80.590478254791598940629700913952747266),
+('1.0e-36', -82.8930633477856446246476923686371114736),
+('1.0e-37', -85.19564844077969030866568382332147568124),
+('1.0e-38', -87.498233533773735992683675278005839888842),
+('1.0e-39', -89.8008186267677816767016667326902040964430),
+('1.0e-40', -92.10340371976182736071965818737456830404406))
+SELECT x, bc_result, ln(x::numeric), ln(x::numeric)-bc_result AS diff FROM t;
+
+-- input very close to but smaller than 1
+--
+-- bc(1) results computed with a scale of 500 and truncated using the script
+-- below, and then rounded by hand to match the precision of LN():
+--
+-- for p in {1..40} ; do l=$(bc -ql <<< "scale=500 ; l(1-10^-$p)" | head -n 1); echo "('1.0e-$p', $l)," ; done
+WITH t(x, bc_result) AS (VALUES
+('1.0e-1', -.10536051565782630),
+('1.0e-2', -.010050335853501441),
+('1.0e-3', -.0010005003335835335),
+('1.0e-4', -.00010000500033335834),
+('1.0e-5', -.000010000050000333336),
+('1.0e-6', -.0000010000005000003333),
+('1.0e-7', -.00000010000000500000033),
+('1.0e-8', -.000000010000000050000000),
+('1.0e-9', -.0000000010000000005000000),
+('1.0e-10', -.00000000010000000000500000),
+('1.0e-11', -.000000000010000000000050000),
+('1.0e-12', -.0000000000010000000000005000),
+('1.0e-13', -.00000000000010000000000000500),
+('1.0e-14', -.000000000000010000000000000050),
+('1.0e-15', -.0000000000000010000000000000005),
+('1.0e-16', -.00000000000000010000000000000001),
+('1.0e-17', -.000000000000000010000000000000000),
+('1.0e-18', -.0000000000000000010000000000000000),
+('1.0e-19', -.00000000000000000010000000000000000),
+('1.0e-20', -.000000000000000000010000000000000000),
+('1.0e-21', -.0000000000000000000010000000000000000),
+('1.0e-22', -.00000000000000000000010000000000000000),
+('1.0e-23', -.000000000000000000000010000000000000000),
+('1.0e-24', -.0000000000000000000000010000000000000000),
+('1.0e-25', -.00000000000000000000000010000000000000000),
+('1.0e-26', -.000000000000000000000000010000000000000000),
+('1.0e-27', -.0000000000000000000000000010000000000000000),
+('1.0e-28', -.00000000000000000000000000010000000000000000),
+('1.0e-29', -.000000000000000000000000000010000000000000000),
+('1.0e-30', -.0000000000000000000000000000010000000000000000),
+('1.0e-31', -.00000000000000000000000000000010000000000000000),
+('1.0e-32', -.000000000000000000000000000000010000000000000000),
+('1.0e-33', -.0000000000000000000000000000000010000000000000000),
+('1.0e-34', -.00000000000000000000000000000000010000000000000000),
+('1.0e-35', -.000000000000000000000000000000000010000000000000000),
+('1.0e-36', -.0000000000000000000000000000000000010000000000000000),
+('1.0e-37', -.00000000000000000000000000000000000010000000000000000),
+('1.0e-38', -.000000000000000000000000000000000000010000000000000000),
+('1.0e-39', -.0000000000000000000000000000000000000010000000000000000),
+('1.0e-40', -.00000000000000000000000000000000000000010000000000000000))
+SELECT '1-'||x, bc_result, ln(1.0-x::numeric), ln(1.0-x::numeric)-bc_result AS diff FROM t;
+
+-- input very close to but larger than 1
+--
+-- bc(1) results computed with a scale of 500 and truncated using the script
+-- below, and then rounded by hand to match the precision of LN():
+--
+-- for p in {1..40} ; do l=$(bc -ql <<< "scale=500 ; l(1+10^-$p)" | head -n 1); echo "('1.0e-$p', $l)," ; done
+WITH t(x, bc_result) AS (VALUES
+('1.0e-1', .09531017980432486),
+('1.0e-2', .009950330853168083),
+('1.0e-3', .0009995003330835332),
+('1.0e-4', .00009999500033330834),
+('1.0e-5', .000009999950000333331),
+('1.0e-6', .0000009999995000003333),
+('1.0e-7', .00000009999999500000033),
+('1.0e-8', .000000009999999950000000),
+('1.0e-9', .0000000009999999995000000),
+('1.0e-10', .00000000009999999999500000),
+('1.0e-11', .000000000009999999999950000),
+('1.0e-12', .0000000000009999999999995000),
+('1.0e-13', .00000000000009999999999999500),
+('1.0e-14', .000000000000009999999999999950),
+('1.0e-15', .0000000000000009999999999999995),
+('1.0e-16', .00000000000000010000000000000000),
+('1.0e-17', .000000000000000010000000000000000),
+('1.0e-18', .0000000000000000010000000000000000),
+('1.0e-19', .00000000000000000010000000000000000),
+('1.0e-20', .000000000000000000010000000000000000),
+('1.0e-21', .0000000000000000000010000000000000000),
+('1.0e-22', .00000000000000000000010000000000000000),
+('1.0e-23', .000000000000000000000010000000000000000),
+('1.0e-24', .0000000000000000000000010000000000000000),
+('1.0e-25', .00000000000000000000000010000000000000000),
+('1.0e-26', .000000000000000000000000010000000000000000),
+('1.0e-27', .0000000000000000000000000010000000000000000),
+('1.0e-28', .00000000000000000000000000010000000000000000),
+('1.0e-29', .000000000000000000000000000010000000000000000),
+('1.0e-30', .0000000000000000000000000000010000000000000000),
+('1.0e-31', .00000000000000000000000000000010000000000000000),
+('1.0e-32', .000000000000000000000000000000010000000000000000),
+('1.0e-33', .0000000000000000000000000000000010000000000000000),
+('1.0e-34', .00000000000000000000000000000000010000000000000000),
+('1.0e-35', .000000000000000000000000000000000010000000000000000),
+('1.0e-36', .0000000000000000000000000000000000010000000000000000),
+('1.0e-37', .00000000000000000000000000000000000010000000000000000),
+('1.0e-38', .000000000000000000000000000000000000010000000000000000),
+('1.0e-39', .0000000000000000000000000000000000000010000000000000000),
+('1.0e-40', .00000000000000000000000000000000000000010000000000000000))
+SELECT '1+'||x, bc_result, ln(1.0+x::numeric), ln(1.0+x::numeric)-bc_result AS diff FROM t;
+
+-- input very large
+--
+-- bc(1) results computed with a scale of 500 and truncated using the script
+-- below, and then rounded by hand to match the precision of LN():
+--
+-- for p in {1..40} ; do l=$(bc -ql <<< "scale=500 ; l(10^$p)" | head -n 1); echo "('1.0e$p', $l)," ; done
+WITH t(x, bc_result) AS (VALUES
+('1.0e1', 2.3025850929940457),
+('1.0e2', 4.6051701859880914),
+('1.0e3', 6.9077552789821371),
+('1.0e4', 9.2103403719761827),
+('1.0e5', 11.512925464970228),
+('1.0e6', 13.815510557964274),
+('1.0e7', 16.118095650958320),
+('1.0e8', 18.420680743952365),
+('1.0e9', 20.723265836946411),
+('1.0e10', 23.025850929940457),
+('1.0e11', 25.328436022934503),
+('1.0e12', 27.631021115928548),
+('1.0e13', 29.933606208922594),
+('1.0e14', 32.236191301916640),
+('1.0e15', 34.538776394910685),
+('1.0e16', 36.841361487904731),
+('1.0e17', 39.143946580898777),
+('1.0e18', 41.446531673892822),
+('1.0e19', 43.749116766886868),
+('1.0e20', 46.051701859880914),
+('1.0e21', 48.354286952874959),
+('1.0e22', 50.656872045869005),
+('1.0e23', 52.959457138863051),
+('1.0e24', 55.262042231857096),
+('1.0e25', 57.564627324851142),
+('1.0e26', 59.867212417845188),
+('1.0e27', 62.169797510839233),
+('1.0e28', 64.472382603833279),
+('1.0e29', 66.774967696827325),
+('1.0e30', 69.077552789821371),
+('1.0e31', 71.380137882815416),
+('1.0e32', 73.682722975809462),
+('1.0e33', 75.985308068803508),
+('1.0e34', 78.287893161797553),
+('1.0e35', 80.590478254791599),
+('1.0e36', 82.893063347785645),
+('1.0e37', 85.195648440779690),
+('1.0e38', 87.498233533773736),
+('1.0e39', 89.800818626767782),
+('1.0e40', 92.103403719761827))
+SELECT x, bc_result, ln(x::numeric), ln(x::numeric)-bc_result AS diff FROM t;
+
+-- input huge
+--
+-- bc(1) results computed with a scale of 1000 and truncated using the script
+-- below, and then rounded by hand to match the precision of LN():
+--
+-- for p in {1..10} ; do l=$(bc -ql <<< "scale=1000 ; l(10^${p}00)" | head -n 1); echo "('1.0e${p}00', $l)," ; done
+WITH t(x, bc_result) AS (VALUES
+('1.0e100', 230.25850929940457),
+('1.0e200', 460.51701859880914),
+('1.0e300', 690.77552789821371),
+('1.0e400', 921.03403719761827),
+('1.0e500', 1151.2925464970228),
+('1.0e600', 1381.5510557964274),
+('1.0e700', 1611.8095650958320),
+('1.0e800', 1842.0680743952365),
+('1.0e900', 2072.3265836946411),
+('1.0e1000', 2302.5850929940457))
+SELECT x, bc_result, ln(x::numeric), ln(x::numeric)-bc_result AS diff FROM t;
+
+--
+-- Tests for LOG() (base 10)
+--
+
+-- invalid inputs
+select log(-12.34);
+select log(0);
+
+-- some random tests
+select log(1.234567e-89);
+select log(3.4634998359873254962349856073435545);
+select log(9.999999999999999999);
+select log(10.00000000000000000);
+select log(10.00000000000000001);
+select log(590489.45235237);
+
+-- input very small, exact result known
+WITH t(x) AS (SELECT '1e-'||n FROM generate_series(1, 100) g(n))
+SELECT x, log(x::numeric) FROM t;
+
+-- input very small, non-exact results
+--
+-- bc(1) results computed with a scale of 500 and truncated using the script
+-- below, and then rounded by hand to match the precision of LN():
+--
+-- for p in {1..50..7} ; do for d in {9..1..3} ; do l=$(bc -ql <<< "scale=500 ; l($d*10^-$p) / l(10)" | head -n 1); echo "('${d}.0e-$p', $l)," ; done ; done
+WITH t(x, bc_result) AS (VALUES
+('9.0e-1', -.04575749056067513),
+('6.0e-1', -.2218487496163564),
+('3.0e-1', -.5228787452803376),
+('9.0e-8', -7.045757490560675),
+('6.0e-8', -7.221848749616356),
+('3.0e-8', -7.522878745280338),
+('9.0e-15', -14.0457574905606751),
+('6.0e-15', -14.2218487496163564),
+('3.0e-15', -14.5228787452803376),
+('9.0e-22', -21.04575749056067512540994),
+('6.0e-22', -21.22184874961635636749123),
+('3.0e-22', -21.52287874528033756270497),
+('9.0e-29', -28.045757490560675125409944193490),
+('6.0e-29', -28.221848749616356367491233202020),
+('3.0e-29', -28.522878745280337562704972096745),
+('9.0e-36', -35.0457574905606751254099441934897693816),
+('6.0e-36', -35.2218487496163563674912332020203916640),
+('3.0e-36', -35.5228787452803375627049720967448846908),
+('9.0e-43', -42.04575749056067512540994419348976938159974227),
+('6.0e-43', -42.22184874961635636749123320202039166403168125),
+('3.0e-43', -42.52287874528033756270497209674488469079987114),
+('9.0e-50', -49.045757490560675125409944193489769381599742271618608),
+('6.0e-50', -49.221848749616356367491233202020391664031681254347196),
+('3.0e-50', -49.522878745280337562704972096744884690799871135809304))
+SELECT x, bc_result, log(x::numeric), log(x::numeric)-bc_result AS diff FROM t;
+
+-- input very close to but smaller than 1
+--
+-- bc(1) results computed with a scale of 500 and truncated using the script
+-- below, and then rounded by hand to match the precision of LN():
+--
+-- for p in {1..40..7} ; do for d in {9..1..3} ; do l=$(bc -ql <<< "scale=500 ; l(1-$d*10^-$p) / l(10)" | head -n 1); echo "('${d}.0e-$p', $l)," ; done ; done
+WITH t(x, bc_result) AS (VALUES
+('9.0e-1', -1.0000000000000000),
+('6.0e-1', -.3979400086720376),
+('3.0e-1', -.1549019599857432),
+('9.0e-8', -.000000039086505130185422),
+('6.0e-8', -.000000026057669695925208),
+('3.0e-8', -.000000013028834652530076),
+('9.0e-15', -.0000000000000039086503371292840),
+('6.0e-15', -.0000000000000026057668914195188),
+('3.0e-15', -.0000000000000013028834457097574),
+('9.0e-22', -.00000000000000000000039086503371292664),
+('6.0e-22', -.00000000000000000000026057668914195110),
+('3.0e-22', -.00000000000000000000013028834457097555),
+('9.0e-29', -.000000000000000000000000000039086503371292664),
+('6.0e-29', -.000000000000000000000000000026057668914195110),
+('3.0e-29', -.000000000000000000000000000013028834457097555),
+('9.0e-36', -.0000000000000000000000000000000000039086503371292664),
+('6.0e-36', -.0000000000000000000000000000000000026057668914195110),
+('3.0e-36', -.0000000000000000000000000000000000013028834457097555))
+SELECT '1-'||x, bc_result, log(1.0-x::numeric), log(1.0-x::numeric)-bc_result AS diff FROM t;
+
+-- input very close to but larger than 1
+--
+-- bc(1) results computed with a scale of 500 and truncated using the script
+-- below, and then rounded by hand to match the precision of LN():
+--
+-- for p in {1..40..7} ; do for d in {9..1..3} ; do l=$(bc -ql <<< "scale=500 ; l(1+$d*10^-$p) / l(10)" | head -n 1); echo "('${d}.0e-$p', $l)," ; done ; done
+WITH t(x, bc_result) AS (VALUES
+('9.0e-1', .2787536009528290),
+('6.0e-1', .2041199826559248),
+('3.0e-1', .1139433523068368),
+('9.0e-8', .000000039086501612400118),
+('6.0e-8', .000000026057668132465074),
+('3.0e-8', .000000013028834261665042),
+('9.0e-15', .0000000000000039086503371292489),
+('6.0e-15', .0000000000000026057668914195031),
+('3.0e-15', .0000000000000013028834457097535),
+('9.0e-22', .00000000000000000000039086503371292664),
+('6.0e-22', .00000000000000000000026057668914195110),
+('3.0e-22', .00000000000000000000013028834457097555),
+('9.0e-29', .000000000000000000000000000039086503371292664),
+('6.0e-29', .000000000000000000000000000026057668914195110),
+('3.0e-29', .000000000000000000000000000013028834457097555),
+('9.0e-36', .0000000000000000000000000000000000039086503371292664),
+('6.0e-36', .0000000000000000000000000000000000026057668914195110),
+('3.0e-36', .0000000000000000000000000000000000013028834457097555))
+SELECT '1+'||x, bc_result, log(1.0+x::numeric), log(1.0+x::numeric)-bc_result AS diff FROM t;
+
+-- input very large, exact result known
+WITH t(x) AS (SELECT '1e'||n FROM generate_series(1, 100) g(n))
+SELECT x, log(x::numeric) FROM t;
+
+-- input very large, non-exact results
+--
+-- bc(1) results computed with a scale of 500 and truncated using the script
+-- below, and then rounded by hand to match the precision of LN():
+--
+-- for p in {10..50..7} ; do for d in {2..9..3} ; do l=$(bc -ql <<< "scale=500 ; l($d*10^$p) / l(10)" | head -n 1); echo "('${d}.0e$p', $l)," ; done ; done
+WITH t(x, bc_result) AS (VALUES
+('2.0e10', 10.301029995663981),
+('5.0e10', 10.698970004336019),
+('8.0e10', 10.903089986991944),
+('2.0e17', 17.301029995663981),
+('5.0e17', 17.698970004336019),
+('8.0e17', 17.903089986991944),
+('2.0e24', 24.301029995663981),
+('5.0e24', 24.698970004336019),
+('8.0e24', 24.903089986991944),
+('2.0e31', 31.301029995663981),
+('5.0e31', 31.698970004336019),
+('8.0e31', 31.903089986991944),
+('2.0e38', 38.301029995663981),
+('5.0e38', 38.698970004336019),
+('8.0e38', 38.903089986991944),
+('2.0e45', 45.30102999566398),
+('5.0e45', 45.69897000433602),
+('8.0e45', 45.90308998699194))
+SELECT x, bc_result, log(x::numeric), log(x::numeric)-bc_result AS diff FROM t;
+
+--
+-- Tests for LOG() (arbitrary base)
+--
+
+-- invalid inputs
+select log(-12.34, 56.78);
+select log(-12.34, -56.78);
+select log(12.34, -56.78);
+select log(0.0, 12.34);
+select log(12.34, 0.0);
+select log(1.0, 12.34);
+
+-- some random tests
+select log(1.23e-89, 6.4689e45);
+select log(0.99923, 4.58934e34);
+select log(1.000016, 8.452010e18);
+select log(3.1954752e47, 9.4792021e-73);
