Skip to content
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

Test floating-point arrays using isapprox #318

Merged
merged 1 commit into from
Apr 3, 2024

Conversation

jishnub
Copy link
Member

@jishnub jishnub commented Dec 4, 2023

The equality need not hold, as floating-point results may change by a few ULP across Julia versions. Using isapprox is the more reasonable choice. This gets tests working on the current Julia nightly.

Copy link

codecov bot commented Dec 4, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (6c3d030) 95.25% compared to head (923c046) 99.88%.
Report is 7 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #318      +/-   ##
==========================================
+ Coverage   95.25%   99.88%   +4.63%     
==========================================
  Files           8        8              
  Lines         906      908       +2     
==========================================
+ Hits          863      907      +44     
+ Misses         43        1      -42     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jishnub
Copy link
Member Author

jishnub commented Dec 12, 2023

Should this be merged?

@dlfivefifty
Copy link
Member

The equality need not hold, as floating-point results may change by a few ULP across Julia versions

I don't understand this statement. All basic floating point operations are exactly defined and should never change.

@jishnub
Copy link
Member Author

jishnub commented Dec 13, 2023

Sorry, I wasn't clear. There are two factors here, and it's not about floating-point arithmetic directly.

The first issue is that Julia may return different random numbers for the same seed on different versions:

julia> VERSION
v"1.9.4"

julia> rng = MersenneTwister(123456); randn(rng, Float64)
-0.620399355121865

vs

julia> VERSION
v"1.11.0-DEV.1071"

julia> rng = MersenneTwister(123456); randn(rng, Float64)
-1.6434956816309902

The second issue is that indexing into a range and an array may lead to different results, as the former involves a different set of floating-point operations than the latter. So, on nightly,

julia> A
5-element Fill{Float64}, with entries equal to -1.6434956816309902

julia> B
6:10

julia> (A + B)[end]
8.356504318369009

julia> A[end] + B[end]
8.35650431836901

julia> (A + B)[end] == A[end] + B[end]
false

julia> (A + B)[end]  A[end] + B[end]
true

On the other hand, on v1.9.4

julia> rng = MersenneTwister(123456); A = Fill(randn(rng, Float64), 5)
5-element Fill{Float64}, with entries equal to -0.620399355121865

julia> B = 6:10
6:10

julia> A[end] + B[end]
9.379600644878135

julia> (A + B)[end]
9.379600644878135

julia> (A + B)[end] == A[end] + B[end]
true

Taken together, a test comparing two values obtained through different sets of operations and starting from different points may happen to pass coincidentally on one version of Julia, but fail on a different version. We should therefore compare these approximately and not exactly.

We may use StableRNGs to solve the first issue, but even then, we may need to find a seed for which the equality between the range getindex and floating-point addition does hold.

@jishnub
Copy link
Member Author

jishnub commented Dec 22, 2023

Gentle bump

@jishnub
Copy link
Member Author

jishnub commented Mar 14, 2024

Gentle bump

@jishnub
Copy link
Member Author

jishnub commented Mar 30, 2024

Bump. It would be good to have a decision on this, as this should get tests passing on unreleased versions of julia

@jishnub jishnub requested a review from oxinabox April 2, 2024 17:22
@@ -380,22 +380,22 @@ end
# type, and produce numerically correct results.
as_array(x::AbstractArray) = Array(x)
as_array(x::UniformScaling) = x
equal_or_undef(a::Number, b::Number) = (a == b) || isequal(a, b)
equal_or_undef(a, b) = all(equal_or_undef.(a, b))
isapprox_or_undef(a::Number, b::Number) = (a ≈ b) || isequal(a, b)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is fine.
I had though might want to flip order because of types that mightt define isapprox but would define isequal.
But if you restrict to Number subtypes there is a fallback defintion for isapprox that does call == at least.
And I think that has you covered

@jishnub jishnub merged commit 002f56c into JuliaArrays:master Apr 3, 2024
24 checks passed
@jishnub jishnub deleted the isapproxtests branch April 3, 2024 11:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants