Round() can anybody explain this behavior to me?

Just see (julia 1.2):

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?

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> 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> @printf("%.17g", big"1e24")
1e+24
3 Likes

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…

Still one question

But why is

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

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

different then?

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

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

I get the same behavior on 1.3.0-rc2 FYI.

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> using Printf

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

(please quote your output, 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

x86_64

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

1 Like

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

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.

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.

1 Like

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?

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)

Windows 64-bit.

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
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)

I opened an issue: https://github.com/JuliaLang/julia/issues/33428

3 Likes
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/