summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Momjian2002-10-19 02:08:19 +0000
committerBruce Momjian2002-10-19 02:08:19 +0000
commitbab3d29fbab48e803e9dec09733a388aa1bbba22 (patch)
tree2ddcdc3283080b626a1012e5edbfd1a895118fe3
parent5c6a5fe18b2e8d1b4ec128c0441aca6d13d44121 (diff)
This patch adds some missing functions for float8 math operations,
specifically ceil(), floor(), and sign(). There may be other functions that need to be added, but this is a start. I've included some simple regression tests. Neil Conway
-rw-r--r--src/backend/utils/adt/float.c65
-rw-r--r--src/backend/utils/adt/numeric.c10
-rw-r--r--src/include/catalog/catversion.h4
-rw-r--r--src/include/catalog/pg_proc.h8
-rw-r--r--src/include/utils/builtins.h5
-rw-r--r--src/test/regress/expected/float8.out35
-rw-r--r--src/test/regress/sql/float8.sql11
7 files changed, 119 insertions, 19 deletions
diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c
index 7b1798d75ca..fa34e105c15 100644
--- a/src/backend/utils/adt/float.c
+++ b/src/backend/utils/adt/float.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/float.c,v 1.81 2002/09/04 20:31:27 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/float.c,v 1.82 2002/10/19 02:08:17 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -129,10 +129,10 @@ static void CheckFloat8Val(double val);
/*
- check to see if a float4 val is outside of
- the FLOAT4_MIN, FLOAT4_MAX bounds.
-
- raise an elog warning if it is
+ * check to see if a float4 val is outside of
+ * the FLOAT4_MIN, FLOAT4_MAX bounds.
+ *
+ * raise an elog warning if it is
*/
static void
CheckFloat4Val(double val)
@@ -153,11 +153,11 @@ CheckFloat4Val(double val)
}
/*
- check to see if a float8 val is outside of
- the FLOAT8_MIN, FLOAT8_MAX bounds.
-
- raise an elog warning if it is
-*/
+ * check to see if a float8 val is outside of
+ * the FLOAT8_MIN, FLOAT8_MAX bounds.
+ *
+ * raise an elog error if it is
+ */
static void
CheckFloat8Val(double val)
{
@@ -172,7 +172,6 @@ CheckFloat8Val(double val)
elog(ERROR, "Bad float8 input format -- overflow");
if (val != 0.0 && fabs(val) < FLOAT8_MIN)
elog(ERROR, "Bad float8 input format -- underflow");
- return;
#endif /* UNSAFE_FLOATS */
}
@@ -1039,6 +1038,50 @@ dround(PG_FUNCTION_ARGS)
PG_RETURN_FLOAT8(result);
}
+/*
+ * dceil - returns the smallest integer greater than or
+ * equal to the specified float
+ */
+Datum
+dceil(PG_FUNCTION_ARGS)
+{
+ float8 arg1 = PG_GETARG_FLOAT8(0);
+
+ PG_RETURN_FLOAT8(ceil(arg1));
+}
+
+/*
+ * dfloor - returns the largest integer lesser than or
+ * equal to the specified float
+ */
+Datum
+dfloor(PG_FUNCTION_ARGS)
+{
+ float8 arg1 = PG_GETARG_FLOAT8(0);
+
+ PG_RETURN_FLOAT8(floor(arg1));
+}
+
+/*
+ * dsign - returns -1 if the argument is less than 0, 0
+ * if the argument is equal to 0, and 1 if the
+ * argument is greater than zero.
+ */
+Datum
+dsign(PG_FUNCTION_ARGS)
+{
+ float8 arg1 = PG_GETARG_FLOAT8(0);
+ float8 result;
+
+ if (arg1 > 0)
+ result = 1.0;
+ else if (arg1 < 0)
+ result = -1.0;
+ else
+ result = 0.0;
+
+ PG_RETURN_FLOAT8(result);
+}
/*
* dtrunc - returns truncation-towards-zero of arg1,
diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c
index 7f32e2e6243..f1967019d53 100644
--- a/src/backend/utils/adt/numeric.c
+++ b/src/backend/utils/adt/numeric.c
@@ -5,7 +5,7 @@
*
* 1998 Jan Wieck
*
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/numeric.c,v 1.55 2002/10/02 19:21:26 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/numeric.c,v 1.56 2002/10/19 02:08:17 momjian Exp $
*
* ----------
*/
@@ -425,7 +425,13 @@ numeric_uplus(PG_FUNCTION_ARGS)
PG_RETURN_NUMERIC(res);
}
-
+/* ----------
+ * numeric_sign() -
+ *
+ * returns -1 if the argument is less than 0, 0 if the argument is equal
+ * to 0, and 1 if the argument is greater than zero.
+ * ----------
+ */
Datum
numeric_sign(PG_FUNCTION_ARGS)
{
diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h
index 967cb794f7d..58df9af0abf 100644
--- a/src/include/catalog/catversion.h
+++ b/src/include/catalog/catversion.h
@@ -37,7 +37,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: catversion.h,v 1.161 2002/10/14 22:14:35 tgl Exp $
+ * $Id: catversion.h,v 1.162 2002/10/19 02:08:18 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -53,6 +53,6 @@
*/
/* yyyymmddN */
-#define CATALOG_VERSION_NO 200210141
+#define CATALOG_VERSION_NO 200210181
#endif
diff --git a/src/include/catalog/pg_proc.h b/src/include/catalog/pg_proc.h
index 369da463e8a..95197811ad8 100644
--- a/src/include/catalog/pg_proc.h
+++ b/src/include/catalog/pg_proc.h
@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: pg_proc.h,v 1.273 2002/09/22 17:27:23 tgl Exp $
+ * $Id: pg_proc.h,v 1.274 2002/10/19 02:08:18 momjian Exp $
*
* NOTES
* The script catalog/genbki.sh reads this file and generates .bki
@@ -473,6 +473,12 @@ DATA(insert OID = 228 ( dround PGNSP PGUID 12 f f t f i 1 701 "701" droun
DESCR("round to nearest integer");
DATA(insert OID = 229 ( dtrunc PGNSP PGUID 12 f f t f i 1 701 "701" dtrunc - _null_ ));
DESCR("truncate to integer");
+DATA(insert OID = 2308 ( ceil PGNSP PGUID 12 f f t f i 1 701 "701" dceil - _null_ ));
+DESCR("smallest integer >= value");
+DATA(insert OID = 2309 ( floor PGNSP PGUID 12 f f t f i 1 701 "701" dfloor - _null_ ));
+DESCR("largest integer <= value");
+DATA(insert OID = 2310 ( sign PGNSP PGUID 12 f f t f i 1 701 "701" dsign - _null_ ));
+DESCR("sign of value");
DATA(insert OID = 230 ( dsqrt PGNSP PGUID 12 f f t f i 1 701 "701" dsqrt - _null_ ));
DESCR("square root");
DATA(insert OID = 231 ( dcbrt PGNSP PGUID 12 f f t f i 1 701 "701" dcbrt - _null_ ));
diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h
index 9f1c9ba8dbf..990c9f36be8 100644
--- a/src/include/utils/builtins.h
+++ b/src/include/utils/builtins.h
@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: builtins.h,v 1.202 2002/09/22 17:27:25 tgl Exp $
+ * $Id: builtins.h,v 1.203 2002/10/19 02:08:18 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -261,6 +261,9 @@ extern Datum text_float4(PG_FUNCTION_ARGS);
extern Datum float8_text(PG_FUNCTION_ARGS);
extern Datum float4_text(PG_FUNCTION_ARGS);
extern Datum dround(PG_FUNCTION_ARGS);
+extern Datum dceil(PG_FUNCTION_ARGS);
+extern Datum dfloor(PG_FUNCTION_ARGS);
+extern Datum dsign(PG_FUNCTION_ARGS);
extern Datum dtrunc(PG_FUNCTION_ARGS);
extern Datum dsqrt(PG_FUNCTION_ARGS);
extern Datum dcbrt(PG_FUNCTION_ARGS);
diff --git a/src/test/regress/expected/float8.out b/src/test/regress/expected/float8.out
index 6449e6e2176..d91427c6ffa 100644
--- a/src/test/regress/expected/float8.out
+++ b/src/test/regress/expected/float8.out
@@ -149,13 +149,46 @@ SELECT '' AS five, f.f1, f.f1 % AS round_f1
| 1.2345678901234e-200 | 0
(5 rows)
+-- ceil
+select ceil(f1) as ceil_f1 from float8_tbl f;
+ ceil_f1
+----------------------
+ 0
+ 1005
+ -34
+ 1.2345678901234e+200
+ 1
+(5 rows)
+
+-- floor
+select floor(f1) as floor_f1 from float8_tbl f;
+ floor_f1
+----------------------
+ 0
+ 1004
+ -35
+ 1.2345678901234e+200
+ 0
+(5 rows)
+
+-- sign
+select sign(f1) as sign_f1 from float8_tbl f;
+ sign_f1
+---------
+ 0
+ 1
+ -1
+ 1
+ 1
+(5 rows)
+
+-- square root
SELECT sqrt(float8 '64') AS eight;
eight
-------
8
(1 row)
--- square root
SELECT |/ float8 '64' AS eight;
eight
-------
diff --git a/src/test/regress/sql/float8.sql b/src/test/regress/sql/float8.sql
index 8314f5cac9d..2cdb64a75a9 100644
--- a/src/test/regress/sql/float8.sql
+++ b/src/test/regress/sql/float8.sql
@@ -60,9 +60,18 @@ SELECT '' AS five, f.f1, %f.f1 AS trunc_f1
SELECT '' AS five, f.f1, f.f1 % AS round_f1
FROM FLOAT8_TBL f;
-SELECT sqrt(float8 '64') AS eight;
+-- ceil
+select ceil(f1) as ceil_f1 from float8_tbl f;
+
+-- floor
+select floor(f1) as floor_f1 from float8_tbl f;
+
+-- sign
+select sign(f1) as sign_f1 from float8_tbl f;
-- square root
+SELECT sqrt(float8 '64') AS eight;
+
SELECT |/ float8 '64' AS eight;
SELECT '' AS three, f.f1, |/f.f1 AS sqrt_f1