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

doc: float: add note for NaN compare #53194

Merged
merged 2 commits into from
Feb 10, 2024
Merged

Conversation

inkydragon
Copy link
Sponsor Member

@inkydragon inkydragon commented Feb 5, 2024

Current documentation seems to imply that one should use === for NaN comparisons.
But non-normalized NaNs will give unexpected results: NaN32p1 !== NaN32.

julia> reinterpret(UInt32, NaN32)
0x7fc00000

julia> NaN32p1 = reinterpret(Float32, 0x7fc00001)
NaN32

julia> NaN32p1 === NaN32,  NaN32p1 == NaN,  isequal(NaN32p1, NaN32),  isnan(NaN32p1)
(false, false, true, true)

julia> NaN32 === NaN32,  NaN32 == NaN,  isequal(NaN32, NaN32),  isnan(NaN32)
(true, false, true, true)

Maybe a little relevant (NaN canonicalization is mentioned): #49353

@inkydragon inkydragon added the domain:docs This change adds or pertains to documentation label Feb 5, 2024
@mikmoore
Copy link
Contributor

mikmoore commented Feb 6, 2024

If we're going to remark on the unsuitability of ===, I think we should provide an explanation. Maybe modify the remark on === to something vaguely like the following?

"There are many different encodings of NaN. Do not rely on === for NaN comparisons because isnan(a) & isnan(b) does not imply a === b."

This might enable a slightly simpler (is it?) example like

julia> a, b = reinterpret(Float64, Int64(-1)), reinterpret(Float64, Int64(-2))
(NaN, NaN)

julia> isequal(a, b), a === b
(true, false)

@vtjnash vtjnash merged commit a59f219 into JuliaLang:master Feb 10, 2024
9 checks passed
@inkydragon inkydragon deleted the doc-nan branch March 15, 2024 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:docs This change adds or pertains to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants