Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arithmetic with big constants should not promote #229

Closed
dpsanders opened this issue Sep 22, 2018 · 3 comments · Fixed by #567
Closed

Arithmetic with big constants should not promote #229

dpsanders opened this issue Sep 22, 2018 · 3 comments · Fixed by #567

Comments

@dpsanders
Copy link
Member

Probably we would prefer the following to return an Interval{Float64}. This is happening due to the automatic promotion rules:

julia> interval(1.0) / factorial(big(100))
Interval(1.071510288125466923183546759519191522011540649292709804836865813095970266876232e-158, 1.071510288125466923183546759519191522011540649292709804836865813095970266876248e-158)

julia> typeof(ans)
Interval{BigFloat}

We can get around it by an explicit conversion:

julia> interval(1.0) / convert(Interval{Float64}, factorial(big(100)))
Interval(1.0715102881254667e-158, 1.0715102881254671e-158)

Note also that Interval{Float64}(factorial(big(100))) does not give an enclosure, which is a bug:

julia> Interval{Float64}(factorial(big(100)))
Interval(9.332621544394415e157, 9.332621544394415e157)
@dpsanders dpsanders changed the title Arithmetic with big constants Arithmetic with big constants should not promote Sep 24, 2018
@lbenet
Copy link
Member

lbenet commented Sep 27, 2018

I think promotion rules are ok, simply because following them is consistent with Base; then, one has to rely on the explicit conversion if Interval{Float64} is the goal.

You are right that the result of Interval{Float64}(factorial(big(100))) is wrong. A simple solution, I think, is by using the following methods (instead of the current one): Interval{T}(x) where {T} = atomic(Interval{T}, x). Then,

julia> IntervalArithmetic.atomic(Interval{Float64}, factorial(big(100)))
Interval(9.332621544394415e157, 9.332621544394416e157)

@dpsanders
Copy link
Member Author

We are free to define our own promotion rules. I think the fact that we are working with Interval{Float64} should take priority over the fact that there's a BigInt number, and if you want Interval{BigFloat} then you should say so explicitly.

@lbenet
Copy link
Member

lbenet commented Dec 12, 2018

I agree with you that we are free to define the promotion behavior. Yet, as pointed out above, the default behavior should be consistency with Base.

In my opinion, in the example with factorial(100), the user needs factorial(big(100)) to avoid an OverflowError. If the user forces an exception going to BigInt, in order to get back to Float64 the user should convert explicitly to Interval{Float64}.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants