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

**URL:** <https://discourse.julialang.org/t/testing-for-lossless-integer-abstractfloat-conversion/15267>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 21, 2018, 9:19am UTC](https://discourse.julialang.org/t/testing-for-lossless-integer-abstractfloat-conversion/15267 "2018-09-21T09:19:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 21, 2018, 9:19am UTC](https://discourse.julialang.org/t/testing-for-lossless-integer-abstractfloat-conversion/15267/1 "2018-09-21T09:19:09Z")

</div>

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

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

```

I was wondering how to test if an

```julia
vec[index] = elt

```

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

Am I doing something questionable by using

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

```

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

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 21, 2018, 10:02am UTC](https://discourse.julialang.org/t/testing-for-lossless-integer-abstractfloat-conversion/15267/2 "2018-09-21T10:02:35Z")

</div>

> [@Tamas\_Papp](#):
>
> Am I doing something questionable

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

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 21, 2018, 11:33am UTC](https://discourse.julialang.org/t/testing-for-lossless-integer-abstractfloat-conversion/15267/3 "2018-09-21T11:33:45Z")

</div>

> [@Tamas\_Papp](#):
>
> Am I doing something questionable by using

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.
