interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / test / fpconv1.test

121 lines · 3958 bytes Raw Blame History
1# 2023-07-03
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
11#
12# This file contains a test that attempts to verify the claim that the
13# floatpoint-to-text conversion routines built into SQLite maintain at
14# least 15 significant digits of accuracy.
15#
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20
21# Unusual rendering cases:
22#
23do_execsql_test fpconv1-1.0 {
24 SELECT 1.23 - 2.34;
25} {-1.1099999999999999}
26# ^--- Not -1.11 as you would expect. -1.11 has a different bit pattern
27
28do_execsql_test fpconv1-1.1 {
29 SELECT 1.23 * 2.34;
30} {2.8781999999999996}
31# ^--- Not 2.8782 as you would expect. 2.8782 has a different bit pattern
32
33# Change significant digits to 15 and get a different result.
34sqlite3_db_config db FP_DIGITS 15
35do_execsql_test fpconv1-1.2 {
36 SELECT 1.23 - 2.34;
37} {-1.11}
38do_execsql_test fpconv1-1.3 {
39 SELECT 1.23 * 2.34;
40} {2.8782}
41sqlite3_db_config db FP_DIGITS 17
42
43
44if {[catch {load_static_extension db decimal} error]} {
45 puts "Skipping decimal tests, hit load error: $error"
46 finish_test; return
47}
48
49sqlite3_create_function db
50do_execsql_test fpconv1-2.0 {
51 WITH RECURSIVE
52 /* Number of random floating-point values to try.
53 ** On a circa 2021 Ryzen 5950X running Mint Linux, and
54 ** compiled with -O0 -DSQLITE_DEBUG, this test runs at
55 ** about 150000 cases per second ------------------vvvvvvv */
56 c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<500_000),
57 fp(y) AS MATERIALIZED (
58 SELECT CAST( format('%+d.%019d0e%+03d',
59 random()%10,abs(random()),random()%200) AS real)
60 FROM c
61 )
62 SELECT y FROM fp
63 WHERE -log10(abs(decimal_sub(dtostr(y,24),format('%!.24e',y))/y))<17.0;
64 /* Number of digits of accuracy required -------^^^^ */
65} {}
66# ^---- Expect a empty set as the result. The output is all tested numbers
67# that fail to preserve at least 16 significant digits of accuracy.
68
69########################################################################
70# Random test to ensure that double -> text -> double conversions
71# round-trip exactly.
72#
73
74load_static_extension db ieee754
75
76do_execsql_test fpconv1-3.0 {
77 WITH RECURSIVE
78 c(x,s) AS MATERIALIZED (VALUES(1,random()&0xffefffffffffffff)
79 UNION ALL
80 SELECT x+1,random()&0xffefffffffffffff
81 FROM c WHERE x<1_000_000),
82 fp(y,s) AS (
83 SELECT ieee754_from_int(s),s FROM c
84 ),
85 fp2(yt,y,s) AS (
86 SELECT y||'', y, s FROM fp
87 )
88 SELECT format('%#016x',s) aS 'orig-hex',
89 format('%#016x',ieee754_to_int(CAST(yt AS real))) AS 'full-roundtrip',
90 yt AS 'rendered-as',
91 decimal_exp(yt,30) AS 'text-decimal',
92 decimal_exp(ieee754_from_int(s),30) AS 'float-decimal'
93 FROM fp2
94 WHERE ieee754_to_int(CAST(yt AS real))<>s;
95} {}
96# ^---- Values that fail to round-trip will be reported
97
98# 2026-10-18 https://sqlite.org/forum/forumpost/98c37a671c
99# Signed integer overflow in sqlite3FpDecode() when the debugging-only
100# fpdecode() SQL function is given a very negative precision.
101#
102ifcapable debug {
103 do_execsql_test fpconv1-4.1 {
104 SELECT fpdecode(1.5, -2147483648, 10);
105 } {+15/1}
106 do_execsql_test fpconv1-4.2 {
107 SELECT fpdecode(1.5e300, -2147483600, 10),
108 fpdecode(1.5e300, -100000000, 10);
109 } {+15/301 +15/301}
110 do_execsql_test fpconv1-4.3 {
111 SELECT fpdecode(0.1, -2147483648, 16),
112 fpdecode(0.1, -2147483647, 16),
113 fpdecode(0.1, -100000001, 16),
114 fpdecode(0.1, -1000, 16);
115 } {+1/0 +1/0 +1/0 +1/0}
116 do_execsql_test fpconv1-4.4 {
117 SELECT fpdecode(-1.0e-300, -2147483648, 5);
118 } {-1/-299}
119}
120
121finish_test