Unexpected result from setprecision

I expected the following to return 512, rather than 256. Could someone please explain why this is happening?

julia> setprecision(512) do
           thisbig = big"1.0"
           @show precision(thisbig)
       end
precision(thisbig) = 256
256

This is obtained on Julia 1.8.2:

julia> versioninfo()
Julia Version 1.8.2
Commit 36034abf260 (2022-09-29 15:21 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, skylake)
  Threads: 8 on 8 virtual cores
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 8

I think the problem is that the @big_str macro is evaluated and parses the value at compile time in the global scope before the setprecision call happens. As a workaround:

julia> setprecision(512) do
           b=parse(BigFloat, "1.0")
           @show precision(b)
       end
precision(b) = 512
512
5 Likes

There is also BigFloat("1.0") as an alternative to using parse (and also BigFloat("1.0", precision=512)).

3 Likes

FTR this is better documented nowadays:

… and …

3 Likes