JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.14k stars 5.44k forks source link

`getindex` and `isassigned` with big `BigInt` ranges and trailing 1 indices errors #54786

Open mtfishman opened 1 month ago

mtfishman commented 1 month ago

isassigned with big BigInt ranges and trailing 1 indices fails with an InexactError:

julia> isassigned(1:big(10)^10, big(10)^10, 1)
true

julia> isassigned(1:big(10)^20, big(10)^20, 1)
ERROR: InexactError: Int64(100000000000000000000)
Stacktrace:
 [1] Type
   @ ./gmp.jl:384 [inlined]
 [2] convert
   @ ./number.jl:7 [inlined]
 [3] to_index
   @ ./indices.jl:292 [inlined]
 [4] to_index
   @ ./indices.jl:277 [inlined]
 [5] _to_indices1
   @ ./indices.jl:359 [inlined]
 [6] to_indices
   @ ./indices.jl:354 [inlined]
 [7] to_indices
   @ ./indices.jl:350 [inlined]
 [8] isassigned(::UnitRange{BigInt}, ::BigInt, ::Int64)
   @ Base ./multidimensional.jl:1568
 [9] top-level scope
   @ REPL[71]:1

julia> isassigned(1:big(10)^20, big(10)^20)
true

julia> versioninfo()
Julia Version 1.10.4
Commit 48d4fd48430 (2024-06-04 10:41 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 10 × Apple M1 Max
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, apple-m1)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)

I see the same issue on:

julia> versioninfo()
Julia Version 1.11.0-beta2
Commit edb3c92d6a6 (2024-05-29 09:37 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 10 × Apple M1 Max
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, apple-m1)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)

and:

julia> versioninfo()
Julia Version 1.12.0-DEV.717
Commit f1c601ddbc0 (2024-06-12 23:50 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: macOS (arm64-apple-darwin22.4.0)
  CPU: 10 × Apple M1 Max
  WORD_SIZE: 64
  LLVM: libLLVM-17.0.6 (ORCJIT, apple-m1)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)

It also doesn't seem to matter if the trailing index is big(1), it gives the same error.

EDIT: getindex also fails with the same error:

julia> (1:big(10)^20)[big(10)^20, 1]
ERROR: InexactError: Int64(100000000000000000000)
Stacktrace:
 [1] Type
   @ ./gmp.jl:384 [inlined]
 [2] convert
   @ ./number.jl:7 [inlined]
 [3] to_index
   @ ./indices.jl:292 [inlined]
 [4] to_index
   @ ./indices.jl:277 [inlined]
 [5] _to_indices1
   @ ./indices.jl:359 [inlined]
 [6] to_indices
   @ ./indices.jl:354 [inlined]
 [7] to_indices
   @ ./indices.jl:350 [inlined]
 [8] getindex(::UnitRange{BigInt}, ::BigInt, ::Int64)
   @ Base ./abstractarray.jl:1291
 [9] top-level scope
   @ REPL[74]:1
jmichel7 commented 1 month ago

The following bug is related (julia 1.10.4)

julia> big(1):big(10^19)
1:0

Edit: yes, sorry not a bug since 10^19<0

Zentrik commented 1 month ago

Isn't this due to normal overflow, perhaps you wanted to do length(1:big(10)^19).