Skip to content

Commit c384693

Browse files
committed
Support Infinity and NaN.
1 parent e0f0cf6 commit c384693

File tree

6 files changed

+50
-0
lines changed

6 files changed

+50
-0
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Remove bison 2 compat mode, but keep pregenerated files for jessie and
88
trusty.
99
* Support send/receive via the binary protocol.
10+
* Support Infinity and NaN.
1011

1112
6.0: Mar 7, 2018
1213
----------------

debian/changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ postgresql-unit (7.0-1) UNRELEASED; urgency=medium
77
* Remove bison 2 compat mode, but keep pregenerated files for jessie and
88
trusty.
99
* Support send/receive via the binary protocol.
10+
* Support Infinity and NaN.
1011

1112
-- Christoph Berg <[email protected]> Sun, 08 Jul 2018 21:46:17 +0200
1213

expected/unit.out

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,31 @@ SELECT '1 foobar'::unit AS error;
178178
ERROR: unit "foobar" is not known
179179
LINE 1: SELECT '1 foobar'::unit AS error;
180180
^
181+
-- special values
182+
SELECT '-0'::unit, -'0'::unit, '-0 m'::unit, -'0 m'::unit;
183+
unit | ?column? | unit | ?column?
184+
------+----------+------+----------
185+
-0 | -0 | -0 m | -0 m
186+
(1 row)
187+
188+
SELECT 'infinity'::unit, 'Infinity m'::unit, 'inf A'::unit;
189+
unit | unit | unit
190+
----------+------------+------------
191+
Infinity | Infinity m | Infinity A
192+
(1 row)
193+
194+
SELECT '-infinity'::unit, '-Infinity m'::unit, 'Inf A'::unit;
195+
unit | unit | unit
196+
-----------+-------------+------------
197+
-Infinity | -Infinity m | Infinity A
198+
(1 row)
199+
200+
SELECT 'nan'::unit, 'NaN'::unit;
201+
unit | unit
202+
------+------
203+
NaN | NaN
204+
(1 row)
205+
181206
-- test parser arithmetic
182207
SELECT '1|10'::unit, '1|10 m'::unit;
183208
unit | unit

float8out_internal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ float8out_internal(double num)
1111

1212
if (isnan(num))
1313
return strcpy(ascii, "NaN");
14+
if (!isfinite(num)) {
15+
if (num > 0)
16+
return strcpy(ascii, "Infinity");
17+
else
18+
return strcpy(ascii, "-Infinity");
19+
}
1420

1521
if (ndig < 1)
1622
ndig = 1;

sql/unit.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ SELECT '10 dm^3'::unit, '10l'::unit;
3939
SELECT '9.81 kg*m/s^2'::unit, '9.81 kg*m/s*s'::unit, '9.81 kg*m/s/s'::unit;
4040
SELECT '1 foobar'::unit AS error;
4141

42+
-- special values
43+
SELECT '-0'::unit, -'0'::unit, '-0 m'::unit, -'0 m'::unit;
44+
SELECT 'infinity'::unit, 'Infinity m'::unit, 'inf A'::unit;
45+
SELECT '-infinity'::unit, '-Infinity m'::unit, 'Inf A'::unit;
46+
SELECT 'nan'::unit, 'NaN'::unit;
47+
4248
-- test parser arithmetic
4349
SELECT '1|10'::unit, '1|10 m'::unit;
4450
SELECT '2 m + 3 m - 4 m'::unit, '6 m - 3 m - 2 m'::unit, '6 m - 3 m + 1 m'::unit;

unitparse.l

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ GNU General Public License for more details.
1818
#include <executor/spi.h>
1919
#include <utils/builtins.h> /* CStringGetTextDatum */
2020
#include <catalog/pg_type.h> /* TEXTOID */
21+
#include <math.h> /* INFINITY, NAN */
2122

2223
#define when(x) if (!strcmp(yytext, x))
2324
%}
@@ -73,6 +74,16 @@ SUPER_9 \xe2\x81\xb9
7374
return DOUBLE;
7475
}
7576
77+
[Ii][Nn][Ff]([Ii][Nn][Ii][Tt][Yy])? {
78+
yyunitlval.DOUBLE = INFINITY;
79+
return DOUBLE;
80+
}
81+
82+
[Nn][Aa][Nn] {
83+
yyunitlval.DOUBLE = NAN;
84+
return DOUBLE;
85+
}
86+
7687
{TIME_R} { /* hh:mm:ss[.sss] */
7788
char *colon;
7889
yyunitlval.DOUBLE = TIME_HOUR * atoi(yytext); /* hh */

0 commit comments

Comments
 (0)