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

range(0, length=Uint(0)) throws InexactError #35711

Closed
ethomag opened this issue May 3, 2020 · 3 comments · Fixed by #43059
Closed

range(0, length=Uint(0)) throws InexactError #35711

ethomag opened this issue May 3, 2020 · 3 comments · Fixed by #43059

Comments

@ethomag
Copy link
Contributor

ethomag commented May 3, 2020

julia> range(0, length=UInt(0))
ERROR: InexactError: check_top_bit(Int64, 18446744073709551615)

Due to the fact that empty ranges are represented by n:n-1, which breaks with n=0 and unsigned types.

I think the range expression is a valid one that ought to be supported and the error message is not very helpful.

Could not find anything related on discourse, the closest one is:
#26608

@Micket
Copy link
Contributor

Micket commented May 3, 2020

So, problem seems to stem from promotion of Int -> UInt (which then underflows) on

_range(a::Real, ::Nothing, ::Nothing, len::Integer) = UnitRange{typeof(a)}(a, oftype(a, a+len-1))

Possible fix

_range(a::Real, ::Nothing, ::Nothing, len::Integer) = UnitRange{typeof(a)}(a, a+oftype(a, len)-1)

@mbauman
Copy link
Member

mbauman commented May 8, 2020

This is strongly related to #10554; we should figure out a way to do the promotion that solves both cases (the MWE for that one is now range(1,length=typemax(Int128))).

@Micket
Copy link
Contributor

Micket commented May 9, 2020

At least range(1,length=typemax(Int128)) throws an error.
There are cases which are just kinda silently "broken"

julia> range(UInt(0),length=0)
0x0000000000000000:0xffffffffffffffff
julia> range(2,length=typemax(Int64))
2:1

Though, surely there is some responsibility of the caller to ensure the datatypes used don't overflow/underflow (unless

vtjnash added a commit that referenced this issue Nov 12, 2021
vtjnash added a commit that referenced this issue Dec 8, 2021
…cable)

Be careful to use `oneunit` instead of `1`, so that arithmetic on
user-given types does not promote first to Int.

Fixes #35711
Fixes #10554
vtjnash added a commit that referenced this issue Dec 17, 2021
…cable)

Be careful to use `oneunit` instead of `1`, so that arithmetic on
user-given types does not promote first to Int.

Fixes #35711
Fixes #10554
vtjnash added a commit that referenced this issue Dec 17, 2021
…cable)

Be careful to use `oneunit` instead of `1`, so that arithmetic on
user-given types does not promote first to Int.

Fixes #35711
Fixes #10554
vtjnash added a commit that referenced this issue Jan 12, 2022
…pplicable) (#43059)

Be careful to use `oneunit` instead of `1`, so that arithmetic on
user-given types does not promote first to Int.

Fixes #35711
Fixes #10554
LilithHafner pushed a commit to LilithHafner/julia that referenced this issue Feb 22, 2022
…pplicable) (JuliaLang#43059)

Be careful to use `oneunit` instead of `1`, so that arithmetic on
user-given types does not promote first to Int.

Fixes JuliaLang#35711
Fixes JuliaLang#10554
LilithHafner pushed a commit to LilithHafner/julia that referenced this issue Mar 8, 2022
…pplicable) (JuliaLang#43059)

Be careful to use `oneunit` instead of `1`, so that arithmetic on
user-given types does not promote first to Int.

Fixes JuliaLang#35711
Fixes JuliaLang#10554
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants