Testing for lossless <:Integer -> <:AbstractFloat conversion

After tracking down a bug in my code which can be neatly summarized as

julia> maxintfloat() + 1 ≤ maxintfloat()
true

I was wondering how to test if an

vec[index] = elt

can be performed losslessly, if vec::AbstractVector{T<:AbstractFloat} and elt <: Integer.

Am I doing something questionable by using

(m = oftype(elt, maxintfloat(T)); -m ≤ elt ≤ m)

which assumes that the minimum representable value is minus the maximum?

apparently yes, eg if elt::Int8 and T==Float64 then oftype will fail.

The oftype is wrong. For example, suppose elt is an Int8 and T is a Float64 — you will get an InexactError. You can use m = Integer(maxintfloat(T)) to convert to an appropriate integer type.

1 Like