InexactError in Range

With f = -9.223372036854776e18, (f/2):0.0 gives a range of floats, while f:0.0 gives the following error:

ERROR: InexactError: trunc(Int64, 9.223372036854776e18)
Stacktrace:
 [1] trunc
   @ ./float.jl:905 [inlined]
 [2] round
   @ ./float.jl:385 [inlined]
 [3] (::Colon)(start::Float64, step::Float64, stop::Float64)
   @ Base ./twiceprecision.jl:427
 [4] (::Colon)(a::Float64, b::Float64)
   @ Base ./range.jl:14
 [5] top-level scope

I’m struggling to understand what boundary I’m crossing going from f/2 to f that makes Julia not able to give me a value for f:0.0!

julia> f == typemin(Int)
true

This is an implementation issue, unfortunately, where the length of the range is assumed to be an Int, but it can’t be rounded to an Int accurately.

julia> start = f
-9.223372036854776e18

julia> stop = 0.0
0.0

julia> step = 1.0
1.0

julia> lf = (stop-start)/step
9.223372036854776e18

julia> lf > typemax(Int)
true
1 Like

If you need this to work, BigInt may help.

julia> r = BigInt(f):0.0
-9.223372036854775808e+18:1.0:0.0

julia> length(r)
9223372036854775809