mirror of https://github.com/postgres/postgres
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
This commit is contained in:
parent
5c6a5fe18b
commit
bab3d29fba
|
@ -8,7 +8,7 @@
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* IDENTIFICATION
|
* 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
|
* check to see if a float4 val is outside of
|
||||||
the FLOAT4_MIN, FLOAT4_MAX bounds.
|
* the FLOAT4_MIN, FLOAT4_MAX bounds.
|
||||||
|
*
|
||||||
raise an elog warning if it is
|
* raise an elog warning if it is
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
CheckFloat4Val(double val)
|
CheckFloat4Val(double val)
|
||||||
|
@ -153,11 +153,11 @@ CheckFloat4Val(double val)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
check to see if a float8 val is outside of
|
* check to see if a float8 val is outside of
|
||||||
the FLOAT8_MIN, FLOAT8_MAX bounds.
|
* the FLOAT8_MIN, FLOAT8_MAX bounds.
|
||||||
|
*
|
||||||
raise an elog warning if it is
|
* raise an elog error if it is
|
||||||
*/
|
*/
|
||||||
static void
|
static void
|
||||||
CheckFloat8Val(double val)
|
CheckFloat8Val(double val)
|
||||||
{
|
{
|
||||||
|
@ -172,7 +172,6 @@ CheckFloat8Val(double val)
|
||||||
elog(ERROR, "Bad float8 input format -- overflow");
|
elog(ERROR, "Bad float8 input format -- overflow");
|
||||||
if (val != 0.0 && fabs(val) < FLOAT8_MIN)
|
if (val != 0.0 && fabs(val) < FLOAT8_MIN)
|
||||||
elog(ERROR, "Bad float8 input format -- underflow");
|
elog(ERROR, "Bad float8 input format -- underflow");
|
||||||
return;
|
|
||||||
#endif /* UNSAFE_FLOATS */
|
#endif /* UNSAFE_FLOATS */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1039,6 +1038,50 @@ dround(PG_FUNCTION_ARGS)
|
||||||
PG_RETURN_FLOAT8(result);
|
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,
|
* dtrunc - returns truncation-towards-zero of arg1,
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
*
|
*
|
||||||
* 1998 Jan Wieck
|
* 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);
|
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
|
Datum
|
||||||
numeric_sign(PG_FUNCTION_ARGS)
|
numeric_sign(PG_FUNCTION_ARGS)
|
||||||
{
|
{
|
||||||
|
|
|
@ -37,7 +37,7 @@
|
||||||
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
||||||
* Portions Copyright (c) 1994, Regents of the University of California
|
* 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 */
|
/* yyyymmddN */
|
||||||
#define CATALOG_VERSION_NO 200210141
|
#define CATALOG_VERSION_NO 200210181
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
||||||
* Portions Copyright (c) 1994, Regents of the University of California
|
* 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
|
* NOTES
|
||||||
* The script catalog/genbki.sh reads this file and generates .bki
|
* 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");
|
DESCR("round to nearest integer");
|
||||||
DATA(insert OID = 229 ( dtrunc PGNSP PGUID 12 f f t f i 1 701 "701" dtrunc - _null_ ));
|
DATA(insert OID = 229 ( dtrunc PGNSP PGUID 12 f f t f i 1 701 "701" dtrunc - _null_ ));
|
||||||
DESCR("truncate to integer");
|
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_ ));
|
DATA(insert OID = 230 ( dsqrt PGNSP PGUID 12 f f t f i 1 701 "701" dsqrt - _null_ ));
|
||||||
DESCR("square root");
|
DESCR("square root");
|
||||||
DATA(insert OID = 231 ( dcbrt PGNSP PGUID 12 f f t f i 1 701 "701" dcbrt - _null_ ));
|
DATA(insert OID = 231 ( dcbrt PGNSP PGUID 12 f f t f i 1 701 "701" dcbrt - _null_ ));
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
||||||
* Portions Copyright (c) 1994, Regents of the University of California
|
* 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 float8_text(PG_FUNCTION_ARGS);
|
||||||
extern Datum float4_text(PG_FUNCTION_ARGS);
|
extern Datum float4_text(PG_FUNCTION_ARGS);
|
||||||
extern Datum dround(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 dtrunc(PG_FUNCTION_ARGS);
|
||||||
extern Datum dsqrt(PG_FUNCTION_ARGS);
|
extern Datum dsqrt(PG_FUNCTION_ARGS);
|
||||||
extern Datum dcbrt(PG_FUNCTION_ARGS);
|
extern Datum dcbrt(PG_FUNCTION_ARGS);
|
||||||
|
|
|
@ -149,13 +149,46 @@ SELECT '' AS five, f.f1, f.f1 % AS round_f1
|
||||||
| 1.2345678901234e-200 | 0
|
| 1.2345678901234e-200 | 0
|
||||||
(5 rows)
|
(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;
|
SELECT sqrt(float8 '64') AS eight;
|
||||||
eight
|
eight
|
||||||
-------
|
-------
|
||||||
8
|
8
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
-- square root
|
|
||||||
SELECT |/ float8 '64' AS eight;
|
SELECT |/ float8 '64' AS eight;
|
||||||
eight
|
eight
|
||||||
-------
|
-------
|
||||||
|
|
|
@ -60,9 +60,18 @@ SELECT '' AS five, f.f1, %f.f1 AS trunc_f1
|
||||||
SELECT '' AS five, f.f1, f.f1 % AS round_f1
|
SELECT '' AS five, f.f1, f.f1 % AS round_f1
|
||||||
FROM FLOAT8_TBL f;
|
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
|
-- square root
|
||||||
|
SELECT sqrt(float8 '64') AS eight;
|
||||||
|
|
||||||
SELECT |/ float8 '64' AS eight;
|
SELECT |/ float8 '64' AS eight;
|
||||||
|
|
||||||
SELECT '' AS three, f.f1, |/f.f1 AS sqrt_f1
|
SELECT '' AS three, f.f1, |/f.f1 AS sqrt_f1
|
||||||
|
|
Loading…
Reference in New Issue