Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbab3d29

Browse files
committed
This patch adds some missing functions for float8 math operations,
specifically ceil(), floor(), and sign(). There may be other functionsthat need to be added, but this is a start. I've included some simpleregression tests.Neil Conway
1 parent5c6a5fe commitbab3d29

File tree

7 files changed

+119
-19
lines changed

7 files changed

+119
-19
lines changed

‎src/backend/utils/adt/float.c

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/float.c,v 1.81 2002/09/04 20:31:27 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/float.c,v 1.82 2002/10/19 02:08:17 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -129,10 +129,10 @@ static void CheckFloat8Val(double val);
129129

130130

131131
/*
132-
check to see if a float4 val is outside of
133-
the FLOAT4_MIN, FLOAT4_MAX bounds.
134-
135-
raise an elog warning if it is
132+
* check to see if a float4 val is outside of
133+
* the FLOAT4_MIN, FLOAT4_MAX bounds.
134+
*
135+
* raise an elog warning if it is
136136
*/
137137
staticvoid
138138
CheckFloat4Val(doubleval)
@@ -153,11 +153,11 @@ CheckFloat4Val(double val)
153153
}
154154

155155
/*
156-
check to see if a float8 val is outside of
157-
the FLOAT8_MIN, FLOAT8_MAX bounds.
158-
159-
raise an elogwarning if it is
160-
*/
156+
* check to see if a float8 val is outside of
157+
* the FLOAT8_MIN, FLOAT8_MAX bounds.
158+
*
159+
*raise an elogerror if it is
160+
*/
161161
staticvoid
162162
CheckFloat8Val(doubleval)
163163
{
@@ -172,7 +172,6 @@ CheckFloat8Val(double val)
172172
elog(ERROR,"Bad float8 input format -- overflow");
173173
if (val!=0.0&&fabs(val)<FLOAT8_MIN)
174174
elog(ERROR,"Bad float8 input format -- underflow");
175-
return;
176175
#endif/* UNSAFE_FLOATS */
177176
}
178177

@@ -1039,6 +1038,50 @@ dround(PG_FUNCTION_ARGS)
10391038
PG_RETURN_FLOAT8(result);
10401039
}
10411040

1041+
/*
1042+
*dceil- returns the smallest integer greater than or
1043+
* equal to the specified float
1044+
*/
1045+
Datum
1046+
dceil(PG_FUNCTION_ARGS)
1047+
{
1048+
float8arg1=PG_GETARG_FLOAT8(0);
1049+
1050+
PG_RETURN_FLOAT8(ceil(arg1));
1051+
}
1052+
1053+
/*
1054+
*dfloor- returns the largest integer lesser than or
1055+
* equal to the specified float
1056+
*/
1057+
Datum
1058+
dfloor(PG_FUNCTION_ARGS)
1059+
{
1060+
float8arg1=PG_GETARG_FLOAT8(0);
1061+
1062+
PG_RETURN_FLOAT8(floor(arg1));
1063+
}
1064+
1065+
/*
1066+
*dsign- returns -1 if the argument is less than 0, 0
1067+
* if the argument is equal to 0, and 1 if the
1068+
* argument is greater than zero.
1069+
*/
1070+
Datum
1071+
dsign(PG_FUNCTION_ARGS)
1072+
{
1073+
float8arg1=PG_GETARG_FLOAT8(0);
1074+
float8result;
1075+
1076+
if (arg1>0)
1077+
result=1.0;
1078+
elseif (arg1<0)
1079+
result=-1.0;
1080+
else
1081+
result=0.0;
1082+
1083+
PG_RETURN_FLOAT8(result);
1084+
}
10421085

10431086
/*
10441087
*dtrunc- returns truncation-towards-zero of arg1,

‎src/backend/utils/adt/numeric.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
*1998 Jan Wieck
77
*
8-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/numeric.c,v 1.55 2002/10/02 19:21:26 tgl Exp $
8+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/numeric.c,v 1.56 2002/10/19 02:08:17 momjian Exp $
99
*
1010
* ----------
1111
*/
@@ -425,7 +425,13 @@ numeric_uplus(PG_FUNCTION_ARGS)
425425
PG_RETURN_NUMERIC(res);
426426
}
427427

