|
| 1 | +-- |
| 2 | +-- FLOAT8 |
| 3 | +-- |
| 4 | +CREATE TABLE FLOAT8_TBL(f1 float8); |
| 5 | +INSERT INTO FLOAT8_TBL(f1) VALUES (' 0.0 '); |
| 6 | +INSERT INTO FLOAT8_TBL(f1) VALUES ('1004.30 '); |
| 7 | +INSERT INTO FLOAT8_TBL(f1) VALUES (' -34.84'); |
| 8 | +INSERT INTO FLOAT8_TBL(f1) VALUES ('1.2345678901234e+200'); |
| 9 | +INSERT INTO FLOAT8_TBL(f1) VALUES ('1.2345678901234e-200'); |
| 10 | +-- test for underflow and overflow handling |
| 11 | +SELECT '10e400'::float8; |
| 12 | +ERROR: "10e400" is out of range for type double precision |
| 13 | +SELECT '-10e400'::float8; |
| 14 | +ERROR: "-10e400" is out of range for type double precision |
| 15 | +SELECT '10e-400'::float8; |
| 16 | + float8 |
| 17 | +-------- |
| 18 | + 0 |
| 19 | +(1 row) |
| 20 | + |
| 21 | +SELECT '-10e-400'::float8; |
| 22 | + float8 |
| 23 | +-------- |
| 24 | + 0 |
| 25 | +(1 row) |
| 26 | + |
| 27 | +-- bad input |
| 28 | +INSERT INTO FLOAT8_TBL(f1) VALUES (' '); |
| 29 | +ERROR: invalid input syntax for type double precision: " " |
| 30 | +INSERT INTO FLOAT8_TBL(f1) VALUES ('xyz'); |
| 31 | +ERROR: invalid input syntax for type double precision: "xyz" |
| 32 | +INSERT INTO FLOAT8_TBL(f1) VALUES ('5.0.0'); |
| 33 | +ERROR: invalid input syntax for type double precision: "5.0.0" |
| 34 | +INSERT INTO FLOAT8_TBL(f1) VALUES ('5 . 0'); |
| 35 | +ERROR: invalid input syntax for type double precision: "5 . 0" |
| 36 | +INSERT INTO FLOAT8_TBL(f1) VALUES ('5. 0'); |
| 37 | +ERROR: invalid input syntax for type double precision: "5. 0" |
| 38 | +INSERT INTO FLOAT8_TBL(f1) VALUES (' - 3'); |
| 39 | +ERROR: invalid input syntax for type double precision: " - 3" |
| 40 | +INSERT INTO FLOAT8_TBL(f1) VALUES ('123 5'); |
| 41 | +ERROR: invalid input syntax for type double precision: "123 5" |
| 42 | +-- special inputs |
| 43 | +SELECT 'NaN'::float8; |
| 44 | + float8 |
| 45 | +-------- |
| 46 | + NaN |
| 47 | +(1 row) |
| 48 | + |
| 49 | +SELECT 'nan'::float8; |
| 50 | + float8 |
| 51 | +-------- |
| 52 | + NaN |
| 53 | +(1 row) |
| 54 | + |
| 55 | +SELECT ' NAN '::float8; |
| 56 | + float8 |
| 57 | +-------- |
| 58 | + NaN |
| 59 | +(1 row) |
| 60 | + |
| 61 | +SELECT 'infinity'::float8; |
| 62 | + float8 |
| 63 | +---------- |
| 64 | + Infinity |
| 65 | +(1 row) |
| 66 | + |
| 67 | +SELECT ' -INFINiTY '::float8; |
| 68 | + float8 |
| 69 | +----------- |
| 70 | + -Infinity |
| 71 | +(1 row) |
| 72 | + |
| 73 | +-- bad special inputs |
| 74 | +SELECT 'N A N'::float8; |
| 75 | +ERROR: invalid input syntax for type double precision: "N A N" |
| 76 | +SELECT 'NaN x'::float8; |
| 77 | +ERROR: invalid input syntax for type double precision: "NaN x" |
| 78 | +SELECT ' INFINITY x'::float8; |
| 79 | +ERROR: invalid input syntax for type double precision: " INFINITY x" |
| 80 | +SELECT 'Infinity'::float8 + 100.0; |
| 81 | +ERROR: type "double precision" value out of range: overflow |
| 82 | +SELECT 'Infinity'::float8 / 'Infinity'::float8; |
| 83 | + ?column? |
| 84 | +---------- |
| 85 | + NaN |
| 86 | +(1 row) |
| 87 | + |
| 88 | +SELECT 'nan'::float8 / 'nan'::float8; |
| 89 | + ?column? |
| 90 | +---------- |
| 91 | + NaN |
| 92 | +(1 row) |
| 93 | + |
| 94 | +SELECT '' AS five, FLOAT8_TBL.*; |
| 95 | + five | f1 |
| 96 | +------+---------------------- |
| 97 | + | 0 |
| 98 | + | 1004.3 |
| 99 | + | -34.84 |
| 100 | + | 1.2345678901234e+200 |
| 101 | + | 1.2345678901234e-200 |
| 102 | +(5 rows) |
| 103 | + |
| 104 | +SELECT '' AS four, f.* FROM FLOAT8_TBL f WHERE f.f1 <> '1004.3'; |
| 105 | + four | f1 |
| 106 | +------+---------------------- |
| 107 | + | 0 |
| 108 | + | -34.84 |
| 109 | + | 1.2345678901234e+200 |
| 110 | + | 1.2345678901234e-200 |
| 111 | +(4 rows) |
| 112 | + |
| 113 | +SELECT '' AS one, f.* FROM FLOAT8_TBL f WHERE f.f1 = '1004.3'; |
| 114 | + one | f1 |
| 115 | +-----+-------- |
| 116 | + | 1004.3 |
| 117 | +(1 row) |
| 118 | + |
| 119 | +SELECT '' AS three, f.* FROM FLOAT8_TBL f WHERE '1004.3' > f.f1; |
| 120 | + three | f1 |
| 121 | +-------+---------------------- |
| 122 | + | 0 |
| 123 | + | -34.84 |
| 124 | + | 1.2345678901234e-200 |
| 125 | +(3 rows) |
| 126 | + |
| 127 | +SELECT '' AS three, f.* FROM FLOAT8_TBL f WHERE f.f1 < '1004.3'; |
| 128 | + three | f1 |
| 129 | +-------+---------------------- |
| 130 | + | 0 |
| 131 | + | -34.84 |
| 132 | + | 1.2345678901234e-200 |
| 133 | +(3 rows) |
| 134 | + |
| 135 | +SELECT '' AS four, f.* FROM FLOAT8_TBL f WHERE '1004.3' >= f.f1; |
| 136 | + four | f1 |
| 137 | +------+---------------------- |
| 138 | + | 0 |
| 139 | + | 1004.3 |
| 140 | + | -34.84 |
| 141 | + | 1.2345678901234e-200 |
| 142 | +(4 rows) |
| 143 | + |
| 144 | +SELECT '' AS four, f.* FROM FLOAT8_TBL f WHERE f.f1 <= '1004.3'; |
| 145 | + four | f1 |
| 146 | +------+---------------------- |
| 147 | + | 0 |
| 148 | + | 1004.3 |
| 149 | + | -34.84 |
| 150 | + | 1.2345678901234e-200 |
| 151 | +(4 rows) |
| 152 | + |
| 153 | +SELECT '' AS three, f.f1, f.f1 * '-10' AS x |
| 154 | + FROM FLOAT8_TBL f |
| 155 | + WHERE f.f1 > '0.0'; |
| 156 | + three | f1 | x |
| 157 | +-------+----------------------+----------------------- |
| 158 | + | 1004.3 | -10043 |
| 159 | + | 1.2345678901234e+200 | -1.2345678901234e+201 |
| 160 | + | 1.2345678901234e-200 | -1.2345678901234e-199 |
| 161 | +(3 rows) |
| 162 | + |
| 163 | +SELECT '' AS three, f.f1, f.f1 + '-10' AS x |
| 164 | + FROM FLOAT8_TBL f |
| 165 | + WHERE f.f1 > '0.0'; |
| 166 | + three | f1 | x |
| 167 | +-------+----------------------+---------------------- |
| 168 | + | 1004.3 | 994.3 |
| 169 | + | 1.2345678901234e+200 | 1.2345678901234e+200 |
| 170 | + | 1.2345678901234e-200 | -10 |
| 171 | +(3 rows) |
| 172 | + |
| 173 | +SELECT '' AS three, f.f1, f.f1 / '-10' AS x |
| 174 | + FROM FLOAT8_TBL f |
| 175 | + WHERE f.f1 > '0.0'; |
| 176 | + three | f1 | x |
| 177 | +-------+----------------------+----------------------- |
| 178 | + | 1004.3 | -100.43 |
| 179 | + | 1.2345678901234e+200 | -1.2345678901234e+199 |
| 180 | + | 1.2345678901234e-200 | -1.2345678901234e-201 |
| 181 | +(3 rows) |
| 182 | + |
| 183 | +SELECT '' AS three, f.f1, f.f1 - '-10' AS x |
| 184 | + FROM FLOAT8_TBL f |
| 185 | + WHERE f.f1 > '0.0'; |
| 186 | + three | f1 | x |
| 187 | +-------+----------------------+---------------------- |
| 188 | + | 1004.3 | 1014.3 |
| 189 | + | 1.2345678901234e+200 | 1.2345678901234e+200 |
| 190 | + | 1.2345678901234e-200 | 10 |
| 191 | +(3 rows) |
| 192 | + |
| 193 | +SELECT '' AS one, f.f1 ^ '2.0' AS square_f1 |
| 194 | + FROM FLOAT8_TBL f where f.f1 = '1004.3'; |
| 195 | + one | square_f1 |
| 196 | +-----+------------ |
| 197 | + | 1008618.49 |
| 198 | +(1 row) |
| 199 | + |
| 200 | +-- absolute value |
| 201 | +SELECT '' AS five, f.f1, @f.f1 AS abs_f1 |
| 202 | + FROM FLOAT8_TBL f; |
| 203 | + five | f1 | abs_f1 |
| 204 | +------+----------------------+---------------------- |
| 205 | + | 0 | 0 |
| 206 | + | 1004.3 | 1004.3 |
| 207 | + | -34.84 | 34.84 |
| 208 | + | 1.2345678901234e+200 | 1.2345678901234e+200 |
| 209 | + | 1.2345678901234e-200 | 1.2345678901234e-200 |
| 210 | +(5 rows) |
| 211 | + |
| 212 | +-- truncate |
| 213 | +SELECT '' AS five, f.f1, %f.f1 AS trunc_f1 |
| 214 | + FROM FLOAT8_TBL f; |
| 215 | + five | f1 | trunc_f1 |
| 216 | +------+----------------------+---------------------- |
| 217 | + | 0 | 0 |
| 218 | + | 1004.3 | 1004 |
| 219 | + | -34.84 | -34 |
| 220 | + | 1.2345678901234e+200 | 1.2345678901234e+200 |
| 221 | + | 1.2345678901234e-200 | 0 |
| 222 | +(5 rows) |
| 223 | + |
| 224 | +-- round |
| 225 | +SELECT '' AS five, f.f1, f.f1 % AS round_f1 |
| 226 | + FROM FLOAT8_TBL f; |
| 227 | + five | f1 | round_f1 |
| 228 | +------+----------------------+---------------------- |
| 229 | + | 0 | 0 |
| 230 | + | 1004.3 | 1004 |
| 231 | + | -34.84 | -35 |
| 232 | + | 1.2345678901234e+200 | 1.2345678901234e+200 |
| 233 | + | 1.2345678901234e-200 | 0 |
| 234 | +(5 rows) |
| 235 | + |
| 236 | +-- ceil / ceiling |
| 237 | +select ceil(f1) as ceil_f1 from float8_tbl f; |
| 238 | + ceil_f1 |
| 239 | +---------------------- |
| 240 | + 0 |
| 241 | + 1005 |
| 242 | + -34 |
| 243 | + 1.2345678901234e+200 |
| 244 | + 1 |
| 245 | +(5 rows) |
| 246 | + |
| 247 | +select ceiling(f1) as ceiling_f1 from float8_tbl f; |
| 248 | + ceiling_f1 |
| 249 | +---------------------- |
| 250 | + 0 |
| 251 | + 1005 |
| 252 | + -34 |
| 253 | + 1.2345678901234e+200 |
| 254 | + 1 |
| 255 | +(5 rows) |
| 256 | + |
| 257 | +-- floor |
| 258 | +select floor(f1) as floor_f1 from float8_tbl f; |
| 259 | + floor_f1 |
| 260 | +---------------------- |
| 261 | + 0 |
| 262 | + 1004 |
| 263 | + -35 |
| 264 | + 1.2345678901234e+200 |
| 265 | + 0 |
| 266 | +(5 rows) |
| 267 | + |
| 268 | +-- sign |
| 269 | +select sign(f1) as sign_f1 from float8_tbl f; |
| 270 | + sign_f1 |
| 271 | +--------- |
| 272 | + 0 |
| 273 | + 1 |
| 274 | + -1 |
| 275 | + 1 |
| 276 | + 1 |
| 277 | +(5 rows) |
| 278 | + |
| 279 | +-- square root |
| 280 | +SELECT sqrt(float8 '64') AS eight; |
| 281 | + eight |
| 282 | +------- |
| 283 | + 8 |
| 284 | +(1 row) |
| 285 | + |
| 286 | +SELECT |/ float8 '64' AS eight; |
| 287 | + eight |
| 288 | +------- |
| 289 | + 8 |
| 290 | +(1 row) |
| 291 | + |
| 292 | +SELECT '' AS three, f.f1, |/f.f1 AS sqrt_f1 |
| 293 | + FROM FLOAT8_TBL f |
| 294 | + WHERE f.f1 > '0.0'; |
| 295 | + three | f1 | sqrt_f1 |
| 296 | +-------+----------------------+----------------------- |
| 297 | + | 1004.3 | 31.6906926399535 |
| 298 | + | 1.2345678901234e+200 | 1.11111110611109e+100 |
| 299 | + | 1.2345678901234e-200 | 1.11111110611109e-100 |
| 300 | +(3 rows) |
| 301 | + |
| 302 | +-- power |
| 303 | +SELECT power(float8 '144', float8 '0.5'); |
| 304 | + power |
| 305 | +------- |
| 306 | + 12 |
| 307 | +(1 row) |
| 308 | + |
| 309 | +-- take exp of ln(f.f1) |
| 310 | +SELECT '' AS three, f.f1, exp(ln(f.f1)) AS exp_ln_f1 |
| 311 | + FROM FLOAT8_TBL f |
| 312 | + WHERE f.f1 > '0.0'; |
| 313 | + three | f1 | exp_ln_f1 |
| 314 | +-------+----------------------+----------------------- |
| 315 | + | 1004.3 | 1004.3 |
| 316 | + | 1.2345678901234e+200 | 1.23456789012338e+200 |
| 317 | + | 1.2345678901234e-200 | 1.23456789012339e-200 |
| 318 | +(3 rows) |
| 319 | + |
| 320 | +-- cube root |
| 321 | +SELECT ||/ float8 '27' AS three; |
| 322 | + three |
| 323 | +------- |
| 324 | + 3 |
| 325 | +(1 row) |
| 326 | + |
| 327 | +SELECT '' AS five, f.f1, ||/f.f1 AS cbrt_f1 FROM FLOAT8_TBL f; |
| 328 | + five | f1 | cbrt_f1 |
| 329 | +------+----------------------+---------------------- |
| 330 | + | 0 | 0 |
| 331 | + | 1004.3 | 10.014312837827 |
| 332 | + | -34.84 | -3.26607421344208 |
| 333 | + | 1.2345678901234e+200 | 4.97933859234765e+66 |
| 334 | + | 1.2345678901234e-200 | 2.3112042409018e-67 |
| 335 | +(5 rows) |
| 336 | + |
| 337 | +SELECT '' AS five, FLOAT8_TBL.*; |
| 338 | + five | f1 |
| 339 | +------+---------------------- |
| 340 | + | 0 |
| 341 | + | 1004.3 |
| 342 | + | -34.84 |
| 343 | + | 1.2345678901234e+200 |
| 344 | + | 1.2345678901234e-200 |
| 345 | +(5 rows) |
| 346 | + |
| 347 | +UPDATE FLOAT8_TBL |
| 348 | + SET f1 = FLOAT8_TBL.f1 * '-1' |
| 349 | + WHERE FLOAT8_TBL.f1 > '0.0'; |
| 350 | +SELECT '' AS bad, f.f1 * '1e200' from FLOAT8_TBL f; |
| 351 | +ERROR: type "double precision" value out of range: overflow |
| 352 | +SELECT '' AS bad, f.f1 ^ '1e200' from FLOAT8_TBL f; |
| 353 | +ERROR: result is out of range |
| 354 | +SELECT '' AS bad, ln(f.f1) from FLOAT8_TBL f where f.f1 = '0.0' ; |
| 355 | +ERROR: cannot take logarithm of zero |
| 356 | +SELECT '' AS bad, ln(f.f1) from FLOAT8_TBL f where f.f1 < '0.0' ; |
| 357 | +ERROR: cannot take logarithm of a negative number |
| 358 | +SELECT '' AS bad, exp(f.f1) from FLOAT8_TBL f; |
| 359 | +ERROR: result is out of range |
| 360 | +SELECT '' AS bad, f.f1 / '0.0' from FLOAT8_TBL f; |
| 361 | +ERROR: division by zero |
| 362 | +SELECT '' AS five, FLOAT8_TBL.*; |
| 363 | + five | f1 |
| 364 | +------+----------------------- |
| 365 | + | 0 |
| 366 | + | -34.84 |
| 367 | + | -1004.3 |
| 368 | + | -1.2345678901234e+200 |
| 369 | + | -1.2345678901234e-200 |
| 370 | +(5 rows) |
| 371 | + |
| 372 | +-- test for over- and underflow |
| 373 | +INSERT INTO FLOAT8_TBL(f1) VALUES ('10e400'); |
| 374 | +ERROR: "10e400" is out of range for type double precision |
| 375 | +INSERT INTO FLOAT8_TBL(f1) VALUES ('-10e400'); |
| 376 | +ERROR: "-10e400" is out of range for type double precision |
| 377 | +INSERT INTO FLOAT8_TBL(f1) VALUES ('10e-400'); |
| 378 | +INSERT INTO FLOAT8_TBL(f1) VALUES ('-10e-400'); |
| 379 | +-- maintain external table consistency across platforms |
| 380 | +-- delete all values and reinsert well-behaved ones |
| 381 | +DELETE FROM FLOAT8_TBL; |
| 382 | +INSERT INTO FLOAT8_TBL(f1) VALUES ('0.0'); |
| 383 | +INSERT INTO FLOAT8_TBL(f1) VALUES ('-34.84'); |
| 384 | +INSERT INTO FLOAT8_TBL(f1) VALUES ('-1004.30'); |
| 385 | +INSERT INTO FLOAT8_TBL(f1) VALUES ('-1.2345678901234e+200'); |
| 386 | +INSERT INTO FLOAT8_TBL(f1) VALUES ('-1.2345678901234e-200'); |
| 387 | +SELECT '' AS five, FLOAT8_TBL.*; |
| 388 | + five | f1 |
| 389 | +------+----------------------- |
| 390 | + | 0 |
| 391 | + | -34.84 |
| 392 | + | -1004.3 |
| 393 | + | -1.2345678901234e+200 |
| 394 | + | -1.2345678901234e-200 |
| 395 | +(5 rows) |
| 396 | + |