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

Commitbfd6f52

Browse files
author
Neil Conway
committed
Allow 'Infinity' and '-Infinity' as input to the float4 and float8
types. Update the regression tests and the documentation to reflectthis. Remove the UNSAFE_FLOATS #ifdef.This is only half the story: we still unconditionally rejectfloating point operations that result in +/- infinity. Seerecent thread on -hackers for more information.
1 parentfe6e922 commitbfd6f52

File tree

7 files changed

+112
-40
lines changed

7 files changed

+112
-40
lines changed

‎doc/src/sgml/syntax.sgml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/syntax.sgml,v 1.89 2003/11/29 19:51:37 pgsql Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/syntax.sgml,v 1.90 2004/03/12 00:25:40 neilc Exp $
33
-->
44

55
<chapter id="sql-syntax">
@@ -359,6 +359,23 @@ SELECT 'foo' 'bar';
359359
</literallayout>
360360
</para>
361361

362+
<para>
363+
In addition, there are several special constant values that are
364+
accepted as numeric constants. The <type>float4</type> and
365+
<type>float8</type> types allow the following special constants:
366+
<literallayout>
367+
Infinity
368+
-Infinity
369+
NaN
370+
</literallayout>
371+
These represent the IEEE 754 special values
372+
<quote>infinity</quote>, <quote>negative infinity</quote>, and
373+
<quote>not-a-number</quote>, respectively. The
374+
<type>numeric</type> type only allows <literal>NaN</>, whereas
375+
the integral types do not allow any of these constants. Note that
376+
these constants are recognized in a case-insensitive manner.
377+
</para>
378+
362379
<para>
363380
<indexterm><primary>integer</primary></indexterm>
364381
<indexterm><primary>bigint</primary></indexterm>

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

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/float.c,v 1.98 2004/03/11 02:11:13 neilc Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/float.c,v 1.99 2004/03/12 00:25:40 neilc Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -114,21 +114,14 @@ static intfloat8_cmp_internal(float8 a, float8 b);
114114

115115

116116
/*
117-
* check to see if a float4 val is outside of
118-
*the FLOAT4_MIN,FLOAT4_MAX bounds.
117+
* check to see if a float4 val is outside of the FLOAT4_MIN,
118+
* FLOAT4_MAX bounds.
119119
*
120-
* raise an ereport warning if it is
121-
*/
120+
* raise an ereport() error if it is
121+
*/
122122
staticvoid
123123
CheckFloat4Val(doubleval)
124124
{
125-
/*
126-
* defining unsafe floats's will make float4 and float8 ops faster at
127-
* the cost of safety, of course!
128-
*/
129-
#ifdefUNSAFE_FLOATS
130-
return;
131-
#else
132125
if (fabs(val)>FLOAT4_MAX)
133126
ereport(ERROR,
134127
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
@@ -137,27 +130,17 @@ CheckFloat4Val(double val)
137130
ereport(ERROR,
138131
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
139132
errmsg("type \"real\" value out of range: underflow")));
140-
141-
return;
142-
#endif/* UNSAFE_FLOATS */
143133
}
144134

145135
/*
146-
* check to see if a float8 val is outside of
147-
*the FLOAT8_MIN,FLOAT8_MAX bounds.
136+
* check to see if a float8 val is outside of the FLOAT8_MIN,
137+
* FLOAT8_MAX bounds.
148138
*
149-
* raise an ereport error if it is
139+
* raise an ereport() error if it is
150140
*/
151141
staticvoid
152142
CheckFloat8Val(doubleval)
153143
{
154-
/*
155-
* defining unsafe floats's will make float4 and float8 ops faster at
156-
* the cost of safety, of course!
157-
*/
158-
#ifdefUNSAFE_FLOATS
159-
return;
160-
#else
161144
if (fabs(val)>FLOAT8_MAX)
162145
ereport(ERROR,
163146
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
@@ -166,7 +149,6 @@ CheckFloat8Val(double val)
166149
ereport(ERROR,
167150
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
168151
errmsg("type \"double precision\" value out of range: underflow")));
169-
#endif/* UNSAFE_FLOATS */
170152
}
171153

172154
/*
@@ -201,10 +183,6 @@ float4in(PG_FUNCTION_ARGS)
201183
* empty strings, but emit a warning noting that the feature
202184
* is deprecated. In 7.6+, the warning should be replaced by
203185
* an error.
204-
*
205-
* XXX we should accept "Infinity" and "-Infinity" too, but
206-
* what are the correct values to assign? HUGE_VAL will
207-
* provoke an error from CheckFloat4Val.
208186
*/
209187
if (*num=='\0')
210188
{
@@ -217,6 +195,10 @@ float4in(PG_FUNCTION_ARGS)
217195
}
218196
elseif (strcasecmp(num,"NaN")==0)
219197
val=NAN;
198+
elseif (strcasecmp(num,"Infinity")==0)
199+
val=HUGE_VAL;
200+
elseif (strcasecmp(num,"-Infinity")==0)
201+
val=-HUGE_VAL;
220202
else
221203
ereport(ERROR,
222204
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
@@ -239,7 +221,8 @@ float4in(PG_FUNCTION_ARGS)
239221
* if we get here, we have a legal double, still need to check to see
240222
* if it's a legal float
241223
*/
242-
CheckFloat4Val(val);
224+
if (!isinf(val))
225+
CheckFloat4Val(val);
243226

244227
PG_RETURN_FLOAT4((float4)val);
245228
}
@@ -364,7 +347,8 @@ float8in(PG_FUNCTION_ARGS)
364347
errmsg("invalid input syntax for type double precision: \"%s\"",
365348
num)));
366349

