summaryrefslogtreecommitdiff
path: root/test/fpconv1.test
blob: 245d526be2644a6c85c79973fe69e87684cc9dcc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# 2023-07-03
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# 
# This file contains a test that attempts to verify the claim that the
# floatpoint-to-text conversion routines built into SQLite maintain at
# least 15 significant digits of accuracy.
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl


# Unusual rendering cases:
#
do_execsql_test fpconv1-1.0 {
  SELECT 1.23 - 2.34;
} {-1.1099999999999999}
#  ^---  Not -1.11 as you would expect.  -1.11 has a different bit pattern

do_execsql_test fpconv1-1.1 {
  SELECT 1.23 * 2.34;
} {2.8781999999999996}
#  ^---  Not 2.8782 as you would expect.  2.8782 has a different bit pattern

# Change significant digits to 15 and get a different result.
sqlite3_test_control SQLITE_TESTCTRL_FP_DIGITS db 15
do_execsql_test fpconv1-1.2 {
  SELECT 1.23 - 2.34;
} {-1.11}
do_execsql_test fpconv1-1.3 {
  SELECT 1.23 * 2.34;
} {2.8782}
sqlite3_test_control SQLITE_TESTCTRL_FP_DIGITS db 17

if {[catch {load_static_extension db decimal} error]} {
  puts "Skipping decimal tests, hit load error: $error"
  finish_test; return
}

sqlite3_create_function db
do_execsql_test fpconv1-2.0 {
  WITH RECURSIVE
       /* Number of random floating-point values to try.
       ** On a circa 2021 Ryzen 5950X running Mint Linux, and
       ** compiled with -O0 -DSQLITE_DEBUG, this test runs at
       ** about 150000 cases per second  ------------------vvvvvvv */
    c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<500_000),
    fp(y) AS MATERIALIZED (
       SELECT CAST( format('%+d.%019d0e%+03d',
                           random()%10,abs(random()),random()%200) AS real)
        FROM c
    )
  SELECT y FROM fp
   WHERE -log10(abs(decimal_sub(dtostr(y,24),format('%!.24e',y))/y))<17.0;
                     /* Number of digits of accuracy required -------^^^^ */
} {}
#  ^---- Expect a empty set as the result.  The output is all tested numbers
#        that fail to preserve at least 16 significant digits of accuracy.

########################################################################
# Random test to ensure that double -> text -> double conversions
# round-trip exactly.
#

load_static_extension db ieee754

do_execsql_test fpconv1-3.0 {
  WITH RECURSIVE
    c(x,s) AS MATERIALIZED (VALUES(1,random()&0xffefffffffffffff)
               UNION ALL
               SELECT x+1,random()&0xffefffffffffffff
                 FROM c WHERE x<1_000_000),
    fp(y,s) AS (
       SELECT ieee754_from_int(s),s FROM c
    ),
    fp2(yt,y,s) AS (
       SELECT y||'', y, s FROM fp
    )
  SELECT format('%#016x',s) aS 'orig-hex',
         format('%#016x',ieee754_to_int(CAST(yt AS real))) AS 'full-roundtrip',
         yt AS 'rendered-as',
         decimal_exp(yt,30) AS 'text-decimal',
         decimal_exp(ieee754_from_int(s),30) AS 'float-decimal'
    FROM fp2
   WHERE ieee754_to_int(CAST(yt AS real))<>s;
} {}
#  ^---- Values that fail to round-trip will be reported

# 2026-10-18 https://sqlite.org/forum/forumpost/98c37a671c
# Signed integer overflow in sqlite3FpDecode() when the debugging-only
# fpdecode() SQL function is given a very negative precision.
#
ifcapable debug {
  do_execsql_test fpconv1-4.1 {
    SELECT fpdecode(1.5, -2147483648, 10);
  } {+15/1}
  do_execsql_test fpconv1-4.2 {
    SELECT fpdecode(1.5e300, -2147483600, 10),
           fpdecode(1.5e300, -100000000, 10);
  } {+15/301 +15/301}
  do_execsql_test fpconv1-4.3 {
    SELECT fpdecode(0.1, -2147483648, 16),
           fpdecode(0.1, -2147483647, 16),
           fpdecode(0.1, -100000001, 16),
           fpdecode(0.1, -1000, 16);
  } {+1/0 +1/0 +1/0 +1/0}
  do_execsql_test fpconv1-4.4 {
    SELECT fpdecode(-1.0e-300, -2147483648, 5);
  } {-1/-299}
}

finish_test