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

InexactError(): using range with higher values than Int64 #10554

Closed
peter1000 opened this issue Mar 18, 2015 · 8 comments · Fixed by #43059
Closed

InexactError(): using range with higher values than Int64 #10554

peter1000 opened this issue Mar 18, 2015 · 8 comments · Fixed by #43059

Comments

@peter1000
Copy link

Just played around and saw this:

Julia
Version 0.4.0-dev+3878 (2015-03-18 02:09 UTC)
Commit 17abcd9* (0 days old master)
x86_64-unknown-linux-gnu

Test code

# test1:
function test1()
    for i in 1:Int128(170141183460469231731687303715884105727)
    end
    println("Finished loop test1")
end

# test2: range
function test2()
    for i in range(1,Int128(170141183460469231731687303715884105727))
    end
    println("Finished loop test2:range()")
end

test1()
test2()

OUTPUT

Finished loop test1


ERROR: LoadError: InexactError()
 in convert at int.jl:157
 in test2 at /home/possible_error.jl:12
 in include at ./boot.jl:250
 in include_from_node1 at loading.jl:129
 in process_options at ./client.jl:318
 in _start at ./client.jl:402
while loading /home/possible_error.jl, in expression starting on line 18

Cheers
P

@nalimilan
Copy link
Member

You don't even need to run loops to reproduce this:

julia> range(1,Int128(170141183460469231731687303715884105727))
ERROR: InexactError()
 in convert at int.jl:157

julia> 1:Int128(170141183460469231731687303715884105727)
1:170141183460469231731687303715884105727

@nalimilan
Copy link
Member

Even clearer:

julia> UnitRange{Int64}(1, Int128(typemax(Int64)))
1:9223372036854775807

julia> UnitRange{Int64}(1, Int128(typemax(Int64))+1)
ERROR: InexactError()
 in convert at int.jl:157

range should probably choose better its type parameter. Currently it only takes into account the first argument.

@tkelman
Copy link
Contributor

tkelman commented Mar 18, 2015

Should this line

range(a::Real, len::Integer) = UnitRange{typeof(a)}(a, a+len-1)
be changed to

range(a::Real, len::Integer) = UnitRange{promote_type(typeof(a),typeof(len))}(a, a+len-1)

?

@ivarne
Copy link
Member

ivarne commented Mar 18, 2015

How would range(typemax(Int64), 3) work then?

I think the current behaviour is good enough, unless we decide to do a sensible checks for bounds, and add sensible errors when things overflow.

@tkelman
Copy link
Contributor

tkelman commented Mar 18, 2015

How would range(typemax(Int64), 3) work then?

Right now that gives me 9223372036854775807:9223372036854775806, and I don't think adding promote_type would change anything for that case.

@ivarne
Copy link
Member

ivarne commented Mar 18, 2015

That is a range with length of -1 (ie empty), when you would expect an error.

@tkelman
Copy link
Contributor

tkelman commented Mar 18, 2015

I think the range function being smarter about checking whether a+len-1 is likely to overflow is a slightly separate issue from whether its type parameter should be determined just by the start point or by promotion of the start and length.

@mschauer
Copy link
Contributor

Simple promotion will not do, one would expect range(0x1, 2) == 0x01:0x02, but range(promote(0x1, 2)...) == 1:2. This comes from the fact that if possible, length is measured as Int and the second parameter is a length-parameter. One could hardcode the cases where length is not Int maybe.

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.

5 participants