Skip to content

isapprox does not handle typemin(::FixedPoint{<:Signed}) correctly #209

@kimikage

Description

@kimikage

For example,

julia> isapprox(-0.25Q0f7, -0.5Q0f7, rtol=0.5, atol=0) # this is OK
true

julia> isapprox(-0.5Q0f7, -1Q0f7, rtol=0.5, atol=0)
false

julia> isapprox(-0.5, -1.0, rtol=0.5, atol=0)
true

The direct cause is an overflow within abs.

function isapprox(x::T, y::T; rtol=0, atol=max(eps(x), eps(y))) where {T <: FixedPoint}
maxdiff = T(atol+rtol*max(abs(x), abs(y)))
rx, ry, rd = reinterpret(x), reinterpret(y), reinterpret(maxdiff)
abs(signed(widen1(rx))-signed(widen1(ry))) <= rd
end

julia> abs(-1Q0f7)
-1.0Q0f7

julia> abs(Int8(-128))
-128

I don't think this is a critical issue, but I plan to add checked_abs / saturating_abs after PR #190. Also, in the future, the default arithmetic may be changed to the checked arithmetic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions