float8.slt 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. # Copyright 1994, Regents of the University of California.
  2. # Copyright 1996-2019 PostgreSQL Global Development Group.
  3. # Copyright Materialize, Inc. and contributors. All rights reserved.
  4. #
  5. # Use of this software is governed by the Business Source License
  6. # included in the LICENSE file at the root of this repository.
  7. #
  8. # As of the Change Date specified in that file, in accordance with
  9. # the Business Source License, use of this software will be governed
  10. # by the Apache License, Version 2.0.
  11. #
  12. # This file is derived from the regression test suite in PostgreSQL.
  13. # The original file was retrieved on February 10, 2021 from:
  14. #
  15. # https://github.com/postgres/postgres/blob/64990081504661ff5c04dbf20cc4252be66ab149/src/test/regress/expected/float8.out
  16. #
  17. # The original source code is subject to the terms of the PostgreSQL
  18. # license, a copy of which can be found in the LICENSE file at the
  19. # root of this repository.
  20. mode cockroach
  21. statement ok
  22. CREATE TABLE float8_tbl (f1 float8)
  23. statement ok
  24. INSERT INTO float8_tbl (f1) VALUES (' 0.0 '), ('1004.30 '), (' -34.84'), ('1.2345678901234e+200'), ('1.2345678901234e-200')
  25. # test for underflow and overflow handling
  26. query error "10e400" is out of range for type double precision
  27. SELECT '10e400'::float8
  28. query error "-10e400" is out of range for type double precision
  29. SELECT '-10e400'::float8
  30. query error "10e-400" is out of range for type double precision
  31. SELECT '10e-400'::float8
  32. query error "-10e-400" is out of range for type double precision
  33. SELECT '-10e-400'::float8
  34. # bad input
  35. query error invalid input syntax for type double precision: ""
  36. SELECT ''::float8
  37. query error invalid input syntax for type double precision: " "
  38. SELECT ' '::float8
  39. query error invalid input syntax for type double precision: "xyz"
  40. SELECT 'xyz'::float8
  41. query error invalid input syntax for type double precision: "5.0.0"
  42. SELECT '5.0.0'::float8
  43. query error invalid input syntax for type double precision: "5 . 0"
  44. SELECT '5 . 0'::float8
  45. query error invalid input syntax for type double precision: "5. 0"
  46. SELECT '5. 0'::float8
  47. query error invalid input syntax for type double precision: " - 3"
  48. SELECT ' - 3'::float8
  49. query error invalid input syntax for type double precision: "123 5"
  50. SELECT '123 5'::float8
  51. # special inputs
  52. query T
  53. SELECT 'NaN'::float8::text
  54. ----
  55. NaN
  56. query T
  57. SELECT 'nan'::float8::text
  58. ----
  59. NaN
  60. query T
  61. SELECT ' NAN '::float8::text
  62. ----
  63. NaN
  64. query T
  65. SELECT 'infinity'::float8::text
  66. ----
  67. Infinity
  68. query T
  69. SELECT ' -INFINiTY '::float8::text
  70. ----
  71. -Infinity
  72. # bad special inputs
  73. query error invalid input syntax for type double precision: "N A N"
  74. SELECT 'N A N'::float8::text
  75. query error invalid input syntax for type double precision: "NaN x"
  76. SELECT 'NaN x'::float8;
  77. query error invalid input syntax for type double precision: " INFINITY x"
  78. SELECT ' INFINITY x'::float8
  79. query T
  80. SELECT ('Infinity'::float8 + 100.0)::text
  81. ----
  82. Infinity
  83. query T
  84. SELECT ('Infinity'::float8 / 'Infinity'::float8)::text
  85. ----
  86. NaN
  87. query R
  88. SELECT '42'::float8 / 'Infinity'::float8
  89. ----
  90. 0
  91. query T
  92. SELECT ('nan'::float8 / 'nan'::float8)::text
  93. ----
  94. NaN
  95. query T
  96. SELECT ('nan'::float8 / '0'::float8)::text
  97. ----
  98. NaN
  99. # TODO(benesch): re-enable when the numeric type supports NaN.
  100. #
  101. # query T
  102. # SELECT 'nan'::numeric::float8
  103. # ----
  104. # NaN
  105. query T rowsort
  106. SELECT f1::text FROM float8_tbl
  107. ----
  108. 0
  109. 1004.3
  110. -34.84
  111. 1.2345678901234e+200
  112. 1.2345678901234e-200
  113. query T rowsort
  114. SELECT f.f1::text FROM float8_tbl f WHERE f.f1 <> '1004.3'
  115. ----
  116. 0
  117. -34.84
  118. 1.2345678901234e+200
  119. 1.2345678901234e-200
  120. query T
  121. SELECT f.f1::text FROM float8_tbl f WHERE f.f1 = '1004.3'
  122. ----
  123. 1004.3
  124. query T rowsort
  125. SELECT f.f1::text FROM float8_tbl f WHERE '1004.3' > f.f1
  126. ----
  127. 0
  128. -34.84
  129. 1.2345678901234e-200
  130. query T rowsort
  131. SELECT f.f1::text FROM float8_tbl f WHERE f.f1 < '1004.3'
  132. ----
  133. 0
  134. -34.84
  135. 1.2345678901234e-200
  136. query T rowsort
  137. SELECT f.f1::text FROM float8_tbl f WHERE '1004.3' >= f.f1
  138. ----
  139. 0
  140. 1004.3
  141. -34.84
  142. 1.2345678901234e-200
  143. query T rowsort
  144. SELECT f.f1::text FROM float8_tbl f WHERE f.f1 <= '1004.3'
  145. ----
  146. 0
  147. 1004.3
  148. -34.84
  149. 1.2345678901234e-200
  150. query TT rowsort
  151. SELECT f.f1::text, (f.f1 * '-10')::text AS x FROM float8_tbl f WHERE f.f1 > '0.0'
  152. ----
  153. 1004.3 -10043
  154. 1.2345678901234e+200 -1.2345678901234e+201
  155. 1.2345678901234e-200 -1.2345678901234e-199
  156. query TT rowsort
  157. SELECT f.f1::text, (f.f1 + '-10')::text AS x FROM float8_tbl f WHERE f.f1 > '0.0'
  158. ----
  159. 1004.3 994.3
  160. 1.2345678901234e+200 1.2345678901234e+200
  161. 1.2345678901234e-200 -10
  162. query TT rowsort
  163. SELECT f.f1::text, (f.f1 / '-10')::text AS x FROM float8_tbl f WHERE f.f1 > '0.0'
  164. ----
  165. 1004.3 -100.42999999999999
  166. 1.2345678901234e+200 -1.2345678901234e+199
  167. 1.2345678901234e-200 -1.2345678901234e-201
  168. query TT rowsort
  169. SELECT f.f1::text, (f.f1 - '-10')::text AS x FROM float8_tbl f WHERE f.f1 > '0.0';
  170. ----
  171. 1004.3 1014.3
  172. 1.2345678901234e+200 1.2345678901234e+200
  173. 1.2345678901234e-200 10
  174. # -- round
  175. query TT rowsort
  176. SELECT f.f1::text, round(f.f1)::text AS round_f1 FROM float8_tbl f
  177. ----
  178. 0 0
  179. 1004.3 1004
  180. -34.84 -35
  181. 1.2345678901234e+200 1.2345678901234e+200
  182. 1.2345678901234e-200 0
  183. # ceil / ceiling
  184. query T rowsort
  185. select ceil(f1)::text as ceil_f1 from float8_tbl f
  186. ----
  187. 0
  188. 1005
  189. -34
  190. 1.2345678901234e+200
  191. 1
  192. query T rowsort
  193. select ceiling(f1)::text as ceiling_f1 from float8_tbl f;
  194. ----
  195. 0
  196. 1005
  197. -34
  198. 1.2345678901234e+200
  199. 1
  200. # floor
  201. query T rowsort
  202. select floor(f1)::text as floor_f1 from float8_tbl f
  203. ----
  204. 0
  205. 1004
  206. -35
  207. 1.2345678901234e+200
  208. 0
  209. # TODO(benesch): support sign.
  210. #
  211. # -- sign
  212. # select sign(f1) as sign_f1 from float8_tbl f;
  213. # sign_f1
  214. # ---------
  215. # 0
  216. # 1
  217. # -1
  218. # 1
  219. # 1
  220. # (5 rows)
  221. # test for over- and underflow
  222. query error "10e400" is out of range for type double precision
  223. SELECT '10e400'::float8
  224. query error "-10e400" is out of range for type double precision
  225. SELECT '-10e400'::float8
  226. query error "10e-400" is out of range for type double precision
  227. SELECT '10e-400'::float8
  228. query error "-10e-400" is out of range for type double precision
  229. SELECT '-10e-400'::float8
  230. # test edge-case coercions to integer
  231. query I
  232. SELECT '32767.4'::float8::int2;
  233. ----
  234. 32767
  235. query error smallint out of range
  236. SELECT '32767.6'::float8::int2;
  237. query I
  238. SELECT '-32768.4'::float8::int2;
  239. ----
  240. -32768
  241. query error smallint out of range
  242. SELECT '-32768.6'::float8::int2;
  243. query I
  244. SELECT '2147483647.4'::float8::int4;
  245. ----
  246. 2147483647
  247. query error "2147483648" integer out of range
  248. SELECT '2147483647.6'::float8::int4
  249. query I
  250. SELECT '-2147483648.4'::float8::int4
  251. ----
  252. -2147483648
  253. query error "-2147483649" integer out of range
  254. SELECT '-2147483648.6'::float8::int4
  255. query I
  256. SELECT '9223372036854773760'::float8::int8
  257. ----
  258. 9223372036854773760
  259. query error "9223372036854776000" bigint out of range
  260. SELECT '9223372036854775807'::float8::int8
  261. query I
  262. SELECT '-9223372036854775808.5'::float8::int8
  263. ----
  264. -9223372036854775808
  265. query error "-9223372036854780000" bigint out of range
  266. SELECT '-9223372036854780000'::float8::int8
  267. query error "922337203685477600000" bigint out of range
  268. SELECT '922337203685477580700.0'::float8::int8
  269. query RI rowsort
  270. SELECT x, x::int4 AS int4_value
  271. FROM (VALUES (-2.5::float8),
  272. (-1.5::float8),
  273. (-0.5::float8),
  274. (0.0::float8),
  275. (0.5::float8),
  276. (1.5::float8),
  277. (2.5::float8)) t(x);
  278. ----
  279. -2.5 -2
  280. -1.5 -2
  281. -0.5 0
  282. 0 0
  283. 0.5 0
  284. 1.5 2
  285. 2.5 2
  286. query RI rowsort
  287. SELECT x, x::int8 AS int8_value
  288. FROM (VALUES (-2.5::float8),
  289. (-1.5::float8),
  290. (-0.5::float8),
  291. (0.0::float8),
  292. (0.5::float8),
  293. (1.5::float8),
  294. (2.5::float8)) t(x);
  295. ----
  296. -2.5 -2
  297. -1.5 -2
  298. -0.5 0
  299. 0 0
  300. 0.5 0
  301. 1.5 2
  302. 2.5 2
  303. query T
  304. SELECT 4567890123456789::int8::float8::text
  305. ----
  306. 4567890123456789