Skip to content

Incorrect results from isapprox with Integer args #55814

Closed as not planned
Closed as not planned
@yurivish

Description

@yurivish

I noticed the following results from isapprox:

julia> isapprox(1, 2, atol=1)
true

julia> isapprox(UInt(1), UInt(2), atol=1)
false

This function is documented to return true if the distance between the two inputs is within tolerance bounds, yet it returns false in the second case despite the fact that the two numbers are distance 1 apart. In fact, it will return false even if the tolerance is increased:

julia> isapprox(UInt(1), UInt(2), atol=100)
false

julia> isapprox(UInt(1), UInt(2), atol=1_000_000)
false

The same behavior occurs for relative tolerances:

julia> isapprox(1, 2, rtol=1)
true

julia> isapprox(UInt(1), UInt(2), rtol=1)
false

julia> isapprox(UInt(1), UInt(2), rtol=100)
false

julia> isapprox(UInt(1), UInt(2), rtol=1_000_000)
false

Similar behavior can be seen for signed integers, where eg. 125 is considered to be within an absolute distance of 10 from -125:

julia> isapprox(Int8(125), Int8(-125), atol=10)
true

julia> isapprox(Int8(-125), Int8(125), atol=10)
true

This appears to be due to an integer overflow bug in the isapprox implementation for integer arguments. The generic implementation has a comment about avoiding integer overflow but no such accounting is done in the version specialized for integers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    correctness bug ⚠Bugs that are likely to lead to incorrect results in user code without throwingduplicateIndicates similar issues or pull requestsmathsMathematical functions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions