JuliaIntervals / IntervalArithmetic.jl

Library for validated numerics using interval arithmetic
https://juliaintervals.github.io/IntervalArithmetic.jl/
Other
290 stars 70 forks source link

Clarification on 'NG' Decoration #634

Closed mi-ayala closed 4 months ago

mi-ayala commented 4 months ago

I've encountered a decoration labeled 'NG' that I couldn't find explained in the documentation. Could you provide some clarity on what 'NG' stands for and its implications?

OlivierHnt commented 4 months ago

Ah good catch, this should definitely be documented. The "NG" flag stands for "Not Guaranteed" and was introduced in this PR https://github.com/JuliaIntervals/IntervalArithmetic.jl/pull/590 if I recall correctly.

In a nutshell, the goal is to have a specific tag that lets us know when a number has been converted to an interval

julia> convert(Interval{Float64}, 1.) # considered "not guaranteed" as this call can be done implicitly
[1.0, 1.0]_com_NG

julia> interval(1) # considered "safe" as the user explicitly constructed the interval
[1.0, 1.0]_com

This is because Julia has a very extensive promotion system, relying on convert, which can make it hard to know if, at some point, an "unsafe" operation has been performed (perhaps by calling a generic function from Base that is not compatible with interval arithmetic). By "unsafe" I mean that we can not guarantee that the resulting interval contains the true result.

Of course the drawback is that some safe operations can also have a "NG" flag, e.g.

julia> 2 + interval(1) # in theory, this is safe since the user typed in `2` explicitly
[3.0, 3.0]_com_NG

Note that recently @lbenet fixed this unfortunate behaviour for powers (but that's because there is a special hook in Base to do so)

julia> interval(2)^2
[4.0, 4.0]_com

So the "NG" flag is an indicator that something may (or not) be unreliable. To avoid "NG" flags, make sure you have wrapped all the quantities involved in the computations as intervals.

mi-ayala commented 4 months ago

Thank you for your insightful reply @OlivierHnt . I've initiated a pull request to incorporate additional information into the documentation.