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 when generating random numbers from static range #15

Open
nsajko opened this issue Dec 20, 2022 · 1 comment
Open

InexactError when generating random numbers from static range #15

nsajko opened this issue Dec 20, 2022 · 1 comment

Comments

@nsajko
Copy link

nsajko commented Dec 20, 2022

julia> using Random, StaticNumbers

julia> v = zeros(UInt8, 2^16);

julia> rand!(v, UInt8(0):UInt8(2));

julia> rand!(v, static(UInt8(0)):static(UInt8(2)));
ERROR: InexactError: check_top_bit(UInt64, -1)
Stacktrace:
  [1] throw_inexacterror(f::Symbol, #unused#::Type{UInt64}, val::Int64)
    @ Core ./boot.jl:635
  [2] check_top_bit
    @ ./boot.jl:649 [inlined]
  [3] toUInt64
    @ ./boot.jl:760 [inlined]
  [4] UInt64(x::Int64)
    @ Core ./boot.jl:790
  [5] UInt64(#unused#::StaticInteger{-1})
    @ StaticNumbers ~/.julia/packages/StaticNumbers/StsSi/src/StaticNumbers.jl:160
  [6] convert(#unused#::Type{UInt64}, x::StaticInteger{-1})
    @ Base ./number.jl:7
  [7] rem(x::StaticInteger{-1}, T::Type{UInt64})
    @ Base ./int.jl:600
  [8] +(a::StaticInteger{-1}, b::UInt64)
    @ Base ./int.jl:1040
  [9] last(r::LengthUnitRange{Int64, StaticInteger{-1}, StaticInteger{0x0000000000000003}})
    @ StaticNumbers ~/.julia/packages/StaticNumbers/StsSi/src/LengthRanges.jl:119
 [10] isempty(r::LengthUnitRange{Int64, StaticInteger{-1}, StaticInteger{0x0000000000000003}})
    @ Base ./range.jl:662
 [11] Random.SamplerRangeNDL(r::LengthUnitRange{Int64, StaticInteger{-1}, StaticInteger{0x0000000000000003}})
    @ Random ~/tmp/julia-61afe7b7f0/share/julia/stdlib/v1.10/Random/src/generation.jl:333
 [12] Random.Sampler(#unused#::Type{TaskLocalRNG}, r::LengthUnitRange{Int64, StaticInteger{-1}, StaticInteger{0x0000000000000003}}, #unused#::Val{Inf})
    @ Random ~/tmp/julia-61afe7b7f0/share/julia/stdlib/v1.10/Random/src/generation.jl:189
 [13] Random.Sampler(rng::TaskLocalRNG, x::LengthUnitRange{Int64, StaticInteger{-1}, StaticInteger{0x0000000000000003}}, r::Val{Inf})
    @ Random ~/tmp/julia-61afe7b7f0/share/julia/stdlib/v1.10/Random/src/Random.jl:140
 [14] Random.Sampler(rng::TaskLocalRNG, x::LengthUnitRange{Int64, StaticInteger{-1}, StaticInteger{0x0000000000000003}})
    @ Random ~/tmp/julia-61afe7b7f0/share/julia/stdlib/v1.10/Random/src/Random.jl:140
 [15] rand!(rng::TaskLocalRNG, A::Vector{UInt8}, X::LengthUnitRange{Int64, StaticInteger{-1}, StaticInteger{0x0000000000000003}})
    @ Random ~/tmp/julia-61afe7b7f0/share/julia/stdlib/v1.10/Random/src/Random.jl:269
 [16] rand!(A::Vector{UInt8}, X::LengthUnitRange{Int64, StaticInteger{-1}, StaticInteger{0x0000000000000003}})
    @ Random ~/tmp/julia-61afe7b7f0/share/julia/stdlib/v1.10/Random/src/Random.jl:266
 [17] top-level scope
    @ REPL[7]:1
@perrutquist
Copy link
Owner

Thanks for the report. This should probably be fixed by adding a method:

Base.rem(x::StaticInteger{X}, ::Type{T}) where {X, T<:Integer} = rem(X, T)

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

No branches or pull requests

2 participants