Skip to content

v1.9.0 breaks float comparisons with EqualValues #1576

Open
@vlasn

Description

@vlasn

Description

Migrating from 1.8.4 to 1.9.0 seems to have broken some of our unit tests that relied on comparing floats of varying bit depths with EqualValues(...).

I tried fixing it myself but given I'm not very familiar with the reflect package, I didn't get much further than discovering the issue stemmed from comparing the output of reflect.ValueOf(aFloat32).Convert(float64Type).Interface() against the actual float64 - Convert seems to misinterpret the underlying bits and the interface of the former operand comes out to eg. 10.100000381469727 instead of 10.1

Step To Reproduce

Run the following suite:

package a_test

import (
	"testing"

	"github.com/stretchr/testify/suite"
)

type FailingMinimalSuite struct {
	suite.Suite
}

func (s *FailingMinimalSuite) TestFloatEqualValues() {
	s.EqualValues(float32(10.1), float64(10.1))
}

func TestRunFailingMinimalSuite(t *testing.T) {
	suite.Run(t, &FailingMinimalSuite{})
}

Alternatively, inject the following case into TestObjectsAreEqualValues:

{float32(10.1), float64(10.1), true},

Expected behavior

I'd expect the EqualValues(float32(10.1), float64(10.1)) assertion to pass.

Actual behavior

The abovementioned assertion fails.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions