# Float vs integer and the == sign

**URL:** <https://discourse.julialang.org/t/float-vs-integer-and-the-sign/70172>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 21, 2021, 6:54pm UTC](https://discourse.julialang.org/t/float-vs-integer-and-the-sign/70172 "2021-10-21T18:54:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 21, 2021, 6:54pm UTC](https://discourse.julialang.org/t/float-vs-integer-and-the-sign/70172/1 "2021-10-21T18:54:35Z")

</div>

I noticed that when D has at least one floating numbers, this program will work:  
D = [-0, 0, 1, 5, 0, 7.1, 0]  
D[D .== 0] .= 0.0001;

But it will quit working when D is composed of integers entirely.  
D = [-0, 0, 1, 5, 0, 7, 0]  
D[D .== 0] .= 0.0001;

Error message:

```julia
InexactError: Int64(0.0001)

Stacktrace:
 [1] Int64
   @ ./float.jl:723 [inlined]
 [2] convert
   @ ./number.jl:7 [inlined]
 [3] fill!(A::SubArray{Int64, 1, Vector{Int64}, Tuple{Vector{Int64}}, false}, x::Float64)
   @ Base ./multidimensional.jl:1055
 [4] copyto!
   @ ./broadcast.jl:944 [inlined]
 [5] materialize!
   @ ./broadcast.jl:894 [inlined]
 [6] materialize!(dest::SubArray{Int64, 1, Vector{Int64}, Tuple{Vector{Int64}}, false}, bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0}, Nothing, typeof(identity), Tuple{Float64}})
   @ Base.Broadcast ./broadcast.jl:891
 [7] top-level scope
   @ In[209]:2
 [8] eval
   @ ./boot.jl:360 [inlined]
 [9] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1116

```

In reality, my D can be all integers or not. What should be the fix? Thanks!

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 21, 2021, 7:00pm UTC](https://discourse.julialang.org/t/float-vs-integer-and-the-sign/70172/2 "2021-10-21T19:00:04Z")

</div>

> [@leon](#):
>
> D = [-0, 0, 1, 5, 0, 7, 0]

The fix is either

```julia
D = [-0.0, 0.0, 1.0, 5.0, 0.0, 7.0, 0.0]

```

or

```julia
D = Float64[-0, 0, 1, 5, 0, 7, 0]

```

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 21, 2021, 7:13pm UTC](https://discourse.julialang.org/t/float-vs-integer-and-the-sign/70172/3 "2021-10-21T19:13:04Z")

</div>

Thanks.

I’m able to apply the below to my data:  
`convert(Array{Float64}, D)`

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 21, 2021, 9:05pm UTC](https://discourse.julialang.org/t/float-vs-integer-and-the-sign/70172/4 "2021-10-21T21:05:19Z")

</div>

> [@leon](#):
>
> `convert(Array{Float64}, D)`

Much nicer to write

```julia
Float64.(D)

```

But isn’t it better to make `D` a float array in the first place instead?

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [October 21, 2021, 9:56pm UTC](https://discourse.julialang.org/t/float-vs-integer-and-the-sign/70172/5 "2021-10-21T21:56:22Z")

</div>

> Much nicer to write
> 
> ```julia
> Float64.(D)
> 
> ```

Why not use [`float`](https://docs.julialang.org/en/v1/base/numbers/#Base.float-Tuple%7BAny%7D)?

```julia
julia> float(D)
7-element Vector{Float64}:
 0.0
 0.0
 1.0
 5.0
 0.0
 7.0
 0.0

```

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [October 21, 2021, 10:21pm UTC](https://discourse.julialang.org/t/float-vs-integer-and-the-sign/70172/6 "2021-10-21T22:21:42Z")

</div>

Two main comments here.

1. Array type in Julia is specified at the point of its creation. So if you create an array with all the elements of type `Int64`, you will get an `array{Int64,<dims>}`. The array type is fixed at this point and cannot be changed later without allocating a new array. Now when you try to insert the value `0.0001` to replace `0` it must be converted first to an `Int64`, but `0.0001` cannot be represented as `Int64` and this is the error you get. To avoid this, you either have to allocate a new array or simply add at least one `Float64` element when creating `D`, like this `D = [-0.0, 0, 1, 5, 0, 7, 0]`.

2. The allocation-free functional programming style is usually the recommended way to code in Julia. Instead of the of the MATLAB-ish way that you used, you can write `replace!(x -> iszero(x) ? 0.0001 : x, D)` which is faster and have zero allocations.

---

<div class="post-metadata">

**Author:** ![leon](https://avatars.discourse-cdn.com/v4/letter/l/dc4da7/32.png) [@leon](https://discourse.julialang.org/u/leon)\
**Post date:** [October 21, 2021, 11:52pm UTC](https://discourse.julialang.org/t/float-vs-integer-and-the-sign/70172/7 "2021-10-21T23:52:59Z")

</div>

Many thanks!

This is such a nicer solution 👍

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [October 22, 2021, 3:13am UTC](https://discourse.julialang.org/t/float-vs-integer-and-the-sign/70172/8 "2021-10-22T03:13:17Z")

</div>

There’s an even simpler version of the solution provided by @Seif_Shebl:

```julia
replace!(D, 0 => 0.0001)

```
