# Getting wrong values when using \\div (÷) operator

**URL:** <https://discourse.julialang.org/t/getting-wrong-values-when-using-div-operator/93329>\
**Category:** General Usage\
**Tags:** bug\
**Created:** [January 21, 2023, 6:48pm UTC](https://discourse.julialang.org/t/getting-wrong-values-when-using-div-operator/93329 "2023-01-21T18:48:49Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gianluca\_Fuwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gianluca_fuwa/32/46071_2.png) [@Gianluca\_Fuwa](https://discourse.julialang.org/u/Gianluca_Fuwa)\
**Post date:** [January 21, 2023, 6:48pm UTC](https://discourse.julialang.org/t/getting-wrong-values-when-using-div-operator/93329/1 "2023-01-21T18:48:49Z")

</div>

Hello,  
I’ve stumbled upon a bug when using the \div (÷) operator:

```julia
 20÷1 
> 20

```

produces the expected result 20, but

```julia
20÷0.1 
> 199.0

```

gives the result 199.0. This continues up until dividing by 1e-6, from which point on I get the expected results again.

I’m using Julia version 1.8.4

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [January 21, 2023, 6:59pm UTC](https://discourse.julialang.org/t/getting-wrong-values-when-using-div-operator/93329/2 "2023-01-21T18:59:03Z")

</div>

Are you perhaps looking for floating-point division, which you get by the `/` operator?

---

<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:** [January 21, 2023, 7:00pm UTC](https://discourse.julialang.org/t/getting-wrong-values-when-using-div-operator/93329/3 "2023-01-21T19:00:04Z")

</div>

It’s not a bug, but could be documented in the div docs. You’ll find the explanation here:  
[https://docs.julialang.org/en/v1/base/math/#Base.fld](https://docs.julialang.org/en/v1/base/math/#Base.fld)

> What is happening here is that the true value of the floating-point number written as `0.1` is slightly larger than the numerical value 1/10 while `6.0` represents the number 6 precisely. Therefore the true value of `6.0 / 0.1` is slightly less than 60. When doing division, this is rounded to precisely `60.0` , but `fld(6.0, 0.1)` always takes the floor of the true value, so the result is `59.0` .

And, you should be using proper float division with `/`, not `div`.

---

<div class="post-metadata">

**Author:** ![Gianluca\_Fuwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gianluca_fuwa/32/46071_2.png) [@Gianluca\_Fuwa](https://discourse.julialang.org/u/Gianluca_Fuwa)\
**Post date:** [January 21, 2023, 7:07pm UTC](https://discourse.julialang.org/t/getting-wrong-values-when-using-div-operator/93329/4 "2023-01-21T19:07:32Z")

</div>

What I am trying to do is create a vector of length

```julia
(upper_limit - lower_limit) / step_size + 1

```

Without having to use the `round()` function, though I might have to use it here looking at @DNF 's answer.

---

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [January 22, 2023, 3:37am UTC](https://discourse.julialang.org/t/getting-wrong-values-when-using-div-operator/93329/5 "2023-01-22T03:37:17Z")

</div>

How about using, e.g.,

```julia
julia> collect(range(0,20,step=0.1))
201-element Vector{Float64}:
  0.0
  0.1
  0.2
  0.3
  0.4
  0.5
  0.6
  0.7
  ⋮
 19.3
 19.4
 19.5
 19.6
 19.7
 19.8
 19.9
 20.0

```

---

<div class="post-metadata">

**Author:** ![Gianluca\_Fuwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gianluca_fuwa/32/46071_2.png) [@Gianluca\_Fuwa](https://discourse.julialang.org/u/Gianluca_Fuwa)\
**Post date:** [January 22, 2023, 5:01am UTC](https://discourse.julialang.org/t/getting-wrong-values-when-using-div-operator/93329/6 "2023-01-22T05:01:20Z")

</div>

The vector I’m creating / initializing here is supposed to be filled with zeros, so this unfortunately doesn’t help.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 22, 2023, 5:46am UTC](https://discourse.julialang.org/t/getting-wrong-values-when-using-div-operator/93329/7 "2023-01-22T05:46:10Z")

</div>

> [@Gianluca\_Fuwa](#):
>
> What I am trying to do is create a vector of length
> 
> ```julia
> (upper_limit - lower_limit) / step_size + 1
> 
> ```
> 
> Without having to use the `round()` function, though I might have to use it here looking at @DNF 's answer.

You may be interested in one of the following.

```julia
julia> lower_limit, upper_limit, step_size = 0, 20, 0.1
(0, 20, 0.1)

julia> range(lower_limit, upper_limit; step = step_size) |> collect
201-element Vector{Float64}:
  0.0
  0.1
  0.2
  0.3
  0.4
  0.5
  0.6
  0.7
  0.8
  0.9
  ⋮
 19.2
 19.3
 19.4
 19.5
 19.6
 19.7
 19.8
 19.9
 20.0

julia> LinRange(lower_limit, upper_limit, 201)
201-element LinRange{Float64, Int64}:
 0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,…,19.4,19.5,19.6,19.7,19.8,19.9,20.0

```

---

<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:** [January 22, 2023, 8:02am UTC](https://discourse.julialang.org/t/getting-wrong-values-when-using-div-operator/93329/8 "2023-01-22T08:02:17Z")

</div>

> [@Gianluca\_Fuwa](#):
>
> Without having to use the `round()` function, though I might have to use it here looking at @DNF 's answer.

Can you explain why you want to avoid` round`? `div` with floats does rounding internally, so is it just a question of avoiding verbosity?

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [January 22, 2023, 8:07am UTC](https://discourse.julialang.org/t/getting-wrong-values-when-using-div-operator/93329/9 "2023-01-22T08:07:40Z")

</div>

> [@Gianluca\_Fuwa](#):
>
> The vector I’m creating / initializing here is supposed to be filled with zeros, so this unfortunately doesn’t help.

zero(range(0,20,step=0.1))

---

<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:** [January 22, 2023, 8:50am UTC](https://discourse.julialang.org/t/getting-wrong-values-when-using-div-operator/93329/10 "2023-01-22T08:50:07Z")

</div>

This might work, but it doesn’t really signal the intent of the code, just “Do whatever `range` does”. Will it work equally well for other input values? We don’t know, because the OP’s intent is unclear.

Using, for example,

```julia
round(Int, (upper_limit - lower_limit) / step_size) + 1 

```

would make it crystal clear.

(Edit: forgot the `Int` argument to `round`)

---

<div class="post-metadata">

**Author:** ![Gianluca\_Fuwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gianluca_fuwa/32/46071_2.png) [@Gianluca\_Fuwa](https://discourse.julialang.org/u/Gianluca_Fuwa)\
**Post date:** [January 22, 2023, 9:23am UTC](https://discourse.julialang.org/t/getting-wrong-values-when-using-div-operator/93329/11 "2023-01-22T09:23:17Z")

</div>

Yes, it was just a matter of me wanting cleaner looking code.  
The reason I started this thread wasn’t even the issue of creating the array, I just thought the `div` behaviour was unintended as I didn’t see the doc comment about it.  
I’m still grateful for all the suggestions.
