-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v1.9.0 breaks float comparisons with EqualValues #1576
Comments
Same issue, but only after upgrading to go 1.22 |
This testcase also fails on v1.8.4 {float32(10.1), float64(10.1), true}, But this does not: {float64(10.1), float32(10.1), true}, This issue happens on Go 1.17 through to 1.22. You've discovered an old bug here, I don't think anyone would disagree that the assertion should behave the same both ways around? The docstring says:
Both the new behaviour and the old behaviour do that, one is sensitive to the argument order, the other is not. The existing docstring leaves ambiguity as to which type the values will be converted to. The exact floating point value is not the same, nether depth can precisely represent thirtyTwo := float32(10.1)
sixtyFour := float64(10.1)
fmt.Println(strconv.FormatFloat(float64(thirtyTwo), 'f', 64, 32)) // 10.1000003814697265625000000000000000000000000000000000000000000000
fmt.Println(strconv.FormatFloat(sixtyFour, 'f', 64, 64)) // 10.0999999999999996447286321199499070644378662109375000000000000000
fmt.Println(thirtyTwo) // 10.1
fmt.Println(sixtyFour) // 10.1 I think there are two options here:
I lean towards 1. @vispariana and @arjunmahishi as the original reporter and fixer of the overflow bug, what are your thoughts? |
Reverting #1531 will leave us with two bugs instead of one right? (overflow and precision). And like you pointed out, the old code still fails the assertion for {float32(10.1), float64(10.1), true} For that reason, I am more inclined towards option 2. |
Would it make sense to then instead introduce |
Ya. I was thinking along the same lines. The user could decide on a |
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 ofreflect.ValueOf(aFloat32).Convert(float64Type).Interface()
against the actualfloat64
-Convert
seems to misinterpret the underlying bits and the interface of the former operand comes out to eg.10.100000381469727
instead of10.1
Step To Reproduce
Run the following suite:
Alternatively, inject the following case into
TestObjectsAreEqualValues
:Expected behavior
I'd expect the
EqualValues(float32(10.1), float64(10.1))
assertion to pass.Actual behavior
The abovementioned assertion fails.
The text was updated successfully, but these errors were encountered: