@@ -18,7 +18,6 @@ import (
1818 gosql "database/sql"
1919 "flag"
2020 "fmt"
21- "math"
2221 "math/rand"
2322 "net"
2423 "net/url"
@@ -3676,9 +3675,9 @@ func (t *logicTest) finishExecQuery(query logicQuery, rows *gosql.Rows, err erro
36763675 // ('R') coltypes are approximately equal to take into account
36773676 // platform differences in floating point calculations.
36783677 if runtime .GOARCH == "s390x" && (colT == 'F' || colT == 'R' ) {
3679- resultMatches , err = floatsMatchApprox (expected , actual )
3678+ resultMatches , err = floatcmp . FloatsMatchApprox (expected , actual )
36803679 } else if colT == 'F' {
3681- resultMatches , err = floatsMatch (expected , actual )
3680+ resultMatches , err = floatcmp . FloatsMatch (expected , actual )
36823681 }
36833682 if err != nil {
36843683 return errors .CombineErrors (makeError (), err )
@@ -3746,93 +3745,6 @@ func (t *logicTest) finishExecQuery(query logicQuery, rows *gosql.Rows, err erro
37463745 return nil
37473746}
37483747
3749- // parseExpectedAndActualFloats converts the strings expectedString and
3750- // actualString to float64 values.
3751- func parseExpectedAndActualFloats (expectedString , actualString string ) (float64 , float64 , error ) {
3752- expected , err := strconv .ParseFloat (expectedString , 64 /* bitSize */ )
3753- if err != nil {
3754- return 0 , 0 , errors .Wrap (err , "when parsing expected" )
3755- }
3756- actual , err := strconv .ParseFloat (actualString , 64 /* bitSize */ )
3757- if err != nil {
3758- return 0 , 0 , errors .Wrap (err , "when parsing actual" )
3759- }
3760- return expected , actual , nil
3761- }
3762-
3763- // floatsMatchApprox returns whether two floating point represented as
3764- // strings are equal within a tolerance.
3765- func floatsMatchApprox (expectedString , actualString string ) (bool , error ) {
3766- expected , actual , err := parseExpectedAndActualFloats (expectedString , actualString )
3767- if err != nil {
3768- return false , err
3769- }
3770- return floatcmp .EqualApprox (expected , actual , floatcmp .CloseFraction , floatcmp .CloseMargin ), nil
3771- }
3772-
3773- // floatsMatch returns whether two floating point numbers represented as
3774- // strings have matching 15 significant decimal digits (this is the precision
3775- // that Postgres supports for 'double precision' type).
3776- func floatsMatch (expectedString , actualString string ) (bool , error ) {
3777- expected , actual , err := parseExpectedAndActualFloats (expectedString , actualString )
3778- if err != nil {
3779- return false , err
3780- }
3781- // Check special values - NaN, +Inf, -Inf, 0.
3782- if math .IsNaN (expected ) || math .IsNaN (actual ) {
3783- return math .IsNaN (expected ) == math .IsNaN (actual ), nil
3784- }
3785- if math .IsInf (expected , 0 /* sign */ ) || math .IsInf (actual , 0 /* sign */ ) {
3786- bothNegativeInf := math .IsInf (expected , - 1 /* sign */ ) == math .IsInf (actual , - 1 /* sign */ )
3787- bothPositiveInf := math .IsInf (expected , 1 /* sign */ ) == math .IsInf (actual , 1 /* sign */ )
3788- return bothNegativeInf || bothPositiveInf , nil
3789- }
3790- if expected == 0 || actual == 0 {
3791- return expected == actual , nil
3792- }
3793- // Check that the numbers have the same sign.
3794- if expected * actual < 0 {
3795- return false , nil
3796- }
3797- expected = math .Abs (expected )
3798- actual = math .Abs (actual )
3799- // Check that 15 significant digits match. We do so by normalizing the
3800- // numbers and then checking one digit at a time.
3801- //
3802- // normalize converts f to base * 10**power representation where base is in
3803- // [1.0, 10.0) range.
3804- normalize := func (f float64 ) (base float64 , power int ) {
3805- for f >= 10 {
3806- f = f / 10
3807- power ++
3808- }
3809- for f < 1 {
3810- f *= 10
3811- power --
3812- }
3813- return f , power
3814- }
3815- var expPower , actPower int
3816- expected , expPower = normalize (expected )
3817- actual , actPower = normalize (actual )
3818- if expPower != actPower {
3819- return false , nil
3820- }
3821- // TODO(yuzefovich): investigate why we can't always guarantee deterministic
3822- // 15 significant digits and switch back from 14 to 15 digits comparison
3823- // here. See #56446 for more details.
3824- for i := 0 ; i < 14 ; i ++ {
3825- expDigit := int (expected )
3826- actDigit := int (actual )
3827- if expDigit != actDigit {
3828- return false , nil
3829- }
3830- expected -= (expected - float64 (expDigit )) * 10
3831- actual -= (actual - float64 (actDigit )) * 10
3832- }
3833- return true , nil
3834- }
3835-
38363748func (t * logicTest ) formatValues (vals []string , valsPerLine int ) []string {
38373749 var buf bytes.Buffer
38383750 tw := tabwriter .NewWriter (& buf , 2 , 1 , 2 , ' ' , 0 )
0 commit comments