428-
428+
/* ----------
429+
* numeric_sign() -
430+
*
431+
* returns -1 if the argument is less than 0, 0 if the argument is equal
432+
* to 0, and 1 if the argument is greater than zero.
433+
* ----------
434+
*/
429435
Datum
430436
numeric_sign(PG_FUNCTION_ARGS)
431437
{

‎src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $Id: catversion.h,v 1.161 2002/10/14 22:14:35 tgl Exp $
40+
* $Id: catversion.h,v 1.162 2002/10/19 02:08:18 momjian Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/*yyyymmddN */
56-
#defineCATALOG_VERSION_NO200210141
56+
#defineCATALOG_VERSION_NO200210181
5757

5858
#endif

‎src/include/catalog/pg_proc.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: pg_proc.h,v 1.273 2002/09/22 17:27:23 tgl Exp $
10+
* $Id: pg_proc.h,v 1.274 2002/10/19 02:08:18 momjian Exp $
1111
*
1212
* NOTES
1313
* 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
473473
DESCR("round to nearest integer");
474474
DATA(insertOID=229 (dtruncPGNSPPGUID12fftfi1701"701"dtrunc-_null_ ));
475475
DESCR("truncate to integer");
476+
DATA(insertOID=2308 (ceilPGNSPPGUID12fftfi1701"701"dceil-_null_ ));
477+
DESCR("smallest integer >= value");
478+
DATA(insertOID=2309 (floorPGNSPPGUID12fftfi1701"701"dfloor-_null_ ));
479+
DESCR("largest integer <= value");
480+
DATA(insertOID=2310 (signPGNSPPGUID12fftfi1701"701"dsign-_null_ ));
481+
DESCR("sign of value");
476482
DATA(insertOID=230 (dsqrtPGNSPPGUID12fftfi1701"701"dsqrt-_null_ ));
477483
DESCR("square root");
478484
DATA(insertOID=231 (dcbrtPGNSPPGUID12fftfi1701"701"dcbrt-_null_ ));

‎src/include/utils/builtins.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: builtins.h,v 1.202 2002/09/22 17:27:25 tgl Exp $
10+
* $Id: builtins.h,v 1.203 2002/10/19 02:08:18 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -261,6 +261,9 @@ extern Datum text_float4(PG_FUNCTION_ARGS);
261261
externDatumfloat8_text(PG_FUNCTION_ARGS);
262262
externDatumfloat4_text(PG_FUNCTION_ARGS);
263263
externDatumdround(PG_FUNCTION_ARGS);
264+
externDatumdceil(PG_FUNCTION_ARGS);
265+
externDatumdfloor(PG_FUNCTION_ARGS);
266+
externDatumdsign(PG_FUNCTION_ARGS);
264267
externDatumdtrunc(PG_FUNCTION_ARGS);
265268
externDatumdsqrt(PG_FUNCTION_ARGS);
266269
externDatumdcbrt(PG_FUNCTION_ARGS);

‎src/test/regress/expected/float8.out

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,46 @@ SELECT '' AS five, f.f1, f.f1 % AS round_f1
149149
| 1.2345678901234e-200 | 0
150150
(5 rows)
151151

152+
-- ceil
153+
select ceil(f1) as ceil_f1 from float8_tbl f;
154+
ceil_f1
155+
----------------------
156+
0
157+
1005
158+
-34
159+
1.2345678901234e+200
160+
1
161+
(5 rows)
162+
163+
-- floor
164+
select floor(f1) as floor_f1 from float8_tbl f;
165+
floor_f1
166+
----------------------
167+
0
168+
1004
169+
-35
170+
1.2345678901234e+200
171+
0
172+
(5 rows)
173+
174+
-- sign
175+
select sign(f1) as sign_f1 from float8_tbl f;
176+
sign_f1
177+
---------
178+
0
179+
1
180+
-1
181+
1
182+
1
183+
(5 rows)
184+
185+
-- square root
152186
SELECT sqrt(float8 '64') AS eight;
153187
eight
154188
-------
155189
8
156190
(1 row)
157191

158-
-- square root
159192
SELECT |/ float8 '64' AS eight;
160193
eight
161194
-------

‎src/test/regress/sql/float8.sql

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,18 @@ SELECT '' AS five, f.f1, %f.f1 AS trunc_f1
6060
SELECT''AS five,f.f1,f.f1 %AS round_f1
6161
FROM FLOAT8_TBL f;
6262

63-
SELECT sqrt(float8'64')AS eight;
63+
-- ceil
64+
select ceil(f1)as ceil_f1from float8_tbl f;
65+
66+
-- floor
67+
select floor(f1)as floor_f1from float8_tbl f;
68+
69+
-- sign
70+
select sign(f1)as sign_f1from float8_tbl f;
6471

6572
-- square root
73+
SELECT sqrt(float8'64')AS eight;
74+
6675
SELECT |/ float8'64'AS eight;
6776

6877
SELECT''AS three,f.f1, |/f.f1AS sqrt_f1

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp