xref: /sqlite-3.40.0/test/atof1.test (revision 72b3fbc7)
189f15088Sdrh# 2012 June 18
289f15088Sdrh#
389f15088Sdrh# The author disclaims copyright to this source code.  In place of
489f15088Sdrh# a legal notice, here is a blessing:
589f15088Sdrh#
689f15088Sdrh#    May you do good and not evil.
789f15088Sdrh#    May you find forgiveness for yourself and forgive others.
889f15088Sdrh#    May you share freely, never taking more than you give.
989f15088Sdrh#
1089f15088Sdrh#***********************************************************************
1189f15088Sdrh#
1289f15088Sdrh# Tests of the sqlite3AtoF() function.
1389f15088Sdrh#
1489f15088Sdrh
1589f15088Sdrhset testdir [file dirname $argv0]
1689f15088Sdrhsource $testdir/tester.tcl
1789f15088Sdrh
1889f15088Sdrhexpr srand(1)
1989f15088Sdrhfor {set i 1} {$i<10000} {incr i} {
2089f15088Sdrh  set pow [expr {int((rand()-0.5)*100)}]
2189f15088Sdrh  set x [expr {pow((rand()-0.5)*2*rand(),$pow)}]
22*72b3fbc7Sdrh  set xf [format %.32e $x]
23*72b3fbc7Sdrh
24*72b3fbc7Sdrh  # Verify that text->real conversions get exactly same ieee754 floating-
25*72b3fbc7Sdrh  # point value in SQLite as they do in TCL.
26*72b3fbc7Sdrh  #
27*72b3fbc7Sdrh  do_test 1.$i.1 {
2889f15088Sdrh    set y [db eval "SELECT $xf=\$x"]
2989f15088Sdrh    if {!$y} {
3089f15088Sdrh      puts -nonewline \173[db eval "SELECT real2hex($xf), real2hex(\$x)"]\175
3189f15088Sdrh      db eval "SELECT $xf+0.0 AS a, \$x AS b" {
3289f15088Sdrh        puts [format "\n%.60e\n%.60e\n%.60e" $x $a $b]
3389f15088Sdrh      }
3489f15088Sdrh    }
3589f15088Sdrh    set y
3689f15088Sdrh  } {1}
37*72b3fbc7Sdrh
38*72b3fbc7Sdrh  # FIX ME.  The following tests (real->text->real round-trips) sometimes
39*72b3fbc7Sdrh  # fail for large floating-point values.  Skip them for now...
40*72b3fbc7Sdrh  #
41*72b3fbc7Sdrh  if {abs($x)>1e50} continue
42*72b3fbc7Sdrh
43*72b3fbc7Sdrh  # Verify that round-trip real->text->real conversions using the quote()
44*72b3fbc7Sdrh  # function preserve the bits of the numeric value exactly.
45*72b3fbc7Sdrh  #
46*72b3fbc7Sdrh  do_test 1.$i.2 {
47*72b3fbc7Sdrh    set y [db eval {SELECT $x=CAST(quote($x) AS real)}]
48*72b3fbc7Sdrh    if {!$y} {
49*72b3fbc7Sdrh      db eval {SELECT real2hex($x) a, real2hex(CAST(quote($x) AS real)) b} {}
50*72b3fbc7Sdrh      puts "\nIN:    $a $xf"
51*72b3fbc7Sdrh      puts [format {QUOTE: %16s %s} {} [db eval {SELECT quote($x)}]]
52*72b3fbc7Sdrh      db eval {SELECT CAST(quote($x) AS real) c} {}
53*72b3fbc7Sdrh      puts "OUT:   $b [format %.32e $c]"
54*72b3fbc7Sdrh    }
55*72b3fbc7Sdrh    set y
56*72b3fbc7Sdrh  } {1}
5789f15088Sdrh}
5889f15088Sdrh
5989f15088Sdrh
6089f15088Sdrhfinish_test
61