# How come integer division 6.0 by 0.2 returning 29 is correct?

**URL:** <https://discourse.julialang.org/t/how-come-integer-division-6-0-by-0-2-returning-29-is-correct/16604>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 21, 2018, 4:49pm UTC](https://discourse.julialang.org/t/how-come-integer-division-6-0-by-0-2-returning-29-is-correct/16604 "2018-10-21T16:49:46Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![xanfus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xanfus/32/5813_2.png) [@xanfus](https://discourse.julialang.org/u/xanfus)\
**Post date:** [October 21, 2018, 4:49pm UTC](https://discourse.julialang.org/t/how-come-integer-division-6-0-by-0-2-returning-29-is-correct/16604/1 "2018-10-21T16:49:46Z")

</div>

“WolframAlpha” returns “30”. There are ways to conduct integer division differently.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [October 21, 2018, 5:31pm UTC](https://discourse.julialang.org/t/how-come-integer-division-6-0-by-0-2-returning-29-is-correct/16604/2 "2018-10-21T17:31:19Z")

</div>

Please provide a minimum working example (copy and paste the code you ran, and paste it inside backticks).

---

<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:** [October 21, 2018, 5:37pm UTC](https://discourse.julialang.org/t/how-come-integer-division-6-0-by-0-2-returning-29-is-correct/16604/3 "2018-10-21T17:37:04Z")

</div>

@dpsanders: this is probably about

```julia
julia> 6.0 ÷ 0.2
29.0

```

@xanfus: hint:

```julia
julia> 6.0-(0.2*29)
0.1999999999999993

```

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [October 21, 2018, 6:41pm UTC](https://discourse.julialang.org/t/how-come-integer-division-6-0-by-0-2-returning-29-is-correct/16604/4 "2018-10-21T18:41:36Z")

</div>

```julia
help?> ÷
"÷" can be typed by \div<tab>

search: ÷

  div(x, y)
  ÷(x, y)

  The quotient from Euclidean division. Computes x/y, truncated to an integer.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> 9 ÷ 4
  2
  
  julia> -5 ÷ 3
  -1

```

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [October 21, 2018, 6:56pm UTC](https://discourse.julialang.org/t/how-come-integer-division-6-0-by-0-2-returning-29-is-correct/16604/5 "2018-10-21T18:56:44Z")

</div>

To be fair, the float 0.2 is larger than two tenth,

```julia
julia> 0.2 > 2//10
true

```

so dividing 6 by the float(!) 0.2 and rounding down exactly gives 29. But

```julia
julia> 6.0/0.2
30.0

```

adding to the confusion, as 30.0 is the closest `Float64` number to 6 exactly divided by Float64(2.0), which is smaller than 30 but very close to.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [October 21, 2018, 9:46pm UTC](https://discourse.julialang.org/t/how-come-integer-division-6-0-by-0-2-returning-29-is-correct/16604/6 "2018-10-21T21:46:41Z")

</div>

This is the doc string for `div`:

```julia
The quotient from Euclidean division. Computes x/y, truncated to an integer.

```

It is does not match what occurs with `div(6.0, 0.2)`

```julia
julia> div(6.0, 0.2), trunc(6.0 / 0.2)
(29.0, 30.0)

```

This is what really occurs

```julia
julia> # trd(x,y) like fld(x,y) is to floor(x/y), cld(x,y) is ceil(x,y) for trunc(x,y)
julia> trd(x, y) = signbit(x) === signbit(y) ? fld(x, y) : cld(x, y)
julia> div(6.0, 0.2), trd(6.0, 0.2)
(29.0, 29.0)

# test
julia> div(6.0,0.2), div(6.0,-0.2), div(-6.0,0.2), div(-6.0,-0.2)
(29.0, -29.0, -29.0, 29.0)

julia> trd(6.0,0.2), trd(6.0,-0.2), trd(-6.0,0.2), trd(-6.0,-0.2)
(29.0, -29.0, -29.0, 29.0)

```

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [October 21, 2018, 11:25pm UTC](https://discourse.julialang.org/t/how-come-integer-division-6-0-by-0-2-returning-29-is-correct/16604/7 "2018-10-21T23:25:29Z")

</div>

Isn’t it strange that it’s defined on floating points at all? What’s the use case for these semantics?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [October 22, 2018, 2:40am UTC](https://discourse.julialang.org/t/how-come-integer-division-6-0-by-0-2-returning-29-is-correct/16604/8 "2018-10-22T02:40:29Z")

</div>

```julia
julia> using IntervalArithmetic
/(
julia> /(6.0, 2.0, RoundDown)
3.0

julia> /(6.0, 0.2, RoundDown)
29.999999999999996

julia> /(6.0, 0.2, RoundUp)
30.0

```

This shows that the true result of dividing the Float64 6.0 by the Float64 written 0.2 is a real number between 29.999999999999996 and 30.0.

Also,

```julia
julia> big(0.2)
2.00000000000000011102230246251565404236316680908203125e-01

julia> big(6.0) / big(0.2)
2.999999999999999833466546306226528180918982845108534622869786706115556703123498e+01

```

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [October 22, 2018, 3:08am UTC](https://discourse.julialang.org/t/how-come-integer-division-6-0-by-0-2-returning-29-is-correct/16604/9 "2018-10-22T03:08:26Z")

</div>

> [@JeffreySarnoff](#):
>
> This is the doc string for `div` :
> 
> ```julia
> The quotient from Euclidean division. Computes x/y, truncated to an integer.
> 
> ```
> 
> It does not match what occurs with `div(6.0, 0.2)`

Alas, docstrings are still written and “checked” by humans.  
IIUC, the code tools (which were implemented by superhumans) agree with you:

```julia
julia-1.1> @code_lowered div(6.0,0.2)
CodeInfo(
647 1 ─ %1 = $(Expr(:static_parameter, 1)) │
    │ %2 = (Base.rem)(x, y) │
    │ %3 = x - %2 │
    │ %4 = %3 / y │
    │ %5 = (Base.round)(%4) │
    │ %6 = (Base.convert)(%1, %5) │
    └── return %6 │
)

```

Aside @cstjean: `range`.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [October 22, 2018, 3:45am UTC](https://discourse.julialang.org/t/how-come-integer-division-6-0-by-0-2-returning-29-is-correct/16604/10 "2018-10-22T03:45:47Z")

</div>

> [@JeffreySarnoff](#):
>
> It is does not match what occurs with `div(6.0, 0.2)`
> 
> ```julia
> julia> div(6.0, 0.2), trunc(6.0 / 0.2)
> (29.0, 30.0)
> 
> ```

The former does exact mathematical division of the number represented by `6.0` (exactly the integer 6) and `0.2` (slightly less than 2/10), the division of which is slightly less than 30 and which result is then truncated to 29. The latter does float division of the same values and rounds the result to the nearest representwble float value, which is exactly 30. Only after rounding the intermediate result does it truncate—which doesn’t change anything since it’s already an integer. It’s a subtle difference but it’s consistent and correct.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [October 22, 2018, 3:49am UTC](https://discourse.julialang.org/t/how-come-integer-division-6-0-by-0-2-returning-29-is-correct/16604/11 "2018-10-22T03:49:45Z")

</div>

fair enough

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [October 22, 2018, 6:13am UTC](https://discourse.julialang.org/t/how-come-integer-division-6-0-by-0-2-returning-29-is-correct/16604/12 "2018-10-22T06:13:18Z")

</div>

In some finance applications, where it is vital to have an exact representation of cents even though numbers are in dollars, people use `Float64`s with a constant scaling factor of `1//100`. (For finer resolution, they typically use binary fractions of cents.)

It’d be fairly easy to create a Julia package to handle these kinds of numbers if people need it.

---

<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:** [October 22, 2018, 6:28am UTC](https://discourse.julialang.org/t/how-come-integer-division-6-0-by-0-2-returning-29-is-correct/16604/13 "2018-10-22T06:28:48Z")

</div>

> [@Per](#):
>
> It’d be fairly easy to create a Julia package to handle these kinds of numbers if people need it.

`Rational` is closed (exact) under `+`, `-`, `/`, `*`; and `^` with integer powers (among other things), and should be the first choice for someone who wants this kind of precision (eg for finance, labels on plot axes, etc).

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [October 22, 2018, 6:33am UTC](https://discourse.julialang.org/t/how-come-integer-division-6-0-by-0-2-returning-29-is-correct/16604/14 "2018-10-22T06:33:37Z")

</div>

Also Decimals.jl does a good job (even though it seems `rem` and maybe `div` are not defined yet)

```julia

julia> using Decimals

julia> Decimal(6) / Decimal(.2)
Decimal(0, 3, 1)

julia> Int(ans)
30

```

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [October 22, 2018, 7:04am UTC](https://discourse.julialang.org/t/how-come-integer-division-6-0-by-0-2-returning-29-is-correct/16604/15 "2018-10-22T07:04:36Z")

</div>

> [@Tamas\_Papp](#):
>
> `Rational` is closed (exact) under `+` , `-` , `/` , `*` ; and `^` with integer powers (among other things), and should be the first choice for someone who wants this kind of precision (eg for finance, labels on plot axes, etc).

There’s a performance trade-off though. Computing exactly how many shares you can buy for $6.00 at a share price of $0.20 is much faster in scaled floats than using `Rational`. Probably doesn’t matter for plot axes, but in high frequency trading, people are paying exorbitant rents for rack-space close to the stock exchange servers just to save a few nanoseconds on the time it takes to place an order…

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [October 22, 2018, 7:10am UTC](https://discourse.julialang.org/t/how-come-integer-division-6-0-by-0-2-returning-29-is-correct/16604/16 "2018-10-22T07:10:07Z")

</div>

For high frequency trading one has to deal with the weird nature of float™ anyways 😉

---

<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:** [October 22, 2018, 7:18am UTC](https://discourse.julialang.org/t/how-come-integer-division-6-0-by-0-2-returning-29-is-correct/16604/17 "2018-10-22T07:18:13Z")

</div>

> [@Per](#):
>
> in high frequency trading, people are paying exorbitant rents for rack-space

It is curious to see how a simple misunderstanding about floating point evolved into requirements of HFT.

Presumably the companies paying those exorbitant rents can also hire developers who understand floating point, or implement a fixed point float solution that is good enough for their requirements.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [October 22, 2018, 7:32am UTC](https://discourse.julialang.org/t/how-come-integer-division-6-0-by-0-2-returning-29-is-correct/16604/18 "2018-10-22T07:32:11Z")

</div>

Yeah, the reference to HF trading is perhaps a bit exaggerated. The point I wanted to make was: speed matters sometimes.

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [October 22, 2018, 11:24am UTC](https://discourse.julialang.org/t/how-come-integer-division-6-0-by-0-2-returning-29-is-correct/16604/19 "2018-10-22T11:24:14Z")

</div>

Look out for my paper in the AES julia special edition titled _Optimal polynomial form characteristic methods_, in it I have created a new mathematical terminology for floating point error bounds, if that sort of thing interests you. I will be posting the article on github also. In my paper I provide a detailed math explanation for why the floats are a pseudo-algebra and how it translates into abstract syntax tree optimization to select the best “equivalent” tree of operations. Actually, there is no equivalence, only a bound and characteristics that should be minimized. In the paper I introduce new characteristic methods to optimize floating point pseudo-algebra. Floating point arithmetic is actualy quite strange and interesting, there are still unexplored topics in that area, which nobody investigated yet.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [October 22, 2018, 8:46pm UTC](https://discourse.julialang.org/t/how-come-integer-division-6-0-by-0-2-returning-29-is-correct/16604/20 "2018-10-22T20:46:39Z")

</div>

There is also the DecFP.jl package, which wraps a library from Intel supporting the IEEE 754-2008 decimal floating point standard (32, 64, and 128 bit) (using the bid format).  
It’s as fast as `BigFloat` (when using BigFloat with 128-bit precision) (at least, last time I benchmarked the two of them), for at least the common operations that I’d tried, and doesn’t allocate anything on the heap.