367-
CheckFloat8Val(val);
350+
if (!isinf(val))
351+
CheckFloat8Val(val);
368352

369353
PG_RETURN_FLOAT8(val);
370354
}

‎src/include/pg_config_manual.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* for developers.If you edit any of these, be sure to do a *full*
77
* rebuild (and an initdb if noted).
88
*
9-
* $PostgreSQL: pgsql/src/include/pg_config_manual.h,v 1.10 2004/02/11 22:55:26 tgl Exp $
9+
* $PostgreSQL: pgsql/src/include/pg_config_manual.h,v 1.11 2004/03/12 00:25:40 neilc Exp $
1010
*------------------------------------------------------------------------
1111
*/
1212

@@ -175,12 +175,6 @@
175175
*/
176176
#defineDEFAULT_PGSOCKET_DIR "/tmp"
177177

178-
/*
179-
* Defining this will make float4 and float8 operations faster by
180-
* suppressing overflow/underflow checks.
181-
*/
182-
/* #define UNSAFE_FLOATS */
183-
184178
/*
185179
* The random() function is expected to yield values between 0 and
186180
* MAX_RANDOM_VALUE. Currently, all known implementations yield

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,39 @@ SELECT ' NAN '::float4;
5050
NaN
5151
(1 row)
5252

53+
SELECT 'infinity'::float4;
54+
float4
55+
----------
56+
Infinity
57+
(1 row)
58+
59+
SELECT ' -INFINiTY '::float4;
60+
float4
61+
-----------
62+
-Infinity
63+
(1 row)
64+
5365
-- bad special inputs
5466
SELECT 'N A N'::float4;
5567
ERROR: invalid input syntax for type real: "N A N"
68+
SELECT 'NaN x'::float4;
69+
ERROR: invalid input syntax for type real: "NaN x"
70+
SELECT ' INFINITY x'::float4;
71+
ERROR: invalid input syntax for type real: " INFINITY x"
72+
SELECT 'Infinity'::float4 + 100.0;
73+
ERROR: type "double precision" value out of range: overflow
74+
SELECT 'Infinity'::float4 / 'Infinity'::float4;
75+
?column?
76+
----------
77+
NaN
78+
(1 row)
79+
80+
SELECT 'nan'::float4 / 'nan'::float4;
81+
?column?
82+
----------
83+
NaN
84+
(1 row)
85+
5686
SELECT '' AS five, FLOAT4_TBL.*;
5787
five | f1
5888
------+-------------

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,39 @@ SELECT ' NAN '::float8;
5050
NaN
5151
(1 row)
5252

53+
SELECT 'infinity'::float8;
54+
float8
55+
----------
56+
Infinity
57+
(1 row)
58+
59+
SELECT ' -INFINiTY '::float8;
60+
float8
61+
-----------
62+
-Infinity
63+
(1 row)
64+
5365
-- bad special inputs
5466
SELECT 'N A N'::float8;
5567
ERROR: invalid input syntax for type double precision: "N A N"
68+
SELECT 'NaN x'::float8;
69+
ERROR: invalid input syntax for type double precision: "NaN x"
70+
SELECT ' INFINITY x'::float8;
71+
ERROR: invalid input syntax for type double precision: " INFINITY x"
72+
SELECT 'Infinity'::float8 + 100.0;
73+
ERROR: type "double precision" value out of range: overflow
74+
SELECT 'Infinity'::float8 / 'Infinity'::float8;
75+
?column?
76+
----------
77+
NaN
78+
(1 row)
79+
80+
SELECT 'nan'::float8 / 'nan'::float8;
81+
?column?
82+
----------
83+
NaN
84+
(1 row)
85+
5686
SELECT '' AS five, FLOAT8_TBL.*;
5787
five | f1
5888
------+----------------------

‎src/test/regress/sql/float4.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,17 @@ INSERT INTO FLOAT4_TBL(f1) VALUES ('123 5');
2929
SELECT'NaN'::float4;
3030
SELECT'nan'::float4;
3131
SELECT' NAN'::float4;
32+
SELECT'infinity'::float4;
33+
SELECT' -INFINiTY'::float4;
3234
-- bad special inputs
3335
SELECT'N A N'::float4;
36+
SELECT'NaN x'::float4;
37+
SELECT' INFINITY x'::float4;
38+
39+
SELECT'Infinity'::float4+100.0;
40+
SELECT'Infinity'::float4/'Infinity'::float4;
41+
SELECT'nan'::float4/'nan'::float4;
42+
3443

3544
SELECT''AS five, FLOAT4_TBL.*;
3645

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,16 @@ INSERT INTO FLOAT8_TBL(f1) VALUES ('123 5');
2929
SELECT'NaN'::float8;
3030
SELECT'nan'::float8;
3131
SELECT' NAN'::float8;
32+
SELECT'infinity'::float8;
33+
SELECT' -INFINiTY'::float8;
3234
-- bad special inputs
3335
SELECT'N A N'::float8;
36+
SELECT'NaN x'::float8;
37+
SELECT' INFINITY x'::float8;
38+
39+
SELECT'Infinity'::float8+100.0;
40+
SELECT'Infinity'::float8/'Infinity'::float8;
41+
SELECT'nan'::float8/'nan'::float8;
3442

3543
SELECT''AS five, FLOAT8_TBL.*;
3644

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp