Debian Codegraph @

test/fpconv1.test

raw
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
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
19
20
21 # Unusual rendering cases:
22 #
23 do_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
28 do_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.
34 sqlite3_db_config db FP_DIGITS 15
35 do_execsql_test fpconv1-1.2 {
36 SELECT 1.23 - 2.34;
37 } {-1.11}
38 do_execsql_test fpconv1-1.3 {
39 SELECT 1.23 * 2.34;
40 } {2.8782}
41 sqlite3_db_config db FP_DIGITS 17
42
43
44 if {[catch {load_static_extension db decimal} error]} {
45 puts "Skipping decimal tests, hit load error: $error"
46 finish_test; return
47 }
48
49 sqlite3_create_function db
50 do_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
74 load_static_extension db ieee754
75
76 do_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 finish_test