# Incorrect compute for rounding function round(x)

**URL:** <https://discourse.julialang.org/t/incorrect-compute-for-rounding-function-round-x/15051>\
**Category:** General Usage\
**Tags:** numbers\
**Created:** [September 17, 2018, 6:51am UTC](https://discourse.julialang.org/t/incorrect-compute-for-rounding-function-round-x/15051 "2018-09-17T06:51:49Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andy-xu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andy-xu/32/5395_2.png) [@Andy-xu](https://discourse.julialang.org/u/Andy-xu)\
**Post date:** [September 17, 2018, 6:51am UTC](https://discourse.julialang.org/t/incorrect-compute-for-rounding-function-round-x/15051/1 "2018-09-17T06:51:49Z")

</div>

I’m a Julia learner, and now i have a confuse question about the rounding function round(x), i’m not sure if it is bug, my compute system is window 64-bit, the version of Julia is 1.0, i have a problem while test the function round(x), post quoted code:  
julia\> round(6.5)  
6.0  
julia\> round(7.5)  
8.0  
julia\> round(17.5)  
18.0  
julia\> round(127.5)  
128.0  
julia\> round(8.5)  
8.0  
As you can see, the parameter is 7.5 or xx7.5, function round(x) is shrink, this different from the other parameters, i’m not sure what happen, just post it to ask for a help, thanks  
please forgive my unmature english presentation,

---

<div class="post-metadata">

**Author:** ![saschatimme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saschatimme/32/10313_2.png) [@saschatimme](https://discourse.julialang.org/u/saschatimme)\
**Post date:** [September 17, 2018, 6:58am UTC](https://discourse.julialang.org/t/incorrect-compute-for-rounding-function-round-x/15051/2 "2018-09-17T06:58:04Z")

</div>

This is specified in the IEEE 754 (the floating point standard) rounding rules ([Wikipedia](https://en.wikipedia.org/wiki/IEEE_754#Rounding_rules)), in particular by default there is the following rule

- **[Round to nearest, ties to even](https://en.wikipedia.org/wiki/Rounding#Round_half_to_even)** – rounds to the nearest value; if the number falls midway it is rounded to the nearest value with an even (zero) least significant bit; this is the default for binary floating-point and the recommended default for decimal.

---

<div class="post-metadata">

**Author:** ![Andy-xu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andy-xu/32/5395_2.png) [@Andy-xu](https://discourse.julialang.org/u/Andy-xu)\
**Post date:** [September 17, 2018, 11:22am UTC](https://discourse.julialang.org/t/incorrect-compute-for-rounding-function-round-x/15051/3 "2018-09-17T11:22:57Z")

</div>

Thanks for your reply,  
Function round(x) really obey the rule IEEE 754, i just can’t understand the compute value of round(6.5) is 6.0, but the compute value of round(7.5) is 8.0, it seems they obey two rule, i think the value of round(6.5) should be 7.0. do you think so?

---

<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:** [September 17, 2018, 11:30am UTC](https://discourse.julialang.org/t/incorrect-compute-for-rounding-function-round-x/15051/4 "2018-09-17T11:30:06Z")

</div>

Yes, it’s not what you’re taught in school, but this is exactly what the standard says the default behavior for rounding should be.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [September 17, 2018, 3:11pm UTC](https://discourse.julialang.org/t/incorrect-compute-for-rounding-function-round-x/15051/5 "2018-09-17T15:11:05Z")

</div>

Note that you can specify a `RoundingMode` to alter this behavior:

```julia
  Currently supported rounding modes are:

    • RoundNearest (default)
    • RoundNearestTiesAway
    • RoundNearestTiesUp
    • RoundToZero
    • RoundFromZero (BigFloat only)
    • RoundUp
    • RoundDown

```

I think `RoundNearestTiesUp` is what most people learn in primary school (edit: n = 1, ca. 1976, United States).

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [September 17, 2018, 3:27pm UTC](https://discourse.julialang.org/t/incorrect-compute-for-rounding-function-round-x/15051/6 "2018-09-17T15:27:43Z")

</div>

> [@Andy-xu](#):
>
> it seems they obey two rule

It is actually one rule and the two cases you’ve noted **is** the rule.

> [@anon94023334](#):
>
> I think `RoundNearestTiesUp` is what most people learn in primary school.

In the US, maybe. Definitely not in China (at least not ~ 20yrs ago).

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [September 17, 2018, 3:29pm UTC](https://discourse.julialang.org/t/incorrect-compute-for-rounding-function-round-x/15051/7 "2018-09-17T15:29:16Z")

</div>

> [@yuyichao](#):
>
> In the US, maybe. Definitely not in China (at least not ~ 20yrs ago).

Fair point. Edited.

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [September 17, 2018, 6:12pm UTC](https://discourse.julialang.org/t/incorrect-compute-for-rounding-function-round-x/15051/8 "2018-09-17T18:12:39Z")

</div>

> [@yuyichao](#):
>
> In the US, maybe. Definitely not in China (at least not ~ 20yrs ago).

Good thing we didn’t copy [AppleScript’s naming conventions](https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html#//apple_ref/doc/uid/TP40000983-CH216-DontLinkElementID_794) then.

---

<div class="post-metadata">

**Author:** ![Andy-xu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andy-xu/32/5395_2.png) [@Andy-xu](https://discourse.julialang.org/u/Andy-xu)\
**Post date:** [September 18, 2018, 8:16am UTC](https://discourse.julialang.org/t/incorrect-compute-for-rounding-function-round-x/15051/9 "2018-09-18T08:16:48Z")

</div>

Thanks for above all analyze, this behave … broke down my previous idea, its really reasonable even if its different from my learned, by the way, im a python user, so i never run into this problem until using Julia, however, thanks Juila, thanks a ton for yous.

---

<div class="post-metadata">

**Author:** ![dawbarton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dawbarton/32/215461_2.png) [@dawbarton](https://discourse.julialang.org/u/dawbarton)\
**Post date:** [September 18, 2018, 8:52am UTC](https://discourse.julialang.org/t/incorrect-compute-for-rounding-function-round-x/15051/10 "2018-09-18T08:52:07Z")

</div>

BTW: it is the same behaviour as Python

```python
Python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:27:45) [MSC v.1900 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: round(6.5)
Out[1]: 6

In [2]: round(7.5)
Out[2]: 8

```

But I note that Matlab thinks differently…

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 18, 2018, 10:30am UTC](https://discourse.julialang.org/t/incorrect-compute-for-rounding-function-round-x/15051/11 "2018-09-18T10:30:54Z")

</div>

The default rounding mode in a programming language is often as much a topic of debate as the famous tabs vs. spaces. Hence there are differences. I think there are even some that skirt around that debate by forcing the user to explicitly specify a rounding mode.

---

<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 18, 2018, 11:51am UTC](https://discourse.julialang.org/t/incorrect-compute-for-rounding-function-round-x/15051/12 "2018-09-18T11:51:12Z")

</div>

I think this is a bit different. In the tabs vs spaces debate (or the other similar ones), users are usually aware of the alternatives and have a preferred one.

OTOH, when it comes to rounding (and other features of floating point), the issue usually comes up because the user is surprised at the result, **then** becomes informed about different rounding modes etc. At this point, arguing for the “unsurpising” solution is natural, but after people think about it, they usually realize the technical reasons for one choice or another and their preferences become more reasoned.

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [September 19, 2018, 4:02am UTC](https://discourse.julialang.org/t/incorrect-compute-for-rounding-function-round-x/15051/13 "2018-09-19T04:02:09Z")

</div>

> [@dawbarton](#):
>
> BTW: it is the same behaviour as Python

This was one of the Python 3 changes, Python 2 used C’s “half away” behaviour.
