Skip to content

isapprox (≈) returns different answers depending on the abstract element type #1083

@mbauman

Description

@mbauman

This is quite problematic:

julia> Any[824.9999999999999] ≈ [825]
true

julia> Number[824.9999999999999] ≈ [825]
false

julia> Union{Number,Missing}[824.9999999999999] ≈ [825]
true

julia> Real[824.9999999999999] ≈ [825]
false

julia> Union{Real,Missing}[824.9999999999999] ≈ [825]
true

julia> AbstractFloat[824.9999999999999] ≈ [825]
ERROR: MethodError: no method matching eps(::Type{AbstractFloat})
The function `eps` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  eps(::Type{BigFloat})
   @ Base mpfr.jl:1087
  eps(::Type{Float32})
   @ Base float.jl:1049
  eps(::Type{Float64})
   @ Base float.jl:1050
  ...

Stacktrace:
 [1] rtoldefault(::Type{AbstractFloat})
   @ Base ./floatfuncs.jl:260
 [2] rtoldefault(x::Type{AbstractFloat}, y::Type{Int64}, atol::Int64)
   @ Base ./floatfuncs.jl:263
 [3] isapprox(x::Vector{AbstractFloat}, y::Vector{Int64})
   @ LinearAlgebra ~/Projects/Julia/julia/usr/share/julia/stdlib/v1.12/LinearAlgebra/src/generic.jl:1880
 [4] top-level scope
   @ REPL[20]:1

julia> Union{AbstractFloat,Missing}[824.9999999999999] ≈ [825]
true

julia> Union{AbstractFloat,Int,}[824.9999999999999] ≈ [825]
false

julia> Union{Float32, Float64}[824.9999999999999] ≈ [825]
ERROR: MethodError: no method matching eps(::Type{Union{Float32, Float64}})

Closest candidates are:
  eps(::Type{BigFloat})
   @ Base mpfr.jl:1013
  eps(::Type{Dates.Time})
   @ Dates ~/.julia/juliaup/julia-1.10.4+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/Dates/src/types.jl:434
  eps(::Type{Float16})
   @ Base float.jl:967
  ...

Stacktrace:
 [1] rtoldefault(::Type{Union{Float32, Float64}})
   @ Base ./floatfuncs.jl:344
 [2] rtoldefault(x::Type{Union{Float32, Float64}}, y::Type{Int64}, atol::Int64)
   @ Base ./floatfuncs.jl:347
 [3] isapprox(x::Vector{Union{Float32, Float64}}, y::Vector{Int64})
   @ LinearAlgebra ~/.julia/juliaup/julia-1.10.4+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/LinearAlgebra/src/generic.jl:1785
 [4] top-level scope
   @ REPL[22]:1

julia> Union{Int, Float64}[824.9999999999999] ≈ [825]
false

Metadata

Metadata

Assignees

No one assigned

    Labels

    correctness bug ⚠Bugs that are likely to lead to incorrect results in user code without throwing

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions