IntervalArithmetic.jl icon indicating copy to clipboard operation
IntervalArithmetic.jl copied to clipboard

Macros can not guarantee tight inclusion of float literals

Open Kolaru opened this issue 3 years ago • 1 comments

Float literals are parsed before being passed to the macro:

julia> macro isitonetenth(expr)
           @show expr
           @show typeof(expr)
           @show expr == 0.1
       end
@isitonetenth (macro with 1 method)

julia> @isitonetenth 0.100000000000000006
expr = 0.1  # I was expecting Symbol("0.100000000000000006") here
typeof(expr) = Float64  # It has already been parsed to a floating point number
expr == 0.1 = true
true

This means that @interval does not know what the literal was before rounding, which means it can lead to incorrect enclosures:

julia> x = @interval 0.100000000000000006
[0.0999999, 0.100001]

julia> big(x.hi)
0.1000000000000000055511151231257827021181583404541015625

julia> BigFloat("0.100000000000000006") in x
false

i.e. despite typing 0.100000000000000006 explicitly, the user has no guarantee it actually ends up in the interval.

My conclusion is that with floats literals we can not do better than returning (prevfloat(x)..nextfloat(x)) with either the @interval macro or atomic. I plan to include it in the final version of #271, since in passing I ended up rewriting big parts of atomic.

Kolaru avatar Dec 29 '21 16:12 Kolaru

Good point. It is pretty frustrating that the Julia parser works like that!

dpsanders avatar Dec 29 '21 18:12 dpsanders

I am closing this issue since @interval has been removed (and if it were to be reintroduced later, it will just be a syntax sugar to wrap every constant literals in an expression with interval). Also atomic does exactly (prevfloat(x)..nextfloat(x)).

OlivierHnt avatar Sep 05 '23 11:09 OlivierHnt