# Unexpected result from setprecision

**URL:** <https://discourse.julialang.org/t/unexpected-result-from-setprecision/89336>\
**Category:** General Usage\
**Tags:** macros, scope, bigfloat\
**Created:** [October 26, 2022, 11:29pm UTC](https://discourse.julialang.org/t/unexpected-result-from-setprecision/89336 "2022-10-26T23:29:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeterSimon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petersimon/32/25193_2.png) [@PeterSimon](https://discourse.julialang.org/u/PeterSimon)\
**Post date:** [October 26, 2022, 11:29pm UTC](https://discourse.julialang.org/t/unexpected-result-from-setprecision/89336/1 "2022-10-26T23:29:35Z")

</div>

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

```julia
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
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

```

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [October 26, 2022, 11:43pm UTC](https://discourse.julialang.org/t/unexpected-result-from-setprecision/89336/2 "2022-10-26T23:43:53Z")

</div>

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
julia> setprecision(512) do
           b=parse(BigFloat, "1.0")
           @show precision(b)
       end
precision(b) = 512
512

```

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [October 27, 2022, 10:08am UTC](https://discourse.julialang.org/t/unexpected-result-from-setprecision/89336/3 "2022-10-27T10:08:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [October 18, 2024, 1:55am UTC](https://discourse.julialang.org/t/unexpected-result-from-setprecision/89336/4 "2024-10-18T01:55:09Z")

</div>

FTR this is better documented nowadays:

 ![manual](https://global.discourse-cdn.com/julialang/original/3X/f/c/fc3d4de0193a356eb41e97f522cbe1949d372478.png)

… and …

 ![big_str](https://global.discourse-cdn.com/julialang/original/3X/2/1/2132111741e8d752148abd225d0fae4dfa394479.png)
