3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
15 set testprefix decimal
17 if {[catch {load_static_extension db decimal} error]} {
18 puts "Skipping decimal tests, hit load error: $error"
22 do_execsql_test 1000 {
25 do_execsql_test 1010 {
26 SELECT decimal('1.0');
28 do_execsql_test 1020 {
29 SELECT decimal('0001.0');
31 do_execsql_test 1030 {
32 SELECT decimal('+0001.0');
34 do_execsql_test 1040 {
35 SELECT decimal('-0001.0');
37 do_execsql_test 1050 {
38 SELECT decimal('1.0e72');
39 } {1000000000000000000000000000000000000000000000000000000000000000000000000}
40 # 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123
41 do_execsql_test 1060 {
42 SELECT decimal('1.0e-72');
43 } {0.0000000000000000000000000000000000000000000000000000000000000000000000010}
44 # 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123
45 do_execsql_test 1070 {
46 SELECT decimal('-123e-4');
48 do_execsql_test 1080 {
49 SELECT decimal('+123e+4');
51 do_execsql_test 1081 {
52 SELECT decimal_exp('+123e+4');
56 do_execsql_test 2000 {
57 CREATE TABLE t1(seq INTEGER PRIMARY KEY, val TEXT);
68 (10,'1.00000000000000001'),
74 AND decimal_cmp(a.val,b.val)>=0;
76 do_execsql_test 2010 {
80 AND decimal_cmp(a.val,b.val)==0;
82 do_execsql_test 2020 {
86 AND decimal_cmp(a.val,b.val)<=0;
88 do_execsql_test 2030 {
89 SELECT seq FROM t1 ORDER BY val COLLATE decimal;
90 } {1 2 3 4 5 6 7 8 9 10 11 12}
91 do_execsql_test 2040 {
92 SELECT seq FROM t1 ORDER BY val COLLATE decimal DESC;
93 } {12 11 10 9 8 7 6 5 4 3 2 1}
95 do_execsql_test 3000 {
96 CREATE TABLE t3(seq INTEGER PRIMARY KEY, val TEXT);
97 WITH RECURSIVE c(x) AS (VALUES(1) UNION SELECT x+1 FROM c WHERE x<10)
98 INSERT INTO t3(seq, val) SELECT x, x FROM c;
99 WITH RECURSIVE c(x) AS (VALUES(1) UNION SELECT x+1 FROM c WHERE x<5)
100 INSERT INTO t3(seq, val) SELECT x+10, x*1000 FROM c;
101 SELECT decimal(val) FROM t3 ORDER BY seq;
102 } {1 2 3 4 5 6 7 8 9 10 1000 2000 3000 4000 5000}
103 do_execsql_test 3020 {
104 SELECT decimal_add(val,'0.5') FROM t3 WHERE seq>5 ORDER BY seq
105 } {6.5 7.5 8.5 9.5 10.5 1000.5 2000.5 3000.5 4000.5 5000.5}
106 do_execsql_test 3030 {
107 SELECT decimal_add(val,'-10') FROM t3 ORDER BY seq;
108 } {-9 -8 -7 -6 -5 -4 -3 -2 -1 0 990 1990 2990 3990 4990}
110 do_execsql_test 4000 {
111 SELECT decimal_sum(val) FROM t3;
113 do_execsql_test 4010 {
114 SELECT decimal_sum(decimal_add(val,val||'e+10')) FROM t3;
116 do_execsql_test 4010 {
117 SELECT decimal_sum(decimal_add(val||'e+20',decimal_add(val,val||'e-20')))
119 } {1505500000000000000015055.00000000000000015055}
121 do_execsql_test 5000 {
122 WITH RECURSIVE c(x,y,z) AS (
125 SELECT x+1, decimal_mul(y,'2'), decimal_mul(z,'0.5')
128 SELECT count(*) FROM c WHERE decimal_mul(y,z)='1';
131 do_execsql_test 5100 {
132 SELECT decimal_mul('1234.00','2.00');
134 do_execsql_test 5101 {
135 SELECT decimal_mul('1234.00','2.0000');
137 do_execsql_test 5102 {
138 SELECT decimal_mul('1234.0000','2.000');
140 do_execsql_test 5103 {
141 SELECT decimal_mul('1234.0000','2');
144 if {[catch {load_static_extension db ieee754} error]} {
145 puts "Skipping ieee754 tests, hit load error: $error"
149 do_execsql_test 6000 {
150 CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT);
151 WITH RECURSIVE c(x,v) AS (
154 SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971
155 ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
156 WITH RECURSIVE c(x,v) AS (
159 SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075
160 ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
162 do_execsql_test 6010 {
163 WITH c(n) AS (SELECT ieee754_from_blob(x'0000000000000001'))
164 SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v)
165 FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n);
166 } {0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004940656458412465441765687928682213723650598026143247644255856825006755072702087518652998363616359923797965646954457177309266567103559397963987747960107818781263007131903114045278458171678489821036887186360569987307230500063874091535649843873124733972731696151400317153853980741262385655911710266585566867681870395603106249319452715914924553293054565444011274801297099995419319894090804165633245247571478690147267801593552386115501348035264934720193790268107107491703332226844753335720832431936092382893458368060106011506169809753078342277318329247904982524730776375927247874656084778203734469699533647017972677717585125660551199131504891101451037862738167250955837389733598993664809941164205702637090279242767544565229087538682506419718265533447265625}
167 do_execsql_test 6011 {
168 WITH c(n) AS (SELECT ieee754_from_blob(x'0000000000000001'))
169 SELECT decimal(c.n) FROM c;
170 } {0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004940656458412465441765687928682213723650598026143247644255856825006755072702087518652998363616359923797965646954457177309266567103559397963987747960107818781263007131903114045278458171678489821036887186360569987307230500063874091535649843873124733972731696151400317153853980741262385655911710266585566867681870395603106249319452715914924553293054565444011274801297099995419319894090804165633245247571478690147267801593552386115501348035264934720193790268107107491703332226844753335720832431936092382893458368060106011506169809753078342277318329247904982524730776375927247874656084778203734469699533647017972677717585125660551199131504891101451037862738167250955837389733598993664809941164205702637090279242767544565229087538682506419718265533447265625}
171 do_execsql_test 6020 {
172 WITH c(n) AS (SELECT ieee754_from_blob(x'7fefffffffffffff'))
173 SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v)
174 FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n);
175 } {179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368}
177 do_execsql_test 6100 {
178 SELECT ieee754(ieee754_from_blob(x'0000000000000001'));
180 do_execsql_test 6110 {
181 SELECT ieee754(ieee754_from_blob(x'7fefffffffffffff'));
182 } {ieee754(9007199254740991,971)}
183 do_execsql_test 6120 {
184 SELECT printf('%.8e',ieee754_from_blob(x'0000000000000001'));
186 do_execsql_test 6130 {
187 SELECT printf('%.8e',ieee754_from_blob(x'ffefffffffffffff'));