Skip to content

Replace with internal _iszero for checks that drop trivial zero, so that IntervalArithmetic can extend it. #623

@hsuknowledge

Description

@hsuknowledge

The package IntervalArithmetic intentionally raises error for comparisons between an interval enclosing multiple floating-point values and an interval with a single value, so-called thin interval, when the two sets overlap. Base.iszero(x) dispatchs to x == 0 so this is a comparison with a thin interval and will throw if x is an interval covering a zero.

> using IntervalArithmetic
> a = interval(-1, 1) ## spans across zero
> iszero(a)
ERROR: ArgumentError: `==` is purposely not supported for
overlapping non-thin intervals. See instead `isequal_interval`

On the other hand, IntervalArithmetic has a new function that serves the purpose, isthinzero, which allows this comparison to move on and only emit true if x does not enclose any other value.

> isthinzero(interval(-0,0)) ## true
> isthinzero(interval(-0.0,0.0)) ## true
> isthinzero(interval(prevfloat(-0.0),0.0)) ## false
> isthinzero(interval(0.0,nextfloat(0.0))) ## false

This is likely the behavior Polynomials authors want. Please consider adding this support to this package, otherwise end users like me are going to patch it with type piracy and mess up with other dependent packages.

Quoting a maintainer at IntervalArithmetic, a "safe iszero" (inside Polynomials module) approach is suggested.

The recommended approach (followed, e.g., by SparseArrays) is to define your own method defaulting to iszero unless the input is an interval. This allows you to not break IntervalArithmetic (which would occur by directly overloading Base.iszero), preserving reliability, and you get a finer control on the behaviour. E.g.,

module MyModule
# ... some code...
_safe_iszero(x) = iszero(x)
_safe_iszero(x::Interval) = isthinzero(x)
end

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