# Round() can anybody explain this behavior to me?

**URL:** <https://discourse.julialang.org/t/round-can-anybody-explain-this-behavior-to-me/29230>\
**Category:** General Usage\
**Tags:** question, bug\
**Created:** [September 27, 2019, 12:39pm UTC](https://discourse.julialang.org/t/round-can-anybody-explain-this-behavior-to-me/29230 "2019-09-27T12:39:00Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [September 27, 2019, 12:39pm UTC](https://discourse.julialang.org/t/round-can-anybody-explain-this-behavior-to-me/29230/1 "2019-09-27T12:39:00Z")

</div>

Just see (julia 1.2):

```julia
julia> round(1e24;digits=1)
9.999999999999998e23

julia> round(1e24;sigdigits=1)
1.0e24

julia> round(1e23;digits=1)
1.0e23

julia> round(1e23;sigdigits=1)
1.0000000000000001e23

julia> round(1e23;sigdigits=2)
1.0e23

```

Possible bug?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [September 27, 2019, 12:51pm UTC](https://discourse.julialang.org/t/round-can-anybody-explain-this-behavior-to-me/29230/2 "2019-09-27T12:51:51Z")

</div>

1e24 is actually outside the range of values for which every integer can be exactly represented as an IEEE double-precision floating point number (i.e. a Julia Float64). So even if you write `1e24`, you will actually get the closest `Float64`, which turns out to be slightly less than 1e24:

```julia
julia> using Printf

julia> @printf("%.17g", 1e24)
9.9999999999999998e+23

```

If you need higher-precision arithmetic on such large numbers, you could use a `BigFloat`:

```julia
julia> @printf("%.17g", big"1e24")
1e+24

```

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [September 27, 2019, 12:59pm UTC](https://discourse.julialang.org/t/round-can-anybody-explain-this-behavior-to-me/29230/3 "2019-09-27T12:59:29Z")

</div>

Ohh thanks i got it, how stupid from me. The value is stored as an (52bit) \*2^(11bit) number and not as an (52bit)\*10^(11bit). I never had the chance to learn the basics…

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [September 27, 2019, 1:45pm UTC](https://discourse.julialang.org/t/round-can-anybody-explain-this-behavior-to-me/29230/4 "2019-09-27T13:45:00Z")

</div>

Still one question

> [@rdeits](#):
>
> ). So even if you write `1e24` , you will actually get the closest `Float64` , which turns out to be slightly less than 1e24

But why is

```julia
julia> round(1e23;sigdigits=1)
1.0000000000000001e23

julia> round(1e23;sigdigits=2)
1.0e23

```

different then?

---

<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:** [September 27, 2019, 4:42pm UTC](https://discourse.julialang.org/t/round-can-anybody-explain-this-behavior-to-me/29230/5 "2019-09-27T16:42:35Z")

</div>

Consider upgrading your version of Julia  
(that does not happen in v1.2).

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [September 28, 2019, 8:32pm UTC](https://discourse.julialang.org/t/round-can-anybody-explain-this-behavior-to-me/29230/6 "2019-09-28T20:32:08Z")

</div>

I’d be happy you were right, but tested it on two different installations now with the same result:

 ![session](https://global.discourse-cdn.com/julialang/original/3X/7/c/7c19713716dc6d46a4bd388b593da14b750a203c.png)

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [September 28, 2019, 9:06pm UTC](https://discourse.julialang.org/t/round-can-anybody-explain-this-behavior-to-me/29230/7 "2019-09-28T21:06:02Z")

</div>

I get the same behavior on 1.3.0-rc2 FYI.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [September 29, 2019, 9:20am UTC](https://discourse.julialang.org/t/round-can-anybody-explain-this-behavior-to-me/29230/8 "2019-09-29T09:20:35Z")

</div>

This is strange. What kind of hardware architecture do you have?

What do you get when you try to round `1e23` with more than 16 digits, i.e. as rdeits suggested above:

```julia
julia> using Printf

julia> @printf("%.17g", 1e23)
9.9999999999999992e+22

```

---

<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 29, 2019, 9:30am UTC](https://discourse.julialang.org/t/round-can-anybody-explain-this-behavior-to-me/29230/9 "2019-09-29T09:30:15Z")

</div>

(please [quote your output](https://discourse.julialang.org/t/psa-how-to-quote-code-with-backticks/7530), it is much easier to read)

I can replicate this on various versions (1.1, 1.2, 1.3). May be related:

[https://github.com/JuliaLang/julia/issues/33103](https://github.com/JuliaLang/julia/issues/33103)

---

<div class="post-metadata">

**Author:** ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)\
**Post date:** [September 29, 2019, 8:09pm UTC](https://discourse.julialang.org/t/round-can-anybody-explain-this-behavior-to-me/29230/10 "2019-09-29T20:09:06Z")

</div>

> [@ffevotte](#):
>
> What kind of hardware architecture do you have?

x86\_64

> [@ffevotte](#):
>
> What do you get when you try to round `1e23` with more than 16 digits, i.e. as rdeits suggested above:

```julia
julia> @printf("%.17g", 1e23)
9.9999999999999992e+22

```

---

<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:** [September 30, 2019, 5:14pm UTC](https://discourse.julialang.org/t/round-can-anybody-explain-this-behavior-to-me/29230/11 "2019-09-30T17:14:16Z")

</div>

![Capture](https://global.discourse-cdn.com/julialang/original/3X/a/6/a6dc849508a05af4c6c93e41f6e71cdfa8851605.png)

I don’t know why you would have different results.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [September 30, 2019, 6:26pm UTC](https://discourse.julialang.org/t/round-can-anybody-explain-this-behavior-to-me/29230/12 "2019-09-30T18:26:45Z")

</div>

Yes, I can confirm that I get the same results as JeffreySarnoff.

@tbeason & @Tamas_Papp I’m not sure I understood your messages: do you get the same behaviour as MatFi or as JeffreySarnoff?

This looks like a bug to me, but I don’t know where it could come from… I might be wrong, but to me it does not seem related to the issue mentioned by Tamas above.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [September 30, 2019, 6:45pm UTC](https://discourse.julialang.org/t/round-can-anybody-explain-this-behavior-to-me/29230/13 "2019-09-30T18:45:35Z")

</div>

On my Linux machine, I see the same results as the OP. Now that I’m on my Win10 desktop, I get results in line with JeffreySarnoff.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [September 30, 2019, 7:04pm UTC](https://discourse.julialang.org/t/round-can-anybody-explain-this-behavior-to-me/29230/14 "2019-09-30T19:04:54Z")

</div>

Thanks, this definitely confirms that there is a problem somewhere. FWIW, the system on which I observe the same results as JeffreySarnoff is a Linux box.

@MatFi perhaps you should open a new issue?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [September 30, 2019, 7:52pm UTC](https://discourse.julialang.org/t/round-can-anybody-explain-this-behavior-to-me/29230/15 "2019-09-30T19:52:19Z")

</div>

```julia
julia> round(1e23;sigdigits=1)
1.0000000000000001e23

julia> round(1e23;sigdigits=2)
1.0e23

julia> versioninfo()
Julia Version 1.2.0
Commit c6da87ff4b (2019-08-20 00:03 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
Environment:
  JULIA_NUM_THREADS = 4

```

And I can confirm Win10 results agrees with @JeffreySarnoff (with the same Julia version ofc)

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [September 30, 2019, 8:03pm UTC](https://discourse.julialang.org/t/round-can-anybody-explain-this-behavior-to-me/29230/16 "2019-09-30T20:03:13Z")

</div>

Windows 64-bit.

```julia
julia> round(1e23;sigdigits=1)
1.0e23

julia> round(1e23;sigdigits = 2)
1.0e23

julia> versioninfo()
Julia Version 1.2.0
Commit c6da87ff4b (2019-08-20 00:03 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-4650U CPU @ 1.70GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, haswell)
Environment:
  JULIA_EDITOR = "C:\Users\PK\AppData\Local\atom\app-1.40.1\atom.exe" -a
  JULIA_NUM_THREADS = 2

```

---

<div class="post-metadata">

**Author:** ![KajWiik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kajwiik/32/199_2.png) [@KajWiik](https://discourse.julialang.org/u/KajWiik)\
**Post date:** [September 30, 2019, 8:53pm UTC](https://discourse.julialang.org/t/round-can-anybody-explain-this-behavior-to-me/29230/17 "2019-09-30T20:53:30Z")

</div>

```julia
julia> round(1e23;sigdigits=1)
1.0e23

julia> round(1e23;sigdigits=2)
1.0e23

julia> versioninfo()
Julia Version 1.1.0
Commit 80516ca202 (2019-01-21 21:24 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-2860QM CPU @ 2.50GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, sandybridge)

```

---

<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:** [September 30, 2019, 10:54pm UTC](https://discourse.julialang.org/t/round-can-anybody-explain-this-behavior-to-me/29230/18 "2019-09-30T22:54:13Z")

</div>

I opened an issue: [https://github.com/JuliaLang/julia/issues/33428](https://github.com/JuliaLang/julia/issues/33428)

---

<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 1, 2019, 5:37am UTC](https://discourse.julialang.org/t/round-can-anybody-explain-this-behavior-to-me/29230/19 "2019-10-01T05:37:03Z")

</div>

```julia
julia> round(1e23;sigdigits=1)
1.0000000000000001e23

julia> round(1e23;sigdigits=2)
1.0e23

julia> versioninfo()
Julia Version 1.3.0-rc2.0
Commit a04936e3e0 (2019-09-12 19:49 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
Environment:
  JULIA_CMDSTAN_HOME = /home/tamas/src/cmdstan/

```
