OSDN Git Service

This patch adds some missing functions for float8 math operations,
authorBruce Momjian <bruce@momjian.us>
Sat, 19 Oct 2002 02:08:19 +0000 (02:08 +0000)
committerBruce Momjian <bruce@momjian.us>
Sat, 19 Oct 2002 02:08:19 +0000 (02:08 +0000)
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

src/backend/utils/adt/float.c
src/backend/utils/adt/numeric.c
src/include/catalog/catversion.h
src/include/catalog/pg_proc.h
src/include/utils/builtins.h
src/test/regress/expected/float8.out
src/test/regress/sql/float8.sql

index 7b1798d..fa34e10 100644 (file)
@@ -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,
index 7f32e2e..f196701 100644 (file)
@@ -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)
 {
index 967cb79..58df9af 100644 (file)
@@ -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
index 369da46..9519781 100644 (file)
@@ -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_ ));
index 9f1c9ba..990c9f3 100644 (file)
@@ -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);
index 6449e6e..d91427c 100644 (file)
@@ -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 
 -------
index 8314f5c..2cdb64a 100644 (file)
@@ -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