# Is \`BigFloat\` loss of precision intended?

**URL:** https://discourse.julialang.org/t/is-bigfloat-loss-of-precision-intended/61728
**Category:** Internals & Design
**Tags:** precision, bigfloat
**Created:** [May 24, 2021, 3:51pm UTC](https://discourse.julialang.org/t/is-bigfloat-loss-of-precision-intended/61728 "2021-05-24T15:51:56Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![MarcMush](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcmush/32/18006_2.png) [@MarcMush](https://discourse.julialang.org/u/MarcMush)
#### Post date: [May 24, 2021, 3:51pm UTC](https://discourse.julialang.org/t/is-bigfloat-loss-of-precision-intended/61728/1 "2021-05-24T15:51:56Z")

</div>

when doing any operation on a `BigFloat`, the precision of the result always reverts back to the default one, instead of getting it from the operands.

Is this behavior intended and if yes, why ?

```julia
julia> a=BigFloat("1.1", precision=512);

julia> a.prec
512

julia> (a+a).prec
256

julia> (0+a).prec
256

```

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [May 24, 2021, 4:56pm UTC](https://discourse.julialang.org/t/is-bigfloat-loss-of-precision-intended/61728/2 "2021-05-24T16:56:10Z")

</div>

I don’t know, and I don’t know why it’s like this, but maybe

```julia
setprecision(512) do
    x = big"1.1"
    x+x
end

```

solves your problem?

---

<div class="post-metadata">

### Author: ![MarcMush](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcmush/32/18006_2.png) [@MarcMush](https://discourse.julialang.org/u/MarcMush)
#### Post date: [May 24, 2021, 5:06pm UTC](https://discourse.julialang.org/t/is-bigfloat-loss-of-precision-intended/61728/3 "2021-05-24T17:06:25Z")

</div>

I know about `setprecision`, but I was wondering the point of the `precision` keyword if it doesn’t propagate

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [May 24, 2021, 6:50pm UTC](https://discourse.julialang.org/t/is-bigfloat-loss-of-precision-intended/61728/4 "2021-05-24T18:50:20Z")

</div>

Hm. `base/mpfr.jl` defines these functions, and it’s an immediate `ccall`. I’m not sure there’s much one can do without changing it to `BigFloat{512}` etc. But I don’t know a lot about it.

---

<div class="post-metadata">

### Author: ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)
#### Post date: [May 24, 2021, 6:52pm UTC](https://discourse.julialang.org/t/is-bigfloat-loss-of-precision-intended/61728/5 "2021-05-24T18:52:23Z")

</div>

No, it is not an immediate ccall. It first allocates the result using `BigFloat()`, which uses the global precision and not the precision of the input (which it could).

PS.: So yes, this is intended. The precision of the result is always the global precision. This is the same behavior as MPFR itself.

---

<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: [May 24, 2021, 7:03pm UTC](https://discourse.julialang.org/t/is-bigfloat-loss-of-precision-intended/61728/6 "2021-05-24T19:03:06Z")

</div>

You must use `setprecision(BigFloat, nbits)` for precision to work properly (at nbits). The precision keyword is there because over time we have been considering how to rework the whole interface.
