aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/numeric.c
diff options
context:
space:
mode:
authorNeil Conway <neilc@samurai.com>2004-05-16 23:18:55 +0000
committerNeil Conway <neilc@samurai.com>2004-05-16 23:18:55 +0000
commit2871f60f232a5a5b496f3d75156bfeb679c3d161 (patch)
treeec553ac498df2de5313f5775d34f0e627eee118a /src/backend/utils/adt/numeric.c
parent335cf9ae7f2c10d8ce77247fd8c2f9cd123f1f16 (diff)
downloadpostgresql-2871f60f232a5a5b496f3d75156bfeb679c3d161.tar.gz
postgresql-2871f60f232a5a5b496f3d75156bfeb679c3d161.zip
Change ln(), log(), power(), and sqrt() to emit the correct SQLSTATE
error codes for certain error conditions, as specified by SQL2003.
Diffstat (limited to 'src/backend/utils/adt/numeric.c')
-rw-r--r--src/backend/utils/adt/numeric.c30
1 files changed, 27 insertions, 3 deletions
diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c
index 4214d7af007..9307ba5e6ec 100644
--- a/src/backend/utils/adt/numeric.c
+++ b/src/backend/utils/adt/numeric.c
@@ -14,7 +14,7 @@
* Copyright (c) 1998-2003, PostgreSQL Global Development Group
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.74 2004/05/14 21:42:28 neilc Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.75 2004/05/16 23:18:55 neilc Exp $
*
*-------------------------------------------------------------------------
*/
@@ -1668,6 +1668,7 @@ numeric_power(PG_FUNCTION_ARGS)
Numeric res;
NumericVar arg1;
NumericVar arg2;
+ NumericVar arg2_trunc;
NumericVar result;
/*
@@ -1681,10 +1682,26 @@ numeric_power(PG_FUNCTION_ARGS)
*/
init_var(&arg1);
init_var(&arg2);
+ init_var(&arg2_trunc);
init_var(&result);
set_var_from_num(num1, &arg1);
set_var_from_num(num2, &arg2);
+ set_var_from_var(&arg2, &arg2_trunc);
+
+ trunc_var(&arg2_trunc, 0);
+
+ /*
+ * Return special SQLSTATE error codes for a few conditions
+ * mandated by the standard.
+ */
+ if ((cmp_var(&arg1, &const_zero) == 0 &&
+ cmp_var(&arg2, &const_zero) < 0) ||
+ (cmp_var(&arg1, &const_zero) < 0 &&
+ cmp_var(&arg2, &arg2_trunc) != 0))
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
+ errmsg("invalid argument for power function")));
/*
* Call power_var() to compute and return the result; note it handles
@@ -1696,6 +1713,7 @@ numeric_power(PG_FUNCTION_ARGS)
free_var(&result);
free_var(&arg2);
+ free_var(&arg2_trunc);
free_var(&arg1);
PG_RETURN_NUMERIC(res);
@@ -4408,10 +4426,16 @@ ln_var(NumericVar *arg, NumericVar *result, int rscale)
NumericVar elem;
NumericVar fact;
int local_rscale;
+ int cmp;
- if (cmp_var(arg, &const_zero) <= 0)
+ cmp = cmp_var(arg, &const_zero);
+ if (cmp == 0)
ereport(ERROR,
- (errcode(ERRCODE_FLOATING_POINT_EXCEPTION),
+ (errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
+ errmsg("cannot take logarithm of zero")));
+ else if (cmp < 0)
+ ereport(ERROR,
+ (errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
errmsg("cannot take logarithm of a negative number")));
local_rscale = rscale + 8;