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