# Rounding off error in simple calculations

**URL:** <https://discourse.julialang.org/t/rounding-off-error-in-simple-calculations/8543>\
**Category:** Numerics\
**Created:** [January 23, 2018, 11:49am UTC](https://discourse.julialang.org/t/rounding-off-error-in-simple-calculations/8543 "2018-01-23T11:49:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sambitdash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sambitdash/32/1377_2.png) [@sambitdash](https://discourse.julialang.org/u/sambitdash)\
**Post date:** [January 23, 2018, 11:49am UTC](https://discourse.julialang.org/t/rounding-off-error-in-simple-calculations/8543/1 "2018-01-23T11:49:20Z")

</div>

```julia
julia> 2.5/3
0.8333333333333334

```

The result should have been reported `0.8333333333333333`. An infinite series of 3s will never reach to 4 and will be a floor when rounded.

Is there any specific reason we have preferred rounding up in this case?

regards,

Sambit

Note: I still use 0.6.1. My apologies if the behavior has been changed in a later release.

---

<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:** [January 23, 2018, 12:05pm UTC](https://discourse.julialang.org/t/rounding-off-error-in-simple-calculations/8543/2 "2018-01-23T12:05:08Z")

</div>

I think this is just a printing issue:

```julia
julia> log(2, rationalize(2.5/3; tol = eps()/8) - 5//6)
-53.77760757866355

```

---

<div class="post-metadata">

**Author:** ![andreasnoack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasnoack/32/27_2.png) [@andreasnoack](https://discourse.julialang.org/u/andreasnoack)\
**Post date:** [January 23, 2018, 12:21pm UTC](https://discourse.julialang.org/t/rounding-off-error-in-simple-calculations/8543/3 "2018-01-23T12:21:08Z")

</div>

The rational number 5/6 is not representable as a binary float. The two nearest binary floats are

```julia
julia> 7505999378950826/big(2)^53
8.333333333333332593184650249895639717578887939453125000000000000000000000000000e-01

julia> 7505999378950827/big(2)^53
8.333333333333333703407674875052180141210556030273437500000000000000000000000000e-01

```

which reveals that the larger of them is actually closer to 5/6

```julia
julia> 7505999378950826/big(2)^53 - 5//6
-7.401486830834376936157544453938802083333333333333333333333333045461048163518512e-17

julia> 7505999378950827/big(2)^53 - 5//6
3.700743415417188468078772226969401041666666666666666666666666954538951836481488e-17

```

so this is not a bug. It is the usual issue that humans like decimal numbers but computers prefer binary numbers. Each time a number is read or written there is a conversion happening between the two representations and this is an eternal source of confusion.

---

<div class="post-metadata">

**Author:** ![sambitdash](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sambitdash/32/1377_2.png) [@sambitdash](https://discourse.julialang.org/u/sambitdash)\
**Post date:** [January 23, 2018, 12:34pm UTC](https://discourse.julialang.org/t/rounding-off-error-in-simple-calculations/8543/4 "2018-01-23T12:34:23Z")

</div>

@andreasnoack Thanks. But never thought that 5/6 falls into that category. I think 0.1 does not have a true binary representation. The behavior now makes perfect sense to me.
