1--
2-- FLOAT4
3--
4
5CREATE TABLE FLOAT4_TBL (f1  float4);
6
7INSERT INTO FLOAT4_TBL(f1) VALUES ('    0.0');
8INSERT INTO FLOAT4_TBL(f1) VALUES ('1004.30   ');
9INSERT INTO FLOAT4_TBL(f1) VALUES ('     -34.84    ');
10INSERT INTO FLOAT4_TBL(f1) VALUES ('1.2345678901234e+20');
11INSERT INTO FLOAT4_TBL(f1) VALUES ('1.2345678901234e-20');
12
13-- test for over and under flow
14INSERT INTO FLOAT4_TBL(f1) VALUES ('10e70');
15INSERT INTO FLOAT4_TBL(f1) VALUES ('-10e70');
16INSERT INTO FLOAT4_TBL(f1) VALUES ('10e-70');
17INSERT INTO FLOAT4_TBL(f1) VALUES ('-10e-70');
18
19INSERT INTO FLOAT4_TBL(f1) VALUES ('10e400');
20INSERT INTO FLOAT4_TBL(f1) VALUES ('-10e400');
21INSERT INTO FLOAT4_TBL(f1) VALUES ('10e-400');
22INSERT INTO FLOAT4_TBL(f1) VALUES ('-10e-400');
23
24-- bad input
25INSERT INTO FLOAT4_TBL(f1) VALUES ('');
26INSERT INTO FLOAT4_TBL(f1) VALUES ('       ');
27INSERT INTO FLOAT4_TBL(f1) VALUES ('xyz');
28INSERT INTO FLOAT4_TBL(f1) VALUES ('5.0.0');
29INSERT INTO FLOAT4_TBL(f1) VALUES ('5 . 0');
30INSERT INTO FLOAT4_TBL(f1) VALUES ('5.   0');
31INSERT INTO FLOAT4_TBL(f1) VALUES ('     - 3.0');
32INSERT INTO FLOAT4_TBL(f1) VALUES ('123            5');
33
34-- special inputs
35SELECT 'NaN'::float4;
36SELECT 'nan'::float4;
37SELECT '   NAN  '::float4;
38SELECT 'infinity'::float4;
39SELECT '          -INFINiTY   '::float4;
40-- bad special inputs
41SELECT 'N A N'::float4;
42SELECT 'NaN x'::float4;
43SELECT ' INFINITY    x'::float4;
44
45SELECT 'Infinity'::float4 + 100.0;
46SELECT 'Infinity'::float4 / 'Infinity'::float4;
47SELECT 'nan'::float4 / 'nan'::float4;
48SELECT 'nan'::numeric::float4;
49
50SELECT '' AS five, * FROM FLOAT4_TBL;
51
52SELECT '' AS four, f.* FROM FLOAT4_TBL f WHERE f.f1 <> '1004.3';
53
54SELECT '' AS one, f.* FROM FLOAT4_TBL f WHERE f.f1 = '1004.3';
55
56SELECT '' AS three, f.* FROM FLOAT4_TBL f WHERE '1004.3' > f.f1;
57
58SELECT '' AS three, f.* FROM FLOAT4_TBL f WHERE  f.f1 < '1004.3';
59
60SELECT '' AS four, f.* FROM FLOAT4_TBL f WHERE '1004.3' >= f.f1;
61
62SELECT '' AS four, f.* FROM FLOAT4_TBL f WHERE  f.f1 <= '1004.3';
63
64SELECT '' AS three, f.f1, f.f1 * '-10' AS x FROM FLOAT4_TBL f
65   WHERE f.f1 > '0.0';
66
67SELECT '' AS three, f.f1, f.f1 + '-10' AS x FROM FLOAT4_TBL f
68   WHERE f.f1 > '0.0';
69
70SELECT '' AS three, f.f1, f.f1 / '-10' AS x FROM FLOAT4_TBL f
71   WHERE f.f1 > '0.0';
72
73SELECT '' AS three, f.f1, f.f1 - '-10' AS x FROM FLOAT4_TBL f
74   WHERE f.f1 > '0.0';
75
76-- test divide by zero
77SELECT '' AS bad, f.f1 / '0.0' from FLOAT4_TBL f;
78
79SELECT '' AS five, * FROM FLOAT4_TBL;
80
81-- test the unary float4abs operator
82SELECT '' AS five, f.f1, @f.f1 AS abs_f1 FROM FLOAT4_TBL f;
83
84UPDATE FLOAT4_TBL
85   SET f1 = FLOAT4_TBL.f1 * '-1'
86   WHERE FLOAT4_TBL.f1 > '0.0';
87
88SELECT '' AS five, * FROM FLOAT4_TBL;
89
90-- test edge-case coercions to integer
91SELECT '32767.4'::float4::int2;
92SELECT '32767.6'::float4::int2;
93SELECT '-32768.4'::float4::int2;
94SELECT '-32768.6'::float4::int2;
95SELECT '2147483520'::float4::int4;
96SELECT '2147483647'::float4::int4;
97SELECT '-2147483648.5'::float4::int4;
98SELECT '-2147483900'::float4::int4;
99SELECT '9223369837831520256'::float4::int8;
100SELECT '9223372036854775807'::float4::int8;
101SELECT '-9223372036854775808.5'::float4::int8;
102SELECT '-9223380000000000000'::float4::int8;
103
104-- Test for correct input rounding in edge cases.
105-- These lists are from Paxson 1991, excluding subnormals and
106-- inputs of over 9 sig. digits.
107
108SELECT float4send('5e-20'::float4);
109SELECT float4send('67e14'::float4);
110SELECT float4send('985e15'::float4);
111SELECT float4send('55895e-16'::float4);
112SELECT float4send('7038531e-32'::float4);
113SELECT float4send('702990899e-20'::float4);
114
115SELECT float4send('3e-23'::float4);
116SELECT float4send('57e18'::float4);
117SELECT float4send('789e-35'::float4);
118SELECT float4send('2539e-18'::float4);
119SELECT float4send('76173e28'::float4);
120SELECT float4send('887745e-11'::float4);
121SELECT float4send('5382571e-37'::float4);
122SELECT float4send('82381273e-35'::float4);
123SELECT float4send('750486563e-38'::float4);
124
125-- Test that the smallest possible normalized input value inputs
126-- correctly, either in 9-significant-digit or shortest-decimal
127-- format.
128--
129-- exact val is             1.1754943508...
130-- shortest val is          1.1754944000
131-- midpoint to next val is  1.1754944208...
132
133SELECT float4send('1.17549435e-38'::float4);
134SELECT float4send('1.1754944e-38'::float4);
135
136-- test output (and round-trip safety) of various values.
137-- To ensure we're testing what we think we're testing, start with
138-- float values specified by bit patterns (as a useful side effect,
139-- this means we'll fail on non-IEEE platforms).
140
141create type xfloat4;
142create function xfloat4in(cstring) returns xfloat4 immutable strict
143  language internal as 'int4in';
144create function xfloat4out(xfloat4) returns cstring immutable strict
145  language internal as 'int4out';
146create type xfloat4 (input = xfloat4in, output = xfloat4out, like = float4);
147create cast (xfloat4 as float4) without function;
148create cast (float4 as xfloat4) without function;
149create cast (xfloat4 as integer) without function;
150create cast (integer as xfloat4) without function;
151
152-- float4: seeeeeee emmmmmmm mmmmmmmm mmmmmmmm
153
154-- we don't care to assume the platform's strtod() handles subnormals
155-- correctly; those are "use at your own risk". However we do test
156-- subnormal outputs, since those are under our control.
157
158with testdata(bits) as (values
159  -- small subnormals
160  (x'00000001'),
161  (x'00000002'), (x'00000003'),
162  (x'00000010'), (x'00000011'), (x'00000100'), (x'00000101'),
163  (x'00004000'), (x'00004001'), (x'00080000'), (x'00080001'),
164  -- stress values
165  (x'0053c4f4'),  -- 7693e-42
166  (x'006c85c4'),  -- 996622e-44
167  (x'0041ca76'),  -- 60419369e-46
168  (x'004b7678'),  -- 6930161142e-48
169  -- taken from upstream testsuite
170  (x'00000007'),
171  (x'00424fe2'),
172  -- borderline between subnormal and normal
173  (x'007ffff0'), (x'007ffff1'), (x'007ffffe'), (x'007fffff'))
174select float4send(flt) as ibits,
175       flt
176  from (select bits::integer::xfloat4::float4 as flt
177          from testdata
178	offset 0) s;
179
180with testdata(bits) as (values
181  (x'00000000'),
182  -- smallest normal values
183  (x'00800000'), (x'00800001'), (x'00800004'), (x'00800005'),
184  (x'00800006'),
185  -- small normal values chosen for short vs. long output
186  (x'008002f1'), (x'008002f2'), (x'008002f3'),
187  (x'00800e17'), (x'00800e18'), (x'00800e19'),
188  -- assorted values (random mantissae)
189  (x'01000001'), (x'01102843'), (x'01a52c98'),
190  (x'0219c229'), (x'02e4464d'), (x'037343c1'), (x'03a91b36'),
191  (x'047ada65'), (x'0496fe87'), (x'0550844f'), (x'05999da3'),
192  (x'060ea5e2'), (x'06e63c45'), (x'07f1e548'), (x'0fc5282b'),
193  (x'1f850283'), (x'2874a9d6'),
194  -- values around 5e-08
195  (x'3356bf94'), (x'3356bf95'), (x'3356bf96'),
196  -- around 1e-07
197  (x'33d6bf94'), (x'33d6bf95'), (x'33d6bf96'),
198  -- around 3e-07 .. 1e-04
199  (x'34a10faf'), (x'34a10fb0'), (x'34a10fb1'),
200  (x'350637bc'), (x'350637bd'), (x'350637be'),
201  (x'35719786'), (x'35719787'), (x'35719788'),
202  (x'358637bc'), (x'358637bd'), (x'358637be'),
203  (x'36a7c5ab'), (x'36a7c5ac'), (x'36a7c5ad'),
204  (x'3727c5ab'), (x'3727c5ac'), (x'3727c5ad'),
205  -- format crossover at 1e-04
206  (x'38d1b714'), (x'38d1b715'), (x'38d1b716'),
207  (x'38d1b717'), (x'38d1b718'), (x'38d1b719'),
208  (x'38d1b71a'), (x'38d1b71b'), (x'38d1b71c'),
209  (x'38d1b71d'),
210  --
211  (x'38dffffe'), (x'38dfffff'), (x'38e00000'),
212  (x'38efffff'), (x'38f00000'), (x'38f00001'),
213  (x'3a83126e'), (x'3a83126f'), (x'3a831270'),
214  (x'3c23d709'), (x'3c23d70a'), (x'3c23d70b'),
215  (x'3dcccccc'), (x'3dcccccd'), (x'3dccccce'),
216  -- chosen to need 9 digits for 3dcccd70
217  (x'3dcccd6f'), (x'3dcccd70'), (x'3dcccd71'),
218  --
219  (x'3effffff'), (x'3f000000'), (x'3f000001'),
220  (x'3f333332'), (x'3f333333'), (x'3f333334'),
221  -- approach 1.0 with increasing numbers of 9s
222  (x'3f666665'), (x'3f666666'), (x'3f666667'),
223  (x'3f7d70a3'), (x'3f7d70a4'), (x'3f7d70a5'),
224  (x'3f7fbe76'), (x'3f7fbe77'), (x'3f7fbe78'),
225  (x'3f7ff971'), (x'3f7ff972'), (x'3f7ff973'),
226  (x'3f7fff57'), (x'3f7fff58'), (x'3f7fff59'),
227  (x'3f7fffee'), (x'3f7fffef'),
228  -- values very close to 1
229  (x'3f7ffff0'), (x'3f7ffff1'), (x'3f7ffff2'),
230  (x'3f7ffff3'), (x'3f7ffff4'), (x'3f7ffff5'),
231  (x'3f7ffff6'), (x'3f7ffff7'), (x'3f7ffff8'),
232  (x'3f7ffff9'), (x'3f7ffffa'), (x'3f7ffffb'),
233  (x'3f7ffffc'), (x'3f7ffffd'), (x'3f7ffffe'),
234  (x'3f7fffff'),
235  (x'3f800000'),
236  (x'3f800001'), (x'3f800002'), (x'3f800003'),
237  (x'3f800004'), (x'3f800005'), (x'3f800006'),
238  (x'3f800007'), (x'3f800008'), (x'3f800009'),
239  -- values 1 to 1.1
240  (x'3f80000f'), (x'3f800010'), (x'3f800011'),
241  (x'3f800012'), (x'3f800013'), (x'3f800014'),
242  (x'3f800017'), (x'3f800018'), (x'3f800019'),
243  (x'3f80001a'), (x'3f80001b'), (x'3f80001c'),
244  (x'3f800029'), (x'3f80002a'), (x'3f80002b'),
245  (x'3f800053'), (x'3f800054'), (x'3f800055'),
246  (x'3f800346'), (x'3f800347'), (x'3f800348'),
247  (x'3f8020c4'), (x'3f8020c5'), (x'3f8020c6'),
248  (x'3f8147ad'), (x'3f8147ae'), (x'3f8147af'),
249  (x'3f8ccccc'), (x'3f8ccccd'), (x'3f8cccce'),
250  --
251  (x'3fc90fdb'), -- pi/2
252  (x'402df854'), -- e
253  (x'40490fdb'), -- pi
254  --
255  (x'409fffff'), (x'40a00000'), (x'40a00001'),
256  (x'40afffff'), (x'40b00000'), (x'40b00001'),
257  (x'411fffff'), (x'41200000'), (x'41200001'),
258  (x'42c7ffff'), (x'42c80000'), (x'42c80001'),
259  (x'4479ffff'), (x'447a0000'), (x'447a0001'),
260  (x'461c3fff'), (x'461c4000'), (x'461c4001'),
261  (x'47c34fff'), (x'47c35000'), (x'47c35001'),
262  (x'497423ff'), (x'49742400'), (x'49742401'),
263  (x'4b18967f'), (x'4b189680'), (x'4b189681'),
264  (x'4cbebc1f'), (x'4cbebc20'), (x'4cbebc21'),
265  (x'4e6e6b27'), (x'4e6e6b28'), (x'4e6e6b29'),
266  (x'501502f8'), (x'501502f9'), (x'501502fa'),
267  (x'51ba43b6'), (x'51ba43b7'), (x'51ba43b8'),
268  -- stress values
269  (x'1f6c1e4a'),  -- 5e-20
270  (x'59be6cea'),  -- 67e14
271  (x'5d5ab6c4'),  -- 985e15
272  (x'2cc4a9bd'),  -- 55895e-16
273  (x'15ae43fd'),  -- 7038531e-32
274  (x'2cf757ca'),  -- 702990899e-20
275  (x'665ba998'),  -- 25933168707e13
276  (x'743c3324'),  -- 596428896559e20
277  -- exercise fixed-point memmoves
278  (x'47f1205a'),
279  (x'4640e6ae'),
280  (x'449a5225'),
281  (x'42f6e9d5'),
282  (x'414587dd'),
283  (x'3f9e064b'),
284  -- these cases come from the upstream's testsuite
285  -- BoundaryRoundEven
286  (x'4c000004'),
287  (x'50061c46'),
288  (x'510006a8'),
289  -- ExactValueRoundEven
290  (x'48951f84'),
291  (x'45fd1840'),
292  -- LotsOfTrailingZeros
293  (x'39800000'),
294  (x'3b200000'),
295  (x'3b900000'),
296  (x'3bd00000'),
297  -- Regression
298  (x'63800000'),
299  (x'4b000000'),
300  (x'4b800000'),
301  (x'4c000001'),
302  (x'4c800b0d'),
303  (x'00d24584'),
304  (x'00d90b88'),
305  (x'45803f34'),
306  (x'4f9f24f7'),
307  (x'3a8722c3'),
308  (x'5c800041'),
309  (x'15ae43fd'),
310  (x'5d4cccfb'),
311  (x'4c800001'),
312  (x'57800ed8'),
313  (x'5f000000'),
314  (x'700000f0'),
315  (x'5f23e9ac'),
316  (x'5e9502f9'),
317  (x'5e8012b1'),
318  (x'3c000028'),
319  (x'60cde861'),
320  (x'03aa2a50'),
321  (x'43480000'),
322  (x'4c000000'),
323  -- LooksLikePow5
324  (x'5D1502F9'),
325  (x'5D9502F9'),
326  (x'5E1502F9'),
327  -- OutputLength
328  (x'3f99999a'),
329  (x'3f9d70a4'),
330  (x'3f9df3b6'),
331  (x'3f9e0419'),
332  (x'3f9e0610'),
333  (x'3f9e064b'),
334  (x'3f9e0651'),
335  (x'03d20cfe')
336)
337select float4send(flt) as ibits,
338       flt,
339       flt::text::float4 as r_flt,
340       float4send(flt::text::float4) as obits,
341       float4send(flt::text::float4) = float4send(flt) as correct
342  from (select bits::integer::xfloat4::float4 as flt
343          from testdata
344	offset 0) s;
345
346-- clean up, lest opr_sanity complain
347drop type xfloat4 cascade;
